Skip to content

Lesson 09 — Add threats and recovery ​

Where this lesson fits: claiming works, but there is no moving threat. Add enemy movement and make collision consequences fair, explicit, and recoverable.

Time: 4–6 hours

Prerequisites and outcomes ​

Complete lessons 05–08. By the end, you can distinguish enemy occupancy, player collision, trail collision, shield consumption, death, respawn, and grace.

Prepare and run ​

  1. From your learner project's solution folder, follow tutorial chapter 07 steps 1–9.
  2. Open Source\Systems\EnemySystem.cs, CollisionSystem.cs, and the corresponding tests. Run the focused commands below.
  3. Run dotnet run --project Platforms\DesktopVK and follow the tutorial's checkpoint for wall safety, trail contact, death, and respawn.
  4. Stage note: shields and powerups arrive in tutorial chapter 11 / LP3 lesson 13; enemy variants first appear in play in tutorial chapter 10. Inspect their code now but verify them when those stages are reached.

Code anchors ​

  • last-perimeter/Source/Systems/EnemySystem.cs
  • last-perimeter/Source/Systems/CollisionSystem.cs
  • last-perimeter/Source/Entities/Enemy.cs
  • last-perimeter/Source/Game/LastPerimeterApp.cs (UpdateEnemies, HandlePlayerDeath)
  • last-perimeter/Tests/Systems/EnemySystemTests.cs
  • last-perimeter/Tests/Systems/CollisionSystemTests.cs

Guided operations ​

  1. Trace enemy retargeting and movement. Find the pressure target and confirm it is supplied only while a trail is active.
  2. Follow the occupancy probes and axis reflection. Verify enemies stay in unclaimed space.
  3. Test player-enemy and enemy-trail predicates separately. Notice that the player is safe on the wall under the current collision rules.
  4. Trace a death event to life loss, then to the selected safe wall-path node and respawn grace timer. Revisit shield consumption in lesson 13.
  5. Inspect enemy variants and temporary splitter clones as parameterized behavior/lifetime, not unrelated duplicated update loops.
  6. Run the enemy and collision suites. For an orchestration change, add an app-level test or a reproducible manual scenario.
powershell
dotnet test --filter FullyQualifiedName~EnemySystemTests
dotnet test --filter FullyQualifiedName~CollisionSystemTests

Checkpoint ​

An enemy can threaten an exposed trail, but does not escape into claimed ground. A hit follows the life/respawn path and the player cannot immediately lose the newly respawned life during grace. Check shields in lesson 13.

Common mistakes ​

  • Treating wall contact and trail contact as the same collision.
  • Removing an enemy before its index in a claim result is safely applied.
  • Allowing an enemy that is frozen to continue timers or split behavior unintentionally.

Questions and extension ​

Which fairness rules are enforced by geometry, which by timing, and which by feedback? Sparx are not part of the current acceptance scope; treat them as a separate extension.