import "../../style/basket_court.css" import {ReactElement, RefObject} from "react" import CourtPlayer from "./CourtPlayer" import {Player} from "../../tactic/Player" import {Action, MovementActionKind} from "../../tactic/Action" import RemoveAction from "../actions/RemoveAction" import ArrowAction from "../actions/ArrowAction" import {useXarrow} from "react-xarrows" import BallAction from "../actions/BallAction"; import {CourtObject} from "../../tactic/CourtObjects"; import {CourtBall} from "./CourtBall"; export interface BasketCourtProps { players: Player[] actions: Action[] objects: CourtObject[] renderAction: (a: Action) => ReactElement setActions: (f: (a: Action[]) => Action[]) => void onPlayerRemove: (p: Player) => void onBallDrop: (ref: HTMLElement) => void onPlayerChange: (p: Player) => void onBallRemove: () => void onBallMoved: (ball: DOMRect) => void courtImage: string courtRef: RefObject } export function BasketCourt({ players, objects, actions, renderAction, setActions, onBallDrop, onPlayerRemove, onBallRemove, onBallMoved, onPlayerChange, courtImage, courtRef, }: BasketCourtProps) { function bindArrowToPlayer( originRef: RefObject, arrowHead: DOMRect, ) { for (const player of players) { if (player.id == originRef.current!.id) { continue } const playerBounds = document .getElementById(player.id)! .getBoundingClientRect() if ( !( playerBounds.top > arrowHead.bottom || playerBounds.right < arrowHead.left || playerBounds.bottom < arrowHead.top || playerBounds.left > arrowHead.right ) ) { const action = { type: MovementActionKind.SCREEN, moveFrom: originRef.current!.id, moveTo: player.id, } setActions((actions) => [...actions, action]) } } } const updateArrows = useXarrow() return (
{"court"} {players.map((player) => ( onPlayerRemove(player)} parentRef={courtRef} availableActions={(pieceRef) => [ onPlayerRemove(player)} />, bindArrowToPlayer(pieceRef, headRect) } />, player.hasBall && ]} /> ))} {objects.map((object) => { if (object.type == "ball") { return ( ) } throw new Error("unknown court object", object.type) })} {actions.map(renderAction)}
) }