In the previous lecture, we started putting together this application with waypoints in it.
So we're going to now create a graph class that will allow us to apply the algorithm and get our little
tank moving automatically between these points and calculating its own path.
So in the assets, what I'll get you to do is to create a folder called graphs.
Now open that up inside here we want to have three C-sharp classes.
So C-sharp, let's create graph.
Then we want another one.
So we're going to create C sharp, which we will call Node and one more create C sharp, which is edge.
Now as you've seen previously in the course, a graph is made up of nodes which are basically our waypoints
and edges, which are the paths that go between those.
So these three classes are going to allow us to hook those things up as well as run and a star through
them.
So let's start by opening up the simplest of these, which is our edge.
So inside of the edge, what we're going to do in here is actually get rid of it being a mono behavior
because we don't need it having any special mono behavior features at all, which means it doesn't need
a update or a start up here.
We're going to have public node, start node.
Now we haven't defined Node, but we will come back and do that in a moment.
Public node and node.
So that's the path's starting node and ending node.
So the ones that you can actually travel in between, then we'll create a constructor.
So public edge where we will put a node from and a node two and then inside of here we will just set
start node equals from end and node equals to okay, that's pretty much it for your edge class, so
just save that.
Now open up your node class.
In the node class, we're going to again get rid of our monitor behavior and get rid of our leftover
unity methods that come with it.
Now, a node is like a waypoint.
It's a position in your map and it can have these edges coming out from it.
So it can have as many edges as it needs to have public list edge.
So make a list of all of these edges, which will be our edge list equals new list edge.
Then we want a public node path equals null.
So this will allow us to define or keep hold of a path that's going from our node game object I.D. Now
that's going to be the game object that is at our path location or sorry, our node location, I should
say the one that's representing where it is.
If you're using this graph in a different kind of situation, then obviously you wouldn't have a game
object associated with it.
You could have something else.
Now we want public float its x position.
Public float it's why position and you guessed it public float for the Z position to help us calculate
where they are, the distances between them and where our tank has to move to, when it's going to go
to one of these nodes.
Okay.
So we'll also for the aStar algorithm, have to put together a float for the F, g and H values of the
star algorithm that we'll use later on.
And we want a public node came from now.
This is going to store another node.
So if our character has moved to a particular node, then this is where they came from to get there.
Okay, so now we want a public node constructor, let's pass through the game object, AI, and then
inside of here we will set ID to equal AI and that's our game object x pause is going to equal AI dot
transform position x y pause will equal ai dot transform dot position dot y and z pose will equal AI
transform dot position dot z and then path initially will equal null.
This is something that's going to get constructed as we run through with the a star.
Then down here will create another public game object, get ai d and that will allow us to get our node
ID, which in this case is our game object so we can use it for comparing against other nodes later
on.
Okay, so that's the node.
So save that.
Now we can open up the graph, which is where a lot of the work is going to go on.
Okay.
So the graph will not be a mono behavior either and the initial methods that are in there aren't needed
either.
First, we're going to have each graph a list of edges.
So we want a list edge, let's call it edges equals new list edge, just like that.
Then we want a list.
Node nodes equals new list of nodes.
And then we want a list of nodes as well, which will be called a path list.
Now, this is the list that will get populated by the aStar algorithm, whereas the other two are just
the edges and nodes that exist all of the time.
So this will be a new list of our node class as well.
Okay.
Now we'll have a public graph constructor, which isn't really going to do very much at all.
Just like that.
Okay.
Now two methods will need to put edges and nodes in will be a public void add node and we'll pass through
the game object ID for now.
That'll do.
And then inside of here, we'll go.
Node equals node.
Node equals new node.
I.D..
And then nodes list dot add node.
Okay.
So that's adding our node.
Then down here we'll go public void, add edge and let me put a capital in there for consistency.
And inside of here, an edge gets created between two nodes.
So it'll be game object from node, game object to node.
And then node from node equals find node from.
Node.
So we're basically taking this from Node as a game object and we have to look through our nodes list
to get this node to return it as the node form.
So we still have to write, find node and let me put a capital F for there.
So that's how from though then then we'll go node two, which will equal our find node to node.
Okay.
We'll come back to this.
We're going to put in this find node method.
So node find node game object or ID and we'll loop through our list and look for it.
So for each node n in nodes.
If n dot get ID equals ID, then we will return n, otherwise we will return null.
So that's just looping through those, finding the particular node that we're after, using the ID of
the node, which is in fact the game object.
Then back up in the add edge.
We'll go if from does not equal null and two does not equal null.
So we're checking that these two nodes actually exist.
Then we can go.
Edge E equals new edge from two and then edges dot add that edge into our edge list and then we can
go from dot edge list dot add a so remember our edge list is what our node has.
So we're actually adding in the edge that's coming out of our from just down in there.
Okay.
So that's the starters of our graph.
Okay.
Now I've got one little issue that's going on down here because I haven't got a closing bracket for
that.
So let's just put that in there to get rid of it.
And now I can save that and we'll finish up with this lecture because we've done enough.
The next part of this will be putting in the a star algorithm to search through and find the paths from
one given node to another.