Game Development Reference
In-Depth Information
How it works...
The PathfinderThread handles pathfinding. To do this in a thread-safe way, we use
the pathfinding Boolean to let other threads know it's currently busy, so that they don't try
to read from the pathfinder.
Target is the position the pathfinder should try to reach. This is set externally and will be
used to indicate whether the thread should attempt to pathfind or not. This is why we set it
to null once pathfinding is successful.
We keep the thread running all the time, to avoid having to initialize it every time. The
thread will wake up once a second to see whether there is any pathfinding to perform. If the
delay was not there, it would use up resources, unnecessarily.
This class uses the waypointPosition field to store the current waypoint we're trying
to reach. This is so that we don't need to look it up in the pathfinder every time, and thus
risk interrupting an ongoing pathfinding. It also allows the AI to keep moving even if it's
currently contemplating a new path.
The controlUpdate method first checks whether the waypointPosition is null .
Null indicates it has no current goal, and should go to the pathfinder to see whether there is
a new waypoint for it.
It can only get a new waypoint if pathfinderThread currently is not actively
pathfinding and if there is a next waypoint to get.
If it already has a waypointPosition field, it will convert both the spatials position
and the waypointPosition to 2D and see how far apart they are. This is necessary as
we can't guarantee that NavMesh is exactly on the same plane as the spatial.
If it finds out that the distance is further than 1f , it will find out the direction to the way-
pointPosition field and tell the spatial to move in that direction. Otherwise (if it's
close enough), it will set the waypointPosition field to null .
Once it has reached the final waypoint, it will tell the spatial to stop.
Search WWH ::




Custom Search