1 00:00:00,860 --> 00:00:03,970 With the setter of the location property being private, 2 00:00:03,970 --> 00:00:07,810 the only way it can be set is from within the invader class. 3 00:00:07,810 --> 00:00:11,480 Right now, we don't have anything that sets the location property. 4 00:00:11,480 --> 00:00:15,490 The default value of an uninitialized map location is null. 5 00:00:15,490 --> 00:00:18,290 We don't want users of the invader class getting null when they ask for 6 00:00:18,290 --> 00:00:19,940 the invader's location. 7 00:00:19,940 --> 00:00:23,780 We need to initialize it to some starting value when the object's created. 8 00:00:23,780 --> 00:00:25,850 That's the purpose of the constructor. 9 00:00:25,850 --> 00:00:27,280 So let's add a constructor. 10 00:00:28,380 --> 00:00:30,310 So we'll type public Invader. 11 00:00:31,420 --> 00:00:33,970 The constructor will set the location property to the first 12 00:00:33,970 --> 00:00:35,150 location on the path. 13 00:00:35,150 --> 00:00:38,090 So the constructor needs to have the path passed in. 14 00:00:38,090 --> 00:00:42,131 We'll get the GetLocation method of the path object to get the first location on 15 00:00:42,131 --> 00:00:43,957 the path and assign it to location. 16 00:00:49,544 --> 00:00:53,145 So now, all invaders will start on the first step of the path. 17 00:00:53,145 --> 00:00:55,577 [SOUND] In the tree house defense game, 18 00:00:55,577 --> 00:01:00,071 invaders move down the path while towers attempt to neutralize them. 19 00:01:00,071 --> 00:01:04,805 If the invader successfully reaches the end of the path, then the game is over and 20 00:01:04,805 --> 00:01:06,417 the player will have lost. 21 00:01:06,417 --> 00:01:10,113 It would be the responsibility of each invader object to keep track of how far 22 00:01:10,113 --> 00:01:11,960 down the path they are. 23 00:01:11,960 --> 00:01:16,140 So, we'll need to add another field called _pathStep to keep track of this. 24 00:01:16,140 --> 00:01:18,570 No other classes will need to know this information. 25 00:01:18,570 --> 00:01:19,898 So we'll make it private. 26 00:01:22,329 --> 00:01:24,530 This field will change over time. 27 00:01:24,530 --> 00:01:27,100 So we don't want to make it read-only. 28 00:01:27,100 --> 00:01:30,150 We'll also give this field an initial value of 0. 29 00:01:30,150 --> 00:01:33,530 Because all invaders will start on step 0 of the path. 30 00:01:34,590 --> 00:01:38,280 In fact we can use this _pathStep field in our call to GetLocationAt, 31 00:01:38,280 --> 00:01:40,400 instead of using 0 twice. 32 00:01:40,400 --> 00:01:43,560 The reason we can do this is because the _pathStep field is 33 00:01:43,560 --> 00:01:46,610 set to 0 before the Invader constructor is called. 34 00:01:46,610 --> 00:01:48,040 The fields of a class are always 35 00:01:48,040 --> 00:01:50,890 initialized before the constructor is called. 36 00:01:50,890 --> 00:01:54,340 Of course, the default value of an integer is always 0, 37 00:01:54,340 --> 00:01:58,360 so initializing it to 0 here is redundant. 38 00:01:58,360 --> 00:02:01,200 But it's a good practice to initialize it to 0. 39 00:02:01,200 --> 00:02:04,690 By doing this we're saying that we've thought about what we want this initial 40 00:02:04,690 --> 00:02:06,805 value to be, and we want it to be 0. 41 00:02:08,360 --> 00:02:13,310 Now we'll create a method called move that'll advance the invader down the path. 42 00:02:13,310 --> 00:02:18,070 This will be called by code outside the invader class, so it needs to be public. 43 00:02:18,070 --> 00:02:21,230 We'll strictly use this method to tell the invader to move, so 44 00:02:21,230 --> 00:02:23,530 we don't need anything back from it. 45 00:02:23,530 --> 00:02:25,000 Let's give it a void return type. 46 00:02:27,590 --> 00:02:29,090 When the move method is called, 47 00:02:29,090 --> 00:02:33,040 it will advance the invader one step down the path. 48 00:02:33,040 --> 00:02:36,020 To do that, we just need to increase the _pathStep by 1. 49 00:02:37,260 --> 00:02:40,030 Now that we've changed what step the invader is on, 50 00:02:40,030 --> 00:02:42,210 we need to update its location. 51 00:02:42,210 --> 00:02:46,120 We can do this using path.GetLocationAt method. 52 00:02:46,120 --> 00:02:50,380 The only problem is, we don't have a path object here to refer to. 53 00:02:50,380 --> 00:02:54,490 It looks like we need to store an instance of the path object in the invader object 54 00:02:54,490 --> 00:02:56,000 so that it can be accessed later. 55 00:02:57,020 --> 00:03:00,200 To do this, we just need to create another field in the Invader class and 56 00:03:00,200 --> 00:03:02,570 assign it the path that was passed into the constructor. 57 00:03:03,610 --> 00:03:06,830 The path object is just for the Invader class to use. 58 00:03:06,830 --> 00:03:08,709 So we'll make it private readonly. 59 00:03:12,518 --> 00:03:17,310 Then in the constructor we'll set it to the path that was passed in, 60 00:03:17,310 --> 00:03:20,690 and I'll change this to use the field instead. 61 00:03:23,560 --> 00:03:27,410 Now we can go back to the move method and use the object's instance of the path 62 00:03:27,410 --> 00:03:30,531 to update the location property to the invader's new location. 63 00:03:31,650 --> 00:03:41,146 So we'll say location = _path.GetLocationAt(_pathStep). 64 00:03:43,730 --> 00:03:45,030 There we go. 65 00:03:45,030 --> 00:03:47,500 Finally, a note about terminology. 66 00:03:47,500 --> 00:03:52,300 We've now created classes that have fields, properties, methods, and 67 00:03:52,300 --> 00:03:53,830 constructors. 68 00:03:53,830 --> 00:03:56,630 Each of these things are called members of the class. 69 00:03:57,660 --> 00:04:02,470 We can refer to the path and pathStep fields, the location property and 70 00:04:02,470 --> 00:04:06,650 the Move method collectively as members of the Invader class. 71 00:04:07,710 --> 00:04:11,730 Members that are treated like variables in the class, such as fields and 72 00:04:11,730 --> 00:04:15,770 properties, are often referred to as member variables. 73 00:04:15,770 --> 00:04:19,450 In other courses we'll learn about other types of members that a class can have.