Game Development Reference
In-Depth Information
Listing 5.10 Adding an FSM Function
private var graph: Graph = new Graph();
...
private var state = "idle";
private var event = "enter";
private var goalLocation: GameObject;
function CreateBiPath(a: String, b: String)
{
...
}
function Start ()
{
...
}
function FSM()
{
if(state == "patrol")
{
if(event == "enter")
{
graph.AStar(currentNode,goalLocation);
currentWP = 0;
this.animation.Play("run");
this.animation["run"].wrapMode =
WrapMode.Loop;
event = "update";
}
else if (event == "update")
{
//if there is no path or we are at the
//end of the path don't do anything
if(graph.getPathLength() == 0 ||
currentWP == graph.getPathLength())
{
state = "idle";
event = "enter";
return;
}
//the node we are closest to at this moment
currentNode = graph.getPathPoint(currentWP);
//if we are close enough to the current
//waypoint start moving toward the next
if(Vector3.Distance(
graph.getPathPoint(currentWP).
transform.position,
transform.position) < accuracy)
Search Nedrilad ::




Custom Search