diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-03-01 21:52:49 +0100 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-03-01 21:52:49 +0100 |
| commit | 5728ebc309b4753c939d751c7fd489b108e5dc09 (patch) | |
| tree | 6bcbe6058a5cfa1e55941702d5fdb6c359d0f5f7 /physics/move.go | |
| parent | ffff169f10b35efaf9f793f745bdfed473ca4d9a (diff) | |
fix: prevent entities from overlapping when arriving at the same cell
Update UpdateMoveSystem to maintain the moveMap as entities move
within a tick. When an entity snaps to a grid cell:
- Block the new cell in the moveMap immediately
- Unblock the old cell
Before snapping, check if the target cell was already claimed by
another entity earlier in the same tick. If so, repath instead of
overlapping.
Diffstat (limited to 'physics/move.go')
| -rw-r--r-- | physics/move.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/physics/move.go b/physics/move.go index 94da69f..b898ec7 100644 --- a/physics/move.go +++ b/physics/move.go @@ -192,7 +192,22 @@ func UpdateMoveSystem(moveMap MoveMap, masks []ecs.Mask, moves []Move, positions *position = moveToward(*position, next) continue } + // About to snap to next cell — check it's still free + if moveMap.Blocked[next] { + slog.Info("cell blocked on arrival, repathing", slog.String("next", next.String())) + path, ok := SearchPath(*position, move.Destination, moveMap) + if !ok { + slog.Info("no path found", slog.String("destination", move.Destination.String())) + move.IsActive = false + continue + } + move.Path = path[1:] + continue + } + oldCell := Point{X: position.X / 100 * 100, Y: position.Y / 100 * 100} *position = next + moveMap.Blocked[next] = true + delete(moveMap.Blocked, oldCell) remainingPath := move.Path[1:] if next == move.Destination { move.IsActive = false |
