Enemy AI Behavior
Relevant source files
The following files were used as context for generating this wiki page:
The Enemy AI Behavior system in Kilo Man governs the movement, state management, and interaction logic for "Monsters" within the game world. These entities serve as dynamic obstacles that patrol specific regions of a level, providing a layer of hazard distinct from static environmental threats.
The system is built upon a reactive architecture where monster states are initialized from level data and updated within a centralized game loop. If a player collides with a monster, the game transitions to a "lost" state, necessitating a mission retry.
Sources: architecture_plan.md:33-37, app/components/Game/GameCanvas.tsx:300-308
AI Architecture and Data Structures
The Enemy AI is implemented using a decoupled approach where the static definition of an enemy is stored in the level configuration, while its active state is managed during runtime.
Data Models
Enemies are defined as LevelEntity objects with a specific monster type. During gameplay, these are transformed into MonsterState objects to track real-time position and velocity.
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the entity. |
x, y | number | Current spatial coordinates in the game world. |
vx | number | Horizontal velocity for movement. |
patrolStart | number | The left-most boundary of the patrol path. |
patrolEnd | number | The right-most boundary of the patrol path. |
speed | number | The constant movement velocity of the monster. |
Sources: app/components/Game/types.ts:24-33, app/components/Game/levelData.ts:10-12
State Initialization
When the game state changes to playing, the GameCanvas component filters the level data for entities of type monster and populates the monstersRef.
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:103-116
Movement Logic: Simple Patrol AI
The AI behavior follows a "Ping-Pong" patrol pattern. Monsters move horizontally at a constant speed until they hit a defined boundary (patrolStart or patrolEnd), at which point they reverse their velocity.
Patrol Flow
The following diagram illustrates the decision logic applied to each monster during the game loop update:
The logic ensures monsters never drift outside their intended zone and maintain consistent movement regardless of frame rate fluctuations (though logic is currently tied to the requestAnimationFrame loop).
Sources: app/components/Game/GameCanvas.tsx:238-250, architecture_plan.md:35-37
Collision and Interaction
The AI interacts with the player through an Axis-Aligned Bounding Box (AABB) collision detection system. Unlike platforms, which resolve collisions by pushing the player back, monster collisions are lethal.
Interaction Flow
When a collision is detected between the playerRef and any entry in monstersRef, the game state is immediately updated to lost.
Visual Representation
Monsters are rendered as distinct spiky entities to clearly indicate danger to the player, contrasting with the blocky, amber-colored platforms.
- Shape: A spiky diamond-like body created using four line segments.
- Color Palette: A red outer body (
#ef4444) with a dark red inner core (#7f1d1d). - Details: "Angry" white eyes rendered as small rectangles.
- Positioning: Drawn relative to the
cameraRefto ensure they scroll correctly with the level.
Sources: app/components/Game/GameCanvas.tsx:189-214, architecture_plan.md:46-47
Summary
Enemy AI in Kilo Man is a deterministic patrol system that uses range-based boundary checking. It is highly configurable via the levelData.ts file, allowing designers to set unique patrol paths and speeds for different sections of the game. The integration with the central GameCanvas loop ensures that AI updates are synchronized with physics and rendering, providing a consistent challenge to the player.