Welcome back to part two on this section on Star Pathfinding.
So what we're going to do now is actually create ourselves a maze and then build in the pathfinding
algorithm using a star.
You're going to need to start a file that comes with this lecture.
Now in it, you're going to find a scene called A Star Path, and there's some scripts that are already
in there which are going to build a maze for you from my maze building course.
If you're interested, the link is here on the screen for you.
Now, you don't really need to know a lot about the background of what's going on inside of the Maze
class, though you can have a look around because we're just basically going to be testing if one position
equals another position a little down the track.
So at this point, if we just press play, you'll see that the code generates a simple maze like this,
which we're going to navigate through now.
It's a random maze using a recursive algorithm.
So every time you press play, you will get a different map to work with.
Okay, so let's begin.
We want to create a new C-sharp file and we're going to call it Find Path a star.
Okay, let's open that up.
Now at the top, we're going to actually use the link library to do some sorting.
So just include system dot link.
And then let's create a new class.
And this class will basically be keeping track of all of the map positions.
So all of those nodes, the neighbors with the little markers that we'll put on them, we'll put them
in a new class.
Because essentially, I guess when you do this, when you're building your game environment, you're
not going to have markers and all that showing up.
You're just going to run it and it will create your path.
But we want to step through it so we can get a really good understanding of what's going on.
So let's make this actually public class path marker and it's going to have quite a few things in here.
So first of all, it's going to have a public map location, which will be its location in the maze,
and then a public float for G and a public.
Float for H and you guessed it a public float for F.
Right next, we're also going to have a public game object, which will be the physical marker itself.
So those coloured boxes that I've made and then we'll have a public path marker for parent.
Okay, let's go ahead now and make a constructor.
So public path marker and we'll pass through the map location l.
Float for G.
Float for H.
Float for F game.
Object for the marker.
Which are those rectangular prisms that I've got marking out the path as it goes around.
And also, we're going to have a path marker for PE to pass through the parent.
Then inside of here we just assign those values.
So location equals l, g equals g, h equals h.
F equals F.
This dot marker equals marker.
And finally, we're going to go parent equals P.
Right now.
We also are going to have to compare if one path marker is the same as another path marker, and therefore
we're going to need to create a override for the equals method.
So I'm just putting the end of my class in there, and here we'll have public override bool equals.
And of course, we're not going to use what it supplies us with, but rather I'm going to leave this
as a challenge for you to fill out what goes inside of this equals method as a clue.
Have a look inside of the Maze class because it also has an override equals method and you pretty much
want the same code as that's got.
Pause a video now.
When we come back, I'll go through the code for you.
Okay.
How did you go with that equals.
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
fairly straightforward.
Okay, so it's testing.
If the objects are empty or not the same object, and then it's just checking the X and the Z for our
map location.
Now I'm going to just cheat and grab this code and also that get hash because you'll get the same problem
back in the A Star.
Let's grab that and let's put it in here and then we can go about editing it.
Now, how do we say that the path marker is the same as another path marker?
Well, it's going to be at the same location and we've already got the equals written for the map location.
So down in here, instead of checking the X and the Z, although you could check the individual x's
or Z, you can just go location, which is your map location equals.
Path marker for the object.
Dot location like that.
So this is calling the equals that is in the map location class.
Okay.
And with our get hash code, we're done.
Now we can get on to creating our find path.
Asta And you know what, I'm going to leave that for the next video so we can put it together and then
try it out.