//Methods public void PlanRouteToNode(Node node) { if(!currentRouteNodes.Contains(node)) { this.newTargetNode = node; } }
////////////////////////////// //Constructors// ////////////////////////////// public GraphMovingEntity(EntityManager.EntityType entityType, Texture2D startTexture, Node startNode) : base(entityType, startTexture) { this.positionNode = startNode; this.Position = positionNode.Position; this.elapsedTimeMoving = INITIAL_ELAPSED_TIME; }
//Constructors public AStarMovementEntity(EntityManager.EntityType entityType, Texture2D startTexture, Node startNode) : base(entityType, startTexture, startNode) { this.currentRouteEdges = new LinkedList<Edge>(); this.currentRouteNodes = new LinkedList<Node>(); this.newTargetNode = null; }
//Constructors public Edge(Graph graph, Node node1, Node node2, int cost) { graph.AllEdges.AddLast(this); this.node1 = node1; this.node2 = node2; node1.LinkToEdge(this); node2.LinkToEdge(this); this.cost = cost; costTexture = DetermineCostTexture(); }
//Methods public static Graph CreateGraph() { Graph graph = new Graph(); Node node0 = new Node(graph, new Vector2(400, 100)); Node node1 = new Node(graph, new Vector2(800, 100)); Node node2 = new Node(graph, new Vector2(150, 300)); Node node3 = new Node(graph, new Vector2(510, 280)); Node node4 = new Node(graph, new Vector2(690, 280)); Node node5 = new Node(graph, new Vector2(1050, 300)); Node node6 = new Node(graph, new Vector2(150, 450)); Node node7 = new Node(graph, new Vector2(510, 440)); Node node8 = new Node(graph, new Vector2(690, 440)); Node node9 = new Node(graph, new Vector2(1050, 450)); Node node10 = new Node(graph, new Vector2(400, 650)); Node node11 = new Node(graph, new Vector2(800, 650)); new Edge(graph, node0, node2, 2); new Edge(graph, node0, node3, 1); new Edge(graph, node1, node4, 1); new Edge(graph, node1, node5, 2); new Edge(graph, node2, node3, 1); new Edge(graph, node2, node6, 3); new Edge(graph, node3, node4, 2); new Edge(graph, node3, node7, 2); new Edge(graph, node4, node5, 1); new Edge(graph, node4, node8, 2); new Edge(graph, node5, node9, 3); new Edge(graph, node6, node7, 1); new Edge(graph, node6, node10, 2); new Edge(graph, node7, node8, 2); new Edge(graph, node7, node10, 1); new Edge(graph, node8, node9, 1); new Edge(graph, node8, node11, 1); new Edge(graph, node9, node11, 2); return graph; }
//Constructors public AStarNodeCapsule(Node node, Node endNode) { this.node = node; this.endNode = endNode; }
private Dictionary<int, AStarNodeCapsule> SetupCapsuleMap(Node endNode) { Dictionary<int, AStarNodeCapsule> capsuleMap = new Dictionary<int, AStarNodeCapsule>(); foreach (Node node in MainModel.Instance.Graph.AllNodes) { capsuleMap.Add(node.ID, new AStarNodeCapsule(node, endNode)); } return capsuleMap; }
/*AStar preparation*/ private void SetupAStarStart(out Dictionary<int, AStarNodeCapsule> capsuleMap, out SortedDictionary<int, SortedDictionary<int, AStarNodeCapsule>> closedList, out SortedDictionary<int, SortedDictionary<int, AStarNodeCapsule>> openList, Node startNode, Node endNode) { capsuleMap = SetupCapsuleMap(endNode); closedList = new SortedDictionary<int, SortedDictionary<int, AStarNodeCapsule>>(); openList = new SortedDictionary<int, SortedDictionary<int, AStarNodeCapsule>>(); capsuleMap[startNode.ID].ShortestDistance = 0; AddNodeCapsuleToClosedList(closedList, capsuleMap[startNode.ID]); }
/*AStar*/ private void PlanAStarRoute() { AStarNodeCapsule endAStarNodeCapsule = FindCapsuleWithAStarForEndNode(newTargetNode); UpdateCurrentRouteWithEndCapsuleData(endAStarNodeCapsule); newTargetNode = null; }
private AStarNodeCapsule FindCapsuleWithAStarForEndNode(Node endNode) { Dictionary<int, AStarNodeCapsule> capsuleMap; SortedDictionary<int, SortedDictionary<int, AStarNodeCapsule>> closedList; SortedDictionary<int, SortedDictionary<int, AStarNodeCapsule>> openList; SetupAStarStart(out capsuleMap, out closedList, out openList, this.PositionNode, endNode); bool failed = false; while (!failed) { if (closedList.Last().Value.Last().Value.Node == endNode) { return closedList.Last().Value.Last().Value; } PerformAStarStep(capsuleMap, closedList, openList); } return null; }
private void ArriveAtNode() { this.PositionNode = DetermineNodeToMoveTo(); this.Position = this.PositionNode.Position; this.movementEdge = null; this.elapsedTimeMoving = INITIAL_ELAPSED_TIME; }