Pathfinding Logic
Relevant source files
The following files were used as context for generating this wiki page:
The Pathfinding Logic in Kilo Man refers to the automated movement patterns and boundary-constrained behaviors of non-player entities, specifically "Monsters." Unlike complex graph-based pathfinding, this system utilizes a deterministic patrol-based logic where entities navigate between predefined spatial markers within the game world.
This system ensures that hazards move predictably within their designated territories, providing consistent challenges for the player. The logic is tightly integrated into the main game loop, where position updates and boundary checks are performed every frame to maintain smooth movement and collision accuracy.
Sources: architecture_plan.md:32-36, app/components/Game/GameCanvas.tsx:238-251
Entity Movement Architecture
The movement of hostile entities is governed by a patrol system defined in the level data. Each monster entity is assigned a horizontal velocity and a specific spatial range. The logic follows a "Ping-Pong" pattern where the entity reverses its velocity upon reaching the limits of its assigned patrolStart and patrolEnd coordinates.
Patrol Logic Components
| Property | Type | Description |
|---|---|---|
x | number | The current horizontal position of the entity. |
vx | number | The current velocity. Positive moves right, negative moves left. |
patrolStart | number | The minimum X-coordinate the entity can reach before reversing. |
patrolEnd | number | The maximum X-coordinate the entity can reach before reversing. |
speed | number | The magnitude of the velocity applied to the entity. |
Sources: app/components/Game/types.ts:34-43, app/components/Game/GameCanvas.tsx:112-125
Movement Flow
The following diagram illustrates the frame-by-frame logic applied to moving entities to simulate pathfinding within a fixed range:
The movement loop ensures entities never drift outside their intended zones due to floating-point errors by snapping the position to the boundary upon reversal.
Sources: app/components/Game/GameCanvas.tsx:239-251
Interaction and Collision
While the entities follow a fixed path, the pathfinding system interacts with the player through a simple AABB (Axis-Aligned Bounding Box) collision check. If the player's coordinates overlap with the monster's current pathing coordinates, the game state transitions to a "lost" status.
Implementation Details
The monsters are initialized during the "playing" state transition. The system extracts monster definitions from levelData.ts and maps them into a mutable monstersRef to track their individual paths.
// Initializing pathing entities
monstersRef.current = LEVEL_1
.filter(e => e.type === 'monster')
.map(m => ({
id: m.id,
x: m.x,
y: m.y,
w: m.w,
h: m.h,
vx: m.speed || 2,
patrolStart: m.patrolStart || m.x - 100,
patrolEnd: m.patrolEnd || m.x + 100,
speed: m.speed || 2,
}));
Sources: app/components/Game/GameCanvas.tsx:112-125
Spatial Configuration
In the current level design, pathfinding ranges vary significantly to create different difficulty tiers:
- Intro Guard: Patrols a short range (800 to 950) at a speed of 2.
- Speed Hazard: Patrols a wide range (2000 to 2500) at a speed of 4.
- Final Guard: High-speed patrol (speed 6) in the end-game section (6100 to 6400).
Sources: app/components/Game/levelData.ts:30, app/components/Game/levelData.ts:46, app/components/Game/levelData.ts:80
The Pathfinding Logic effectively manages entity movement by utilizing simple boundary-based oscillation, ensuring that hazards remain within their designated platform or area while providing dynamic obstacles for the player.