fix crash when removing a player that was attached, or a screen action

pull/113/head
maxime.batista 1 year ago
parent 8b407b67eb
commit b6f2a97d8f

@ -7,8 +7,8 @@ import {removeBall, updateComponent} from "./TacticContentDomains"
import {
areInSamePath,
changePlayerBallState,
getComponent,
getOrigin,
getPlayerNextTo,
isNextInPath,
removePlayer
} from "./PlayerDomains"
@ -379,10 +379,10 @@ export function removeAllActionsTargeting(
export function removeAction(
origin: TacticComponent,
action: Action,
actionIdx: number,
content: TacticContent,
): TacticContent {
const action = origin.actions[actionIdx]
origin = {
...origin,
actions: origin.actions.toSpliced(actionIdx, 1),
@ -431,12 +431,8 @@ export function removeAction(
// if the action type is a screen over a player, remove the phantom bound to the target
if (action.type === ActionKind.SCREEN && (origin.type === "phantom" || origin.type === "player")) {
const playerOrigin = origin.type === "phantom" ? getOrigin(origin, content.components) : origin
const pathItems = playerOrigin.path?.items!
const originIdx = pathItems.indexOf(origin.id)
// remove the screen phantom
const phantomId = pathItems.at(originIdx + 1)!
content = removePlayer(getComponent(phantomId, content.components), content)
const screenPhantom = getPlayerNextTo(origin, 1, content.components)!
content = removePlayer(screenPhantom, content)
}
return content
@ -506,7 +502,7 @@ export function spreadNewStateFromOriginStateChange(
}
if (deleteAction) {
content = removeAction(origin, action, i, content)
content = removeAction(origin, i, content)
origin = content.components.find((c) => c.id === origin.id)! as
| Player
| PlayerPhantom

@ -14,6 +14,21 @@ export function getOrigin(
return components.find((c) => c.id == pathItem.originPlayerId)! as Player
}
export function getPlayerNextTo(player: PlayerLike, n: number, components: TacticComponent[]): PlayerLike | undefined {
const playerOrigin = player.type === "player" ? player : getOrigin(player, components)
const pathItems = playerOrigin.path?.items!
// add one as there is a shifting because a Player is never at the head of its own path
const idx = pathItems.indexOf(player.id) + 1 // is 0 if the player is the origin
const targetIdx = idx + n
// remove the screen phantom
const result = targetIdx == 0 ? playerOrigin : getComponent<PlayerLike>(pathItems[targetIdx - 1], components)
console.log(result, targetIdx, idx, n)
return result
}
//FIXME this function can be a bottleneck if the phantom's position is
// following another phantom and / or the origin of the phantom is another
export function computePhantomPositioning(phantom: PlayerPhantom,
@ -151,6 +166,19 @@ export function removePlayer(
content = removeAllPhantomsAttached(player.id, content)
if (player.type === "phantom") {
const pos = player.pos
// if the phantom was attached to another player, remove the action that symbolizes the attachment
if (pos.type === "follows") {
const playerBefore = getPlayerNextTo(player, -1, content.components)!
const actionIdx = playerBefore.actions.findIndex(a => a.target === pos.attach)
console.log(actionIdx, playerBefore)
content = updateComponent({
...playerBefore,
actions: playerBefore.actions.toSpliced(actionIdx, 1)
}, content)
}
const origin = getOrigin(player, content.components)
return truncatePlayerPath(origin, player, content)
}

@ -434,8 +434,8 @@ function EditorView({
)
const doDeleteAction = useCallback(
(action: Action, idx: number, origin: TacticComponent) => {
setContent((content) => removeAction(origin, action, idx, content))
(_: Action, idx: number, origin: TacticComponent) => {
setContent((content) => removeAction(origin, idx, content))
},
[setContent],
)

Loading…
Cancel
Save