//I have made this it's own function so that I can extend the class and make slightly different versions //Picks the direction that leads to being the closest to the player protected List<Point> MakeDesision (MapNode node, Vector3 trackPosition) { List<Point> nextSet; float northDistance = Vector3.Distance (node.NorthNode.transform.position, trackPosition); float eastDistance = Vector3.Distance (node.EastNode.transform.position, trackPosition); float southDistance = Vector3.Distance (node.SouthNode.transform.position, trackPosition); float westDistance = Vector3.Distance (node.WestNode.transform.position, trackPosition); float[] distances = new float[]{ northDistance, eastDistance, southDistance, westDistance }; float minDistance = Mathf.Min (distances); if (minDistance == northDistance) { nextSet = node.North; } else if (minDistance == eastDistance) { nextSet = node.East; } else if (minDistance == southDistance) { nextSet = node.South; } else { nextSet = node.West; } //prevents backtracking or non valid direction //defaults to random in that case if (nextSet.Count <= 0 || (lastNode != null && node.GetNodeFor(nextSet) == lastNode)) { return base.MakeDesision (node); } return nextSet; }
/// <summary> /// Given a map node Decides what direction to go in /// </summary> /// <returns>The next set of directions.</returns> /// <param name="node">Node.</param> protected virtual List<Point> MakeDesision (MapNode node) { List<Point> nextSet; int random = Random.Range (1, 5); switch (random) { case 1: nextSet = node.North; break; case 2: nextSet = node.East; break; case 3: nextSet = node.South; break; case 4: nextSet = node.West; break; default: return MakeDesision (node); } //Make sure we're not choosing a path that doesn't exist if (nextSet.Count <= 0 || (lastNode != null && node.GetNodeFor(nextSet) == lastNode)) { return MakeDesision (node); } return nextSet; }
protected override List<Point> MakeDesision (MapNode node) { List<Point> nextSet; float yDirection = Player.player.transform.position.y - transform.position.y; float xDirection = Player.player.transform.position.x - transform.position.x; if (Mathf.Abs (xDirection) > Mathf.Abs (yDirection)) { if (xDirection > 0) { nextSet = node.East; } else { nextSet = node.West; } } else { if (yDirection > 0) { nextSet = node.North; } else { nextSet = node.South; } } //prevents backtracking or non valid direction //defaults to random in that case if (nextSet.Count <= 0 || (lastNode != null && node.GetNodeFor(nextSet) == lastNode)) { return base.MakeDesision (node); } return nextSet; }