Skip to main content

Enemy Spawn Example

This example demonstrates the spawn system with timed enemy spawning, object pooling, and movement behaviors using the helios game loop architecture with typed passes and state management.

Features

  • GameLoop Architecture - Phase-based game loop with Pre/Main/Post phases, typed passes, and commit points
  • SpawnSystemFactory DSL - Declarative spawn configuration with fluent builder API
  • Object Pooling - Efficient enemy management via GameObjectPoolManager with PrefabId
  • Random Placement - Enemies spawn at random positions within level bounds
  • MoveInitializer - Random initial movement direction for spawned entities
  • SpinComponent - Visual rotation effect on enemies
  • Level Bounds - Enemies reflect off arena boundaries
  • GameObjectFactory - Fluent builder pattern for creating GameObjects
  • State Management - Game state tracking via Session
  • Automated Rendering - SceneRenderingSystem handles viewport-based rendering

Building

cmake -S . -B build
cmake --build build --target enemy_spawnmain

Running

./build/examples/enemy_spawn/main

Controls

InputAction
Left StickMove spaceship / Rotate
ESCExit application
~ (Tilde)Toggle ImGui overlay

Code Structure

FilePurpose
main.cppApplication entry point with spawn configuration

Components Used

ComponentPurpose
SceneNodeComponentLinks GameObject to scene graph
Move2DComponent2D physics with velocity and acceleration
SpinComponentContinuous rotation animation
ComposeTransformComponentLocal/world transform composition
ScaleStateComponentUnit-based sizing (meters)
LevelBoundsBehaviorComponentReflect behavior at arena boundaries
AabbColliderComponentWorld-space collision bounds
SpawnedByProfileComponentLinks entity to spawn profile
PrefabIdComponentIdentifies prefab type for pooling
Active / InactiveEntity activation state tags

Systems Used

SystemPurpose
GameObjectSpawnSystemProcesses spawn schedulers and triggers spawning
SpinSystemApplies rotation from SpinComponent
Move2DSystemPhysics simulation (velocity integration)
SteeringSystemDirection-based rotation
ScaleSystemApplies ScaleStateComponent sizing
BoundsUpdateSystemUpdates AABB colliders from transforms
LevelBoundsBehaviorSystemHandles boundary reflections
ComposeTransformSystemComputes final transforms
StateToViewportPolicyUpdateSystemActivates viewports based on game state
SceneSyncSystemSyncs transforms to scene graph
SceneRenderingSystemRenders scenes to associated viewports
TransformClearSystemClears dirty flags post-frame

Spawn System Configuration (DSL)

SpawnSystemFactory::configure(poolManager, spawnManager)
.pool(EnemyPoolId, EnemyPrefabId, 200)
.profile(RandomSpawnProfileId)
.randomPlacement()
.randomDirectionInitializer()
.scheduledBy(SpawnRuleId)
.timerCondition(5.0f)
.fixedAmount(1)
.done()
.done()
.commit();

GameLoop Phase Configuration

// Pre-Phase: Input, Spawning, Physics
gameLoop.phase(PhaseType::Pre)
.addPass<GameState>(GameState::Any)
.addSystem<TwinStickInputSystem>(shipGameObject)
.addCommitPoint(CommitPoint::Structural)
.addPass<GameState>(GameState::Any)
.addSystem<GameObjectSpawnSystem>(spawnManager)
.addCommitPoint(CommitPoint::Structural)
.addPass<GameState>(GameState::Any)
.addSystem<ScaleSystem>()
.addSystem<SteeringSystem>()
.addSystem<SpinSystem>()
.addSystem<Move2DSystem>();

// Main-Phase: Collision Detection
gameLoop.phase(PhaseType::Main)
.addPass<GameState>(GameState::Any)
.addSystem<BoundsUpdateSystem>()
.addSystem<LevelBoundsBehaviorSystem>()
.addCommitPoint();

// Post-Phase: Transform and Rendering
gameLoop.phase(PhaseType::Post)
.addPass<GameState>(GameState::Any)
.addSystem<ComposeTransformSystem>()
.addSystem<StateToViewportPolicyUpdateSystem<GameState, MatchState>>(stateToViewportMap)
.addSystem<SceneSyncSystem>(sceneToViewportMap)
.addSystem<SceneRenderingSystem>(renderingDevice, sceneToViewportMap)
.addSystem<TransformClearSystem>();

See Also