using Microsoft.Xna.Framework; using System.Collections.Generic; using System.Linq; namespace Subsurface { class PathNode { private WayPoint wayPoint; private int wayPointID; public int state; public PathNode Parent; private Vector2 position; public float F,G,H; public List connections; public float[] distances; public WayPoint Waypoint { get { return wayPoint; } } public Vector2 Position { get {return position;} } public PathNode(WayPoint wayPoint) { this.wayPoint = wayPoint; this.position = wayPoint.SimPosition; wayPointID = wayPoint.ID; connections = new List(); } public static List GenerateNodes(List wayPoints) { var nodes = new Dictionary(); foreach (WayPoint wayPoint in wayPoints) { nodes.Add(wayPoint.ID, new PathNode(wayPoint)); } foreach (KeyValuePair node in nodes) { foreach (MapEntity linked in node.Value.wayPoint.linkedTo) { PathNode connectedNode = null; nodes.TryGetValue(linked.ID, out connectedNode); if (connectedNode == null) continue; node.Value.connections.Add(connectedNode); } } var nodeList = nodes.Values.ToList(); foreach (PathNode node in nodeList) { node.distances = new float[node.connections.Count]; for (int i = 0; i< node.distances.Length; i++) { node.distances[i] = Vector2.Distance(node.position, node.connections[i].position); } } return nodeList; } } class PathFinder { List nodes; private bool insideSubmarine; public PathFinder(List wayPoints, bool insideSubmarine = false) { nodes = PathNode.GenerateNodes(wayPoints.FindAll(w => w.MoveWithLevel != insideSubmarine)); this.insideSubmarine = insideSubmarine; } public SteeringPath FindPath(Vector2 start, Vector2 end) { float closestDist = 0.0f; PathNode startNode = null; foreach (PathNode node in nodes) { float dist = Vector2.Distance(start,node.Position); if (dist finalPath = new List(); PathNode pathNode = end; while (pathNode != start && pathNode != null) { finalPath.Add(pathNode.Waypoint); pathNode = pathNode.Parent; } finalPath.Reverse(); foreach (WayPoint wayPoint in finalPath) { path.AddNode(wayPoint); } return path; } } }