From 8b407b67eb71068682b0e2fa8c9b3a7fd9754dbf Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Sun, 25 Feb 2024 23:22:29 +0100 Subject: [PATCH] fix crash when removing a player that had phantoms attached to it --- src/editor/PlayerDomains.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/editor/PlayerDomains.ts b/src/editor/PlayerDomains.ts index 7c2d23c..623d0ec 100644 --- a/src/editor/PlayerDomains.ts +++ b/src/editor/PlayerDomains.ts @@ -1,5 +1,5 @@ import {BallState, Player, PlayerLike, PlayerPhantom,} from "../model/tactic/Player" -import {TacticComponent, TacticContent} from "../model/tactic/Tactic" +import {ComponentId, TacticComponent, TacticContent} from "../model/tactic/Tactic" import {removeComponent, updateComponent} from "./TacticContentDomains" import {removeAllActionsTargeting, spreadNewStateFromOriginStateChange,} from "./ActionsDomains" import {ActionKind} from "../model/tactic/Action" @@ -56,7 +56,6 @@ export function computePhantomPositioning(phantom: PlayerPhantom, : playerBeforePhantom.pos) - const segment = posWithinBase(relativeTo(referentPos, pivotPoint), area) const segmentLength = norm(segment) const phantomDistanceFromReferent = PLAYER_RADIUS_PIXELS //TODO Place this in constants @@ -107,7 +106,7 @@ export function isNextInPath( ) } -export function removePlayerPath( +export function clearPlayerPath( player: Player, content: TacticContent, ): TacticContent { @@ -128,18 +127,35 @@ export function removePlayerPath( ) } +function removeAllPhantomsAttached(to: ComponentId, content: TacticContent): TacticContent { + let i = 0 + while (i < content.components.length) { + const component = content.components[i] + if (component.type === "phantom") { + + if (component.pos.type === "follows" && component.pos.attach === to) { + content = removePlayer(component, content) + continue + } + } + i++ + } + return content +} + export function removePlayer( player: PlayerLike, content: TacticContent, ): TacticContent { content = removeAllActionsTargeting(player.id, content) + content = removeAllPhantomsAttached(player.id, content) - if (player.type == "phantom") { + if (player.type === "phantom") { const origin = getOrigin(player, content.components) return truncatePlayerPath(origin, player, content) } - content = removePlayerPath(player, content) + content = clearPlayerPath(player, content) content = removeComponent(player.id, content) for (const action of player.actions) {