1 00:00:00,080 --> 00:00:04,500 [MUSIC] 2 00:00:04,500 --> 00:00:09,290 Now that we finished up the Invader class, let's move on to the Tower class. 3 00:00:09,290 --> 00:00:12,470 So far the Tower class is completely empty, let's fill it out. 4 00:00:13,510 --> 00:00:16,810 First each Tower will need to have a location on the map, so 5 00:00:16,810 --> 00:00:18,600 let's add a field for that. 6 00:00:18,600 --> 00:00:21,990 Nothing outside the class needs to know the location of the Towers, 7 00:00:21,990 --> 00:00:25,310 so we won't create a MapLocation property. 8 00:00:25,310 --> 00:00:30,370 This will just be a field, because each tower needs to have a location on the map, 9 00:00:30,370 --> 00:00:32,840 it should be a parameter in the constructor. 10 00:00:32,840 --> 00:00:35,331 And we'll need to use it to set the location field. 11 00:00:43,035 --> 00:00:48,030 Now most tower defense games don't allow placing towers on the path. 12 00:00:48,030 --> 00:00:51,270 We could add some validation here to ensure the towers can't be 13 00:00:51,270 --> 00:00:51,870 placed on paths. 14 00:00:53,000 --> 00:00:54,600 In fact, you know what? 15 00:00:54,600 --> 00:00:57,760 You already saw an example of this validation pattern when we created 16 00:00:57,760 --> 00:00:59,640 the MapLocation class. 17 00:00:59,640 --> 00:01:01,750 I'll leave that as a challenge for you to do on your own. 18 00:01:03,590 --> 00:01:08,320 Now we need to think about what other behaviors and attribute Towers will need. 19 00:01:08,320 --> 00:01:11,759 One behavior I can think of is the ability to shoot at invaders. 20 00:01:13,170 --> 00:01:16,570 Let's create a method called FireOnInvaders. 21 00:01:16,570 --> 00:01:19,930 This Method will go through all of the Invaders on the map and 22 00:01:19,930 --> 00:01:21,990 check to see if they are in range. 23 00:01:21,990 --> 00:01:25,780 If they are, the Tower will shoot at them and decrease their health. 24 00:01:26,900 --> 00:01:31,060 This method will need to have access to all the Invaders on the map. 25 00:01:31,060 --> 00:01:33,850 So we'll pass them in as an array. 26 00:01:33,850 --> 00:01:37,560 Now we'll need to determine which invaders are in range. 27 00:01:37,560 --> 00:01:41,670 To do that we'll need to look at each invader in the array. 28 00:01:41,670 --> 00:01:42,830 For that we'll need a loop. 29 00:01:44,030 --> 00:01:47,140 Let's look at how we would use a while loop for this. 30 00:01:47,140 --> 00:01:50,500 If you're unfamiliar with while loops check out the teacher's notes for 31 00:01:50,500 --> 00:01:52,440 a link to a Treehouse course that covers them. 32 00:01:54,200 --> 00:01:57,000 We'll need to loop through each index in the array. 33 00:01:57,000 --> 00:02:01,110 So we'll need a variable to keep track of where we are in the array. 34 00:02:01,110 --> 00:02:06,180 Let's create an integer variable called index here and initialize it to zero. 35 00:02:07,220 --> 00:02:10,870 Now let's create a while loop that will stop when it gets to the end of the array. 36 00:02:11,980 --> 00:02:16,787 To do that, we'll check if the index is less than the length of the array. 37 00:02:19,550 --> 00:02:22,643 The last index in an array is always one less than the length of 38 00:02:22,643 --> 00:02:25,840 the array because we start counting at zero. 39 00:02:25,840 --> 00:02:28,270 If index equals the length of the array, 40 00:02:28,270 --> 00:02:30,030 then we know that it's time to exit the loop. 41 00:02:31,430 --> 00:02:35,450 Now we can use the index variable to index into the array. 42 00:02:35,450 --> 00:02:38,530 We'll assign it to a variable so we don't have to index into the array 43 00:02:38,530 --> 00:02:41,140 every time we want to reference this specific Invader. 44 00:02:42,870 --> 00:02:45,130 Now that we have an Invader from the array, 45 00:02:45,130 --> 00:02:47,570 we can do whatever we want with it here. 46 00:02:47,570 --> 00:02:50,430 Finally we'll need to add one to the index variable so 47 00:02:50,430 --> 00:02:53,720 that the next time through the loop we're getting the next invader in the array. 48 00:02:54,760 --> 00:02:57,950 This way, we can do something with each of the items in the array. 49 00:02:59,370 --> 00:03:02,440 This is called incrementing the index variable by one. 50 00:03:03,470 --> 00:03:07,380 Incrementing is so common that there is some syntactic sugar for it. 51 00:03:07,380 --> 00:03:13,560 Instead of saying index plus equals one, we can just type index++. 52 00:03:13,560 --> 00:03:17,280 This is shorthand for adding one to the index variable. 53 00:03:17,280 --> 00:03:21,660 Sometimes you may see the ++ at the start of the variable name. 54 00:03:21,660 --> 00:03:24,630 There's a very subtle difference between these two forms. 55 00:03:24,630 --> 00:03:28,080 I've included a description of these differences in the teacher's notes. 56 00:03:28,080 --> 00:03:32,670 In most case, you want to put the ++ on the right side of the variable. 57 00:03:32,670 --> 00:03:36,500 Our while loop as it's written will loop through each item in the array 58 00:03:36,500 --> 00:03:37,130 one at a time. 59 00:03:38,230 --> 00:03:41,960 This allows us to do something with each item by adding some code right here where 60 00:03:41,960 --> 00:03:43,800 I've typed this comment. 61 00:03:43,800 --> 00:03:48,300 This works perfectly, but I want to show you another way to write this 62 00:03:48,300 --> 00:03:51,620 that's both easier to write and to understand. 63 00:03:51,620 --> 00:03:54,590 Let's take a short break and then we'll learn about for loops.