1 00:00:00,490 --> 00:00:02,890 Now that we have all of the classes implemented for 2 00:00:02,890 --> 00:00:06,020 each of the types of objects we have in Treehouse Defense, 3 00:00:06,020 --> 00:00:09,670 we can use these classes together to finish the game. 4 00:00:09,670 --> 00:00:14,350 We have a really nice set of classes, but nothing ties them all together yet. 5 00:00:14,350 --> 00:00:19,260 We haven't yet coded up how all these objects work together to create a game. 6 00:00:19,260 --> 00:00:23,340 So now we need to write the code for how the game runs. 7 00:00:23,340 --> 00:00:26,570 Instead of putting the code that runs the game in the game class, 8 00:00:26,570 --> 00:00:29,272 let's introduce the idea of a level. 9 00:00:29,272 --> 00:00:33,340 In Treehouse Defense, a level is composed of a map, a path, and 10 00:00:33,340 --> 00:00:35,140 a number of invaders. 11 00:00:35,140 --> 00:00:39,270 Then we can create different levels with unique maps, paths, and 12 00:00:39,270 --> 00:00:40,220 collection of invaders. 13 00:00:41,400 --> 00:00:45,590 What we do in this video is mostly review of many of the things we've learned so 14 00:00:45,590 --> 00:00:47,050 far about C Sharp. 15 00:00:47,050 --> 00:00:48,433 So we're going to move through it quickly. 16 00:00:50,073 --> 00:00:53,974 Let's start by creating a new file called Level.cs. 17 00:00:56,735 --> 00:01:00,675 In the Treehouse Defense namespace we'll create a class named Level. 18 00:01:05,317 --> 00:01:08,995 The level constructor only needs the array of invaders. 19 00:01:08,995 --> 00:01:13,055 Remember each invader already has an instance of the path object. 20 00:01:13,055 --> 00:01:16,675 So we don't need to set the path as a field in the level class. 21 00:01:24,075 --> 00:01:27,160 Towers aren't actually required in order to construct a level. 22 00:01:27,160 --> 00:01:31,040 But, eventually the level does need to know about them. 23 00:01:31,040 --> 00:01:34,860 Let's add a tower's property with the public getter and a setter. 24 00:01:34,860 --> 00:01:37,965 So the players can set this array after the level is created. 25 00:01:41,605 --> 00:01:44,205 Now let's make a method called play. 26 00:01:44,205 --> 00:01:49,980 It will return true if the player wins the level and false if they lose. 27 00:01:49,980 --> 00:01:53,080 Let's use comments to scaffold out how the game will work. 28 00:01:54,350 --> 00:02:01,884 The level will run until all invaders are neutralized or 29 00:02:01,884 --> 00:02:08,019 an invader reaches the end of the path. 30 00:02:08,019 --> 00:02:11,411 Got an extra the there. 31 00:02:11,411 --> 00:02:19,590 First, Each tower has an opportunity to fire on invaders. 32 00:02:22,200 --> 00:02:28,280 Then we'll need to count and move the invaders that are still active. 33 00:02:30,800 --> 00:02:33,800 When we see the words Run until that means we'll need a loop. 34 00:02:34,840 --> 00:02:38,185 Let's make a variable to keep track of how many invaders there are remaining. 35 00:02:44,805 --> 00:02:48,385 The loop will run until all the invaders have been neutralized. 36 00:02:56,685 --> 00:03:00,585 The things described by these two comments will happen inside the loop. 37 00:03:00,585 --> 00:03:06,425 So now we'll loop through each of the towers and let them fire on the invaders. 38 00:03:23,425 --> 00:03:25,885 Now we'll count the number of invaders again. 39 00:03:25,885 --> 00:03:30,386 We'll reset remaining invaders to zero and then loop through the invaders. 40 00:03:42,166 --> 00:03:46,146 We'll increment remaining invaders for each active invader in the array. 41 00:03:55,822 --> 00:03:59,762 If the invader is active, then we should also move it down the path. 42 00:04:02,526 --> 00:04:03,839 After the invader has moved, 43 00:04:03,839 --> 00:04:07,350 we should check to see if it's reached the end of the path. 44 00:04:07,350 --> 00:04:12,210 If it has, we'll return false signifying that the user lost. 45 00:04:12,210 --> 00:04:17,090 So we'll check invader.hasscored. 46 00:04:17,090 --> 00:04:20,260 Finally if all invaders are neutralized then the loop will 47 00:04:20,260 --> 00:04:23,650 exit because remaining invaders will be zero. 48 00:04:23,650 --> 00:04:26,810 Then we'll return true signifying that the user won. 49 00:04:28,130 --> 00:04:30,250 So this is the end of the loop here. 50 00:04:30,250 --> 00:04:32,120 So here we return true. 51 00:04:34,650 --> 00:04:35,950 As we look at this method, 52 00:04:35,950 --> 00:04:40,710 it isn't entirely obvious on the surface, what returning true or false means. 53 00:04:41,810 --> 00:04:46,350 We can provide that information in a comment here at the top of the method. 54 00:04:46,350 --> 00:04:51,204 So I'll say Returns: true if 55 00:04:51,204 --> 00:04:57,240 the player wins, false otherwise. 56 00:04:57,240 --> 00:04:59,560 We just wrote a ton of code. 57 00:04:59,560 --> 00:05:02,430 Be sure to read through this code carefully to make sure that you understand 58 00:05:02,430 --> 00:05:02,940 what it's doing. 59 00:05:02,940 --> 00:05:06,560 You can always watch this video again if that helps. 60 00:05:06,560 --> 00:05:10,557 Finally, let's compile to make sure that we haven't introduced any compiler errors.