1 00:00:00,380 --> 00:00:03,030 There's one more thing we need to add to the invader class. 2 00:00:03,030 --> 00:00:04,920 Invaders can't be invincible. 3 00:00:04,920 --> 00:00:09,052 As towers shoot and hit them, they need to be disabled somehow. 4 00:00:09,052 --> 00:00:12,030 Often times, opponents in games have a certain amount of health 5 00:00:12,030 --> 00:00:15,030 that decreases as the opponent sustains damage. 6 00:00:15,030 --> 00:00:17,560 We can implement this same idea in the invader class. 7 00:00:19,100 --> 00:00:21,950 You know everything you need to know in order to add this feature to 8 00:00:21,950 --> 00:00:23,620 the invader class. 9 00:00:23,620 --> 00:00:27,650 Think about what we'll need to add to this class in order to implement this. 10 00:00:27,650 --> 00:00:30,570 You'll probably need some sort of variable to store 11 00:00:30,570 --> 00:00:33,110 how much health the invader has remaining. 12 00:00:33,110 --> 00:00:37,200 Invaders also need to start out with a certain level of health. 13 00:00:37,200 --> 00:00:41,290 We'll need to provide some way for other classes, such as the tower class 14 00:00:41,290 --> 00:00:44,800 to decrease the health of the invader if they successfully hit it. 15 00:00:44,800 --> 00:00:48,660 I suggest you pause the video and take a moment to think about this and 16 00:00:48,660 --> 00:00:50,610 even code up your own solution. 17 00:00:50,610 --> 00:00:52,650 When you come back, I'll show you how I would do it. 18 00:00:56,450 --> 00:00:59,830 All right, here in our code let's work through a solution. 19 00:00:59,830 --> 00:01:00,350 First. 20 00:01:00,350 --> 00:01:02,240 I think we should add a member to the class for 21 00:01:02,240 --> 00:01:04,260 storing how much health the invader has remaining. 22 00:01:05,380 --> 00:01:08,650 We need to decide if this should be a property or a field and 23 00:01:08,650 --> 00:01:11,850 what sort of accessibility it should have. 24 00:01:11,850 --> 00:01:15,370 Maybe we should answer the question of accessibility first. 25 00:01:15,370 --> 00:01:16,210 Is it reasonable for 26 00:01:16,210 --> 00:01:20,080 other classes to be able to see how much health the invader has remaining? 27 00:01:20,080 --> 00:01:21,270 I think so. 28 00:01:21,270 --> 00:01:24,160 So, that means we should make it public. 29 00:01:24,160 --> 00:01:28,970 Making it public answers our question of whether to make it a property or a field. 30 00:01:28,970 --> 00:01:32,540 I mentioned earlier that for a number of reasons it's best to make public 31 00:01:32,540 --> 00:01:36,610 member variables properties, so we'll make this a property. 32 00:01:36,610 --> 00:01:38,290 I'll call it Health. 33 00:01:38,290 --> 00:01:41,950 Now, we need to decide what sort of accessibility the getter and 34 00:01:41,950 --> 00:01:43,370 the setter of the property should have. 35 00:01:43,370 --> 00:01:47,940 I already mentioned the other classes will want to see what the health of 36 00:01:47,940 --> 00:01:49,240 the invader is. 37 00:01:49,240 --> 00:01:51,930 So, we should make the getter public. 38 00:01:51,930 --> 00:01:55,430 Since getters and setters of a property are public by default, 39 00:01:55,430 --> 00:01:57,070 we don't need to type public here. 40 00:01:58,510 --> 00:01:59,910 What about the setter? 41 00:01:59,910 --> 00:02:03,570 Should other classes be able to change the health of the invader? 42 00:02:03,570 --> 00:02:06,990 Well, that's sort of the point of adding this in the first place, so 43 00:02:06,990 --> 00:02:09,270 let's keep it public as well. 44 00:02:09,270 --> 00:02:13,840 Now we need to give the health property a starting value, otherwise it will 45 00:02:13,840 --> 00:02:17,780 have a default value of zero, which would make for a very short lived invader. 46 00:02:18,920 --> 00:02:21,530 Now there are a couple of ways we could give it an initial value. 47 00:02:21,530 --> 00:02:25,400 We could assign it a value here in the constructor like so and 48 00:02:25,400 --> 00:02:26,760 give it a starting value of two. 49 00:02:28,380 --> 00:02:32,410 Or we can just assign it a value where we declared it. 50 00:02:32,410 --> 00:02:36,480 In a case like this where the value is a literal number that isn't passed into 51 00:02:36,480 --> 00:02:40,250 the constructor, I prefer to assign it here at the property. 52 00:02:41,880 --> 00:02:44,840 Now we could actually improve this interface a little more, and 53 00:02:44,840 --> 00:02:47,170 encapsulate the health concept even more. 54 00:02:48,280 --> 00:02:50,860 Let's provide a method called decrease health. 55 00:02:50,860 --> 00:02:54,130 That decreases the health by a passed in factor. 56 00:02:54,130 --> 00:02:57,700 School type public, void, decrease health. 57 00:02:57,700 --> 00:03:03,690 And then the parameter is the factor by which to decrease the health. 58 00:03:03,690 --> 00:03:04,566 So make that an integer. 59 00:03:08,626 --> 00:03:14,300 And we'll just say health equals factor. 60 00:03:14,300 --> 00:03:16,890 Now it's obvious to all users of this class 61 00:03:16,890 --> 00:03:18,740 how to decrease the health of an invader. 62 00:03:19,850 --> 00:03:23,730 Instead of a coder having to assume that they should alter the health property 63 00:03:23,730 --> 00:03:26,500 directly, there's a method called DecreaseHealth. 64 00:03:27,520 --> 00:03:30,880 This is an example of self documenting code. 65 00:03:30,880 --> 00:03:35,860 Self documenting code is code that's so clear we don't have to write many comments 66 00:03:35,860 --> 00:03:39,000 or other documentation that describes how to use it. 67 00:03:39,000 --> 00:03:42,270 This is usually accomplished by a well thought out design, 68 00:03:42,270 --> 00:03:46,170 good encapsulation practices, and giving things clear descriptive names. 69 00:03:47,400 --> 00:03:50,030 Now that we're providing a method to change the health, 70 00:03:50,030 --> 00:03:53,180 we should make the health property's setter private 71 00:03:53,180 --> 00:03:56,090 to make it clear to other coders that they should use the method.