WEBVTT

00:06.810 --> 00:12.990
Welcome back to part two on this section on Star Pathfinding.

00:13.050 --> 00:20.430
So what we're going to do now is actually create ourselves a maze and then build in the pathfinding

00:20.430 --> 00:22.910
algorithm using a star.

00:22.920 --> 00:26.890
You're going to need to start a file that comes with this lecture.

00:26.910 --> 00:32.760
Now in it, you're going to find a scene called A Star Path, and there's some scripts that are already

00:32.760 --> 00:39.850
in there which are going to build a maze for you from my maze building course.

00:39.870 --> 00:44.310
If you're interested, the link is here on the screen for you.

00:44.430 --> 00:50.010
Now, you don't really need to know a lot about the background of what's going on inside of the Maze

00:50.010 --> 00:55.620
class, though you can have a look around because we're just basically going to be testing if one position

00:55.620 --> 00:58.470
equals another position a little down the track.

00:58.470 --> 01:07.080
So at this point, if we just press play, you'll see that the code generates a simple maze like this,

01:07.080 --> 01:09.150
which we're going to navigate through now.

01:09.150 --> 01:13.260
It's a random maze using a recursive algorithm.

01:13.260 --> 01:17.760
So every time you press play, you will get a different map to work with.

01:17.940 --> 01:20.070
Okay, so let's begin.

01:20.070 --> 01:29.940
We want to create a new C-sharp file and we're going to call it Find Path a star.

01:30.750 --> 01:33.750
Okay, let's open that up.

01:34.560 --> 01:39.960
Now at the top, we're going to actually use the link library to do some sorting.

01:39.960 --> 01:43.350
So just include system dot link.

01:44.830 --> 01:48.820
And then let's create a new class.

01:48.820 --> 01:54.460
And this class will basically be keeping track of all of the map positions.

01:54.610 --> 01:59.170
So all of those nodes, the neighbors with the little markers that we'll put on them, we'll put them

01:59.170 --> 02:00.210
in a new class.

02:00.220 --> 02:05.500
Because essentially, I guess when you do this, when you're building your game environment, you're

02:05.500 --> 02:07.930
not going to have markers and all that showing up.

02:07.930 --> 02:10.720
You're just going to run it and it will create your path.

02:10.720 --> 02:14.950
But we want to step through it so we can get a really good understanding of what's going on.

02:14.950 --> 02:23.620
So let's make this actually public class path marker and it's going to have quite a few things in here.

02:23.620 --> 02:31.330
So first of all, it's going to have a public map location, which will be its location in the maze,

02:31.450 --> 02:38.950
and then a public float for G and a public.

02:40.490 --> 02:48.500
Float for H and you guessed it a public float for F.

02:50.720 --> 02:56.420
Right next, we're also going to have a public game object, which will be the physical marker itself.

02:56.420 --> 03:06.200
So those coloured boxes that I've made and then we'll have a public path marker for parent.

03:07.040 --> 03:09.670
Okay, let's go ahead now and make a constructor.

03:09.680 --> 03:18.740
So public path marker and we'll pass through the map location l.

03:18.860 --> 03:20.410
Float for G.

03:20.420 --> 03:22.640
Float for H.

03:22.640 --> 03:25.430
Float for F game.

03:25.430 --> 03:27.290
Object for the marker.

03:27.500 --> 03:34.450
Which are those rectangular prisms that I've got marking out the path as it goes around.

03:34.460 --> 03:40.880
And also, we're going to have a path marker for PE to pass through the parent.

03:40.880 --> 03:43.520
Then inside of here we just assign those values.

03:43.520 --> 03:54.530
So location equals l, g equals g, h equals h.

03:55.370 --> 03:58.610
F equals F.

04:00.330 --> 04:04.380
This dot marker equals marker.

04:05.490 --> 04:11.850
And finally, we're going to go parent equals P.

04:13.350 --> 04:14.010
Right now.

04:14.010 --> 04:19.950
We also are going to have to compare if one path marker is the same as another path marker, and therefore

04:19.950 --> 04:25.290
we're going to need to create a override for the equals method.

04:25.470 --> 04:35.340
So I'm just putting the end of my class in there, and here we'll have public override bool equals.

04:35.460 --> 04:41.820
And of course, we're not going to use what it supplies us with, but rather I'm going to leave this

04:41.820 --> 04:47.340
as a challenge for you to fill out what goes inside of this equals method as a clue.

04:47.370 --> 04:55.260
Have a look inside of the Maze class because it also has an override equals method and you pretty much

04:55.260 --> 04:57.120
want the same code as that's got.

04:57.810 --> 04:58.980
Pause a video now.

04:59.070 --> 05:01.920
When we come back, I'll go through the code for you.

05:02.880 --> 05:03.380
Okay.

05:03.390 --> 05:05.800
How did you go with that equals.

05:05.820 --> 05:12.000
If we go to the maze code and have a look at the one that's in there here for our map location, it's

05:12.210 --> 05:13.410
fairly straightforward.

05:13.410 --> 05:14.550
Okay, so it's testing.

05:14.550 --> 05:19.470
If the objects are empty or not the same object, and then it's just checking the X and the Z for our

05:19.470 --> 05:20.220
map location.

05:20.220 --> 05:26.580
Now I'm going to just cheat and grab this code and also that get hash because you'll get the same problem

05:26.580 --> 05:28.170
back in the A Star.

05:28.200 --> 05:32.930
Let's grab that and let's put it in here and then we can go about editing it.

05:32.940 --> 05:37.740
Now, how do we say that the path marker is the same as another path marker?

05:37.740 --> 05:46.530
Well, it's going to be at the same location and we've already got the equals written for the map location.

05:46.530 --> 05:52.290
So down in here, instead of checking the X and the Z, although you could check the individual x's

05:52.290 --> 05:58.170
or Z, you can just go location, which is your map location equals.

06:00.160 --> 06:04.480
Path marker for the object.

06:06.070 --> 06:09.220
Dot location like that.

06:09.220 --> 06:14.800
So this is calling the equals that is in the map location class.

06:15.310 --> 06:15.680
Okay.

06:15.730 --> 06:18.550
And with our get hash code, we're done.

06:18.550 --> 06:22.870
Now we can get on to creating our find path.

06:22.900 --> 06:28.750
Asta And you know what, I'm going to leave that for the next video so we can put it together and then

06:28.750 --> 06:29.980
try it out.
