///<summary> ///load graph from map data ///</summary> ///<param name="mapData"></param> ///<returns>true if successful</returns> public bool Load(MapData mapData) { Clear(); //get the number of nodes and read them in int numNodes = mapData.NodeList.Count; for (int n = 0; n < numNodes; ++n) { NavGraphNode newNode = new NavGraphNode(mapData.NodeList[n]); //when editing graphs (with Buckland's Raven map editor), it's //possible to end up with a situation where some of the nodes //have been invalidated (their id's set to invalid_node_index). //Therefore when a node of index invalid_node_index is //encountered, it must still be added. if (!GraphNode.IsInvalidIndex(newNode.Index)) { AddNode(newNode); } else { Nodes.Add(newNode); //make sure an edgelist is added for each node Edges.Add(new LinkedList<NavGraphEdge>()); ++NextNodeIndex; } } //now add the edges int numEdges = mapData.EdgeList.Count; for (int e = 0; e < numEdges; ++e) { NavGraphEdge nextEdge = new NavGraphEdge(mapData.EdgeList[e]); Edges[nextEdge.From].AddLast(nextEdge); } return true; }
///<summary> ///Checks to see if an entity has moved cells. If so the data structure ///is updated accordingly ///</summary> ///<param name="ent"></param> ///<param name="oldPos"></param> public void UpdateEntity(NavGraphNode ent, Vector2 oldPos) { //if the index for the old pos and the new pos are not equal then //the entity has moved to another cell. int oldIdx = PositionToIndex(oldPos); int newIdx = PositionToIndex(ent.Position); if (newIdx == oldIdx) return; //the entity has moved into another cell so delete from current //cell and add to new one Cells[oldIdx].Members.Remove(ent); Cells[newIdx].Members.Add(ent); }
///<summary> ///Given a node this method first checks to see if the node has been ///added previously but is now inactive. If it is, it is reactivated. /// ///If the node has not been added previously, it is checked to make ///sure its index matches the next node index before being added to ///the graph ///</summary> ///<param name="node"></param> ///<returns></returns> public int AddNode(NavGraphNode node) { if (node.Index < Nodes.Count) { //make sure the client is not trying to add a node with the same //Id as a currently active node Assert.Fatal(GraphNode.IsInvalidIndex(Nodes[node.Index].Index), "SparseGraph.AddNode: Attempting to add a node with a duplicate Id"); Nodes[node.Index] = node; return NextNodeIndex; } //make sure the new node has been indexed correctly Assert.Fatal(node.Index == NextNodeIndex, "SparseGraph.AddNode: invalid index"); Nodes.Add(node); Edges.Add(new LinkedList<NavGraphEdge>()); return NextNodeIndex++; }
///<summary> ///Used to add the entities to the data structure ///</summary> ///<param name="ent"></param> public void AddEntity(NavGraphNode ent) { Assert.Fatal(ent != null, "CellSpacePartition.AddEntity: ent is null"); if (ent == null) return; int idx = PositionToIndex(ent.Position); Cells[idx].Members.Add(ent); }