|
|
|
@ -1,11 +1,4 @@
|
|
|
|
|
import {
|
|
|
|
|
CSSProperties,
|
|
|
|
|
Dispatch,
|
|
|
|
|
SetStateAction,
|
|
|
|
|
useCallback,
|
|
|
|
|
useRef,
|
|
|
|
|
useState,
|
|
|
|
|
} from "react"
|
|
|
|
|
import {CSSProperties, Dispatch, SetStateAction, useCallback, useRef, useState,} from "react"
|
|
|
|
|
import "../style/editor.css"
|
|
|
|
|
import TitleInput from "../components/TitleInput"
|
|
|
|
|
import { BasketCourt } from "../components/editor/BasketCourt"
|
|
|
|
@ -16,8 +9,6 @@ import halfCourt from "../assets/court/half_court.svg"
|
|
|
|
|
|
|
|
|
|
import {BallPiece, CourtBall} from "../components/editor/BallPiece";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { Rack } from "../components/Rack"
|
|
|
|
|
import { PlayerPiece } from "../components/editor/PlayerPiece"
|
|
|
|
|
import { Player } from "../tactic/Player"
|
|
|
|
@ -66,7 +57,6 @@ interface RackedPlayer {
|
|
|
|
|
|
|
|
|
|
type RackedCourtObject = { key: "ball" }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function Editor({
|
|
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
@ -139,17 +129,12 @@ function EditorView({
|
|
|
|
|
const [opponents, setOpponents] = useState(
|
|
|
|
|
getRackPlayers(Team.Opponents, content.players),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const [objects, setObjects] = useState<RackedCourtObject[]>(
|
|
|
|
|
isBallOnCourt(content) ? [] : [{ key: "ball" }],
|
|
|
|
|
)
|
|
|
|
|
const [objects, setObjects] = useState<RackedCourtObject[]>([{key: "ball"}])
|
|
|
|
|
|
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isBoundsOnCourt = (bounds: DOMRect) => {
|
|
|
|
|
const rackBallRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
const [rackBall, setRackBall] = useState<boolean>(true)
|
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
const canDetach = (bounds: DOMRect) => {
|
|
|
|
@ -184,7 +169,6 @@ function EditorView({
|
|
|
|
|
hasBall: false,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
ball: content.ball
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
@ -236,46 +220,37 @@ function EditorView({
|
|
|
|
|
objects: [...content.objects, courtObject],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onBallDetach = (pos: DOMRect) => {
|
|
|
|
|
const onObjectDetach = (ref: HTMLDivElement, rackedObject: RackedCourtObject) => {
|
|
|
|
|
const refBounds = ref.getBoundingClientRect()
|
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
const {x, y} = calculateRatio(pos, courtBounds)
|
|
|
|
|
const {x, y} = calculateRatio(refBounds, courtBounds)
|
|
|
|
|
|
|
|
|
|
setContent((content) => {
|
|
|
|
|
return {
|
|
|
|
|
players: content.players,
|
|
|
|
|
ball: {
|
|
|
|
|
let courtObject: CourtObject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (rackedObject.key) {
|
|
|
|
|
case "ball":
|
|
|
|
|
courtObject = {
|
|
|
|
|
type: "ball",
|
|
|
|
|
rightRatio: x,
|
|
|
|
|
bottomRatio: y
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onBallDrop = (ballBounds: DOMRect) => {
|
|
|
|
|
let ballAssigned = false
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
throw new Error("unknown court object ", rackedObject.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setContent(content => {
|
|
|
|
|
const players = content.players.map(player => {
|
|
|
|
|
if (ballAssigned) {
|
|
|
|
|
return {...player, hasBall: false}
|
|
|
|
|
}
|
|
|
|
|
const playerBounds = document.getElementById(player.id)!.getBoundingClientRect()
|
|
|
|
|
const doesOverlap = !(
|
|
|
|
|
ballBounds.top > playerBounds.bottom ||
|
|
|
|
|
ballBounds.right < playerBounds.left ||
|
|
|
|
|
ballBounds.bottom < playerBounds.top ||
|
|
|
|
|
ballBounds.left > playerBounds.right
|
|
|
|
|
)
|
|
|
|
|
if (doesOverlap) {
|
|
|
|
|
ballAssigned = true
|
|
|
|
|
}
|
|
|
|
|
return {...player, hasBall: doesOverlap}
|
|
|
|
|
setContent((content) =>
|
|
|
|
|
({
|
|
|
|
|
...content,
|
|
|
|
|
objects: [
|
|
|
|
|
...content.objects,
|
|
|
|
|
courtObject,
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
return {players: players, ball: content.ball}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getPlayerCollided = (
|
|
|
|
|
bounds: DOMRect,
|
|
|
|
|
players: Player[],
|
|
|
|
@ -496,7 +471,6 @@ function EditorView({
|
|
|
|
|
player,
|
|
|
|
|
true,
|
|
|
|
|
),
|
|
|
|
|
ball: content.ball
|
|
|
|
|
}))
|
|
|
|
|
}}
|
|
|
|
|
onPlayerRemove={(player) => {
|
|
|
|
@ -513,6 +487,7 @@ function EditorView({
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isBallOnCourt(content: TacticContent) {
|
|
|
|
|
if (content.players.findIndex((p) => p.hasBall) != -1) {
|
|
|
|
|
return true
|
|
|
|
@ -522,7 +497,7 @@ function isBallOnCourt(content: TacticContent) {
|
|
|
|
|
|
|
|
|
|
function renderCourtObject(courtObject: RackedCourtObject) {
|
|
|
|
|
if (courtObject.key == "ball") {
|
|
|
|
|
return <BallPiece />
|
|
|
|
|
return <BallPiece/>
|
|
|
|
|
}
|
|
|
|
|
throw new Error("unknown racked court object ", courtObject.key)
|
|
|
|
|
}
|
|
|
|
|