The game will end if any of the following conditions ever become true
- All the monsters are dead
- The player is dead
An actor is dead when it’s life has been reduced to zero or less. In order to implement the above I had to give some thought to the game loop and how the player and monsters would fit into the game. I came up with the following unit test to describe the behaviour I was looking for.
10 [Test]
11 public void TestGameEndsWhenAllMonstersAreDead()
12 {
13 // Create a new game instance
14 var game = new Game();
15 // Initialize the game with a player actor and a few dead monsters
16 game.Initialize(new Actor {HitPoints = 10},
17 new List<IActor>
18 {
19 new Actor {HitPoints = 0},
20 new Actor {HitPoints = 0}
21 });
22
23 // Process a game turn
24 game.ProcessTurn();
25 // Assert that the game has ended
26 Assert.IsFalse(game.IsActive);
27 }
In the above test I create a new instance of my game and initialize it with a live player and some dead monsters. I foresee this initialization step to happen when we start our game, or load an existing game or maybe when changing levels. For now I’m keeping it simple.
I then foresee the game loop to keep processing turns while the game is active. Of course this will include things like rendering and reacting to player input, but I’m not worried about this right now.
The ProcessTurn method is responsible for checking if the monsters are all dead and then setting the IsActive flag to false.
The following test verifies that the game ends when the player dies.
29 [Test]
30 public void TestGameEndsWhenThePlayerIsDead()
31 {
32 // Create a new game instance
33 var game = new Game();
34 // Initialize the game with a dead player actor and a live monster
35 game.Initialize(new Actor {HitPoints = 0},
36 new List<IActor>
37 {
38 new Actor {HitPoints = 5}
39 });
40 // Process a game turn
41 game.ProcessTurn();
42 // Assert that the game has ended
43 Assert.IsFalse(game.IsActive);
44 }
This test is exactly the same as before except that the player is dead and the monster alive. No real rocket science. I added the following test to make sure that the game is in fact active when both the player and at least one monster is alive.
46 [Test]
47 public void TestGameIsActiveWhenThePlayerIsAliveAndAMonsterIsAlive()
48 {
49 // Create a new game instance
50 var game = new Game();
51 // Initialize the game with a live player actor, a live monster,
52 // and a dead monster
53 game.Initialize(new Actor {HitPoints = 10},
54 new List<IActor>
55 {
56 new Actor {HitPoints = 5},
57 new Actor {HitPoints = 0}
58 });
59 // Process a game turn
60 game.ProcessTurn();
61 // Assert that the game has ended
62 Assert.IsTrue(game.IsActive);
63 }
This test initializes the game with a live player and monster and a dead monster (just to mix things up a bit).
I feel the above tests have given me enough functionality to satisfy my end game requirements. I am going to have to do some work on the ProcessTurn method to implement actors taking turns and checking that dead actors don’t get a chance to act. So far I’m happy with the design (I haven’t had to make any major changes to implement a feature yet). I do have a lot of thoughts on new features I’d like to implement, but I’m trying to be really disciplined on keeping to my original scope.