can put the ball on the court if we want

pull/77/head
Vivien DUFOUR 1 year ago committed by vidufour1
parent a7a2df158c
commit c3fbd45c2f

@ -1,6 +1,10 @@
import { RefObject, useRef } from "react" import { RefObject, useRef } from "react"
import "../../style/player.css" import "../../style/player.css"
<<<<<<< HEAD
import { BallPiece } from "./BallPiece" import { BallPiece } from "./BallPiece"
=======
import {BallPiece} from "./BallPiece"
>>>>>>> 20078f8 (can put the ball on the court if we want)
import Draggable from "react-draggable" import Draggable from "react-draggable"
import { PlayerPiece } from "./PlayerPiece" import { PlayerPiece } from "./PlayerPiece"
import { Player } from "../../tactic/Player" import { Player } from "../../tactic/Player"
@ -67,6 +71,7 @@ export default function CourtPlayer({
if (e.key == "Delete") onRemove() if (e.key == "Delete") onRemove()
}}> }}>
<div className="player-selection-tab"> <div className="player-selection-tab">
<<<<<<< HEAD
{hasBall && ( {hasBall && (
<Draggable <Draggable
nodeRef={ballPiece} nodeRef={ballPiece}
@ -81,6 +86,15 @@ export default function CourtPlayer({
</div> </div>
</Draggable> </Draggable>
)} )}
=======
{hasBall && (<Draggable nodeRef={ballPiece}
onStop={() => onBallDrop(ballPiece.current!.getBoundingClientRect())}
position={{x:0, y: 0}}>
<div ref={ballPiece}>
<BallPiece />
</div>
</Draggable>)}
>>>>>>> 20078f8 (can put the ball on the court if we want)
</div> </div>
<PlayerPiece <PlayerPiece
team={player.team} team={player.team}

@ -1,4 +1,11 @@
import {CSSProperties, Dispatch, SetStateAction, useCallback, useRef, useState,} from "react" import {
CSSProperties,
Dispatch,
SetStateAction,
useCallback,
useRef,
useState,
} from "react"
import "../style/editor.css" import "../style/editor.css"
import TitleInput from "../components/TitleInput" import TitleInput from "../components/TitleInput"
import { BasketCourt } from "../components/editor/BasketCourt" import { BasketCourt } from "../components/editor/BasketCourt"
@ -7,7 +14,7 @@ import plainCourt from "../assets/court/full_court.svg"
import halfCourt from "../assets/court/half_court.svg" import halfCourt from "../assets/court/half_court.svg"
import {BallPiece, CourtBall} from "../components/editor/BallPiece"; import { BallPiece } from "../components/editor/BallPiece"
import { Rack } from "../components/Rack" import { Rack } from "../components/Rack"
import { PlayerPiece } from "../components/editor/PlayerPiece" import { PlayerPiece } from "../components/editor/PlayerPiece"
@ -57,6 +64,7 @@ interface RackedPlayer {
type RackedCourtObject = { key: "ball" } type RackedCourtObject = { key: "ball" }
export default function Editor({ export default function Editor({
id, id,
name, name,
@ -113,7 +121,6 @@ function EditorView({
onNameChange, onNameChange,
courtType, courtType,
}: EditorViewProps) { }: EditorViewProps) {
const isInGuestMode = id == -1 const isInGuestMode = id == -1
const [titleStyle, setTitleStyle] = useState<CSSProperties>({}) const [titleStyle, setTitleStyle] = useState<CSSProperties>({})
@ -129,15 +136,15 @@ function EditorView({
const [opponents, setOpponents] = useState( const [opponents, setOpponents] = useState(
getRackPlayers(Team.Opponents, content.players), getRackPlayers(Team.Opponents, content.players),
) )
const [objects, setObjects] = useState<RackedCourtObject[]>([{key: "ball"}])
const [objects, setObjects] = useState<RackedCourtObject[]>(
isBallOnCourt(content) ? [] : [{ key: "ball" }],
)
const courtDivContentRef = useRef<HTMLDivElement>(null) const courtDivContentRef = useRef<HTMLDivElement>(null)
const isBoundsOnCourt = (bounds: DOMRect) => { const isBoundsOnCourt = (bounds: DOMRect) => {
const courtDivContentRef = useRef<HTMLDivElement>(null)
const canDetach = (bounds: DOMRect) => {
const courtBounds = courtDivContentRef.current!.getBoundingClientRect() const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
// check if refBounds overlaps courtBounds // check if refBounds overlaps courtBounds
@ -174,6 +181,7 @@ function EditorView({
} }
const onObjectDetach = ( const onObjectDetach = (
ref: HTMLDivElement,
rackedObject: RackedCourtObject, rackedObject: RackedCourtObject,
) => { ) => {
const refBounds = ref.getBoundingClientRect() const refBounds = ref.getBoundingClientRect()
@ -219,38 +227,9 @@ function EditorView({
...content, ...content,
objects: [...content.objects, courtObject], objects: [...content.objects, courtObject],
} }
const onObjectDetach = (ref: HTMLDivElement, rackedObject: RackedCourtObject) => {
const refBounds = ref.getBoundingClientRect()
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
const {x, y} = calculateRatio(refBounds, courtBounds)
let courtObject: CourtObject
switch (rackedObject.key) {
case "ball":
courtObject = {
type: "ball",
rightRatio: x,
bottomRatio: y
}
break
default:
throw new Error("unknown court object ", rackedObject.key)
}
setContent((content) =>
({
...content,
objects: [
...content.objects,
courtObject,
]
}) })
)
} }
const getPlayerCollided = ( const getPlayerCollided = (
bounds: DOMRect, bounds: DOMRect,
players: Player[], players: Player[],
@ -487,7 +466,6 @@ function EditorView({
) )
} }
function isBallOnCourt(content: TacticContent) { function isBallOnCourt(content: TacticContent) {
if (content.players.findIndex((p) => p.hasBall) != -1) { if (content.players.findIndex((p) => p.hasBall) != -1) {
return true return true

Loading…
Cancel
Save