private Node CreateNode(Stream mdlStream, Stream mdxStream, Node parent) { byte[] buffer = new byte[2]; mdlStream.Read(buffer, 0, 2); Node.Type nodeType = (Node.Type)BitConverter.ToUInt16(buffer, 0); Node node; if ((nodeType & Node.Type.Saber) == Node.Type.Saber) { node = new SaberNode(mdlStream, mdxStream, nodeType, this); } else if ((nodeType & Node.Type.Skin) == Node.Type.Skin) { node = new SkinnedMeshNode(mdlStream, mdxStream, nodeType, this); } else if ((nodeType & Node.Type.Mesh) == Node.Type.Mesh) { node = new MeshNode(mdlStream, mdxStream, nodeType, this); } else if ((nodeType & Node.Type.Light) == Node.Type.Light) { node = new LightNode(mdlStream, mdxStream, nodeType, this); } else { node = new Node(mdlStream, mdxStream, nodeType, this); } node.parent = parent; return(node); }
/// <summary> /// Creates nodes for the vertices of an obstacle. Remember it's an inverted Y axis. /// </summary> /// <param name="obstacle"></param> /// <param name="obstacleType"></param> /// <returns></returns> private void createNodesForObstacle(ObstacleRepresentation obstacle, Node.Type obstacleType) { List <Point> corners = new List <Point>(); // Make the 'corners' with just a little bit of offset, so that walkable line works fine Point upLeftCorner = new Point((int)(obstacle.X - obstacle.Width / 2) - 1, (int)(obstacle.Y - obstacle.Height / 2) - 1); Point upRightCorner = new Point((int)(obstacle.X + obstacle.Width / 2) + 1, (int)(obstacle.Y - obstacle.Height / 2) - 1); Point downLeftCorner = new Point((int)(obstacle.X - obstacle.Width / 2) - 1, (int)(obstacle.Y + obstacle.Height / 2) + 1); Point downRightCorner = new Point((int)(obstacle.X + obstacle.Width / 2) + 1, (int)(obstacle.Y + obstacle.Height / 2) + 1); corners.Add(upLeftCorner); corners.Add(upRightCorner); corners.Add(downLeftCorner); corners.Add(downRightCorner); for (int i = 0; i < corners.Count; i++) { Point currentCorner = corners[i]; if (this.matrix.inBounds(currentCorner)) { Node node = new Node(currentCorner.X, currentCorner.Y, obstacleType); this.addNode(node); } } }
private int ParseFRule(Node fruleNode, int currentRule, Dictionary <string, List <Node> > vars) { Node.Type type = fruleNode.GetNodeType(); List <Node> children, grandchildren; string varName; switch (type) { case (Node.Type.TSRULE): children = fruleNode.GetChildren(); varName = ParseString(children [0]); if (vars.ContainsKey(varName)) { throw new CloudMakefile.ParseException("There should not be two variables with the same name " + "within the same rule."); } grandchildren = children [1].GetChildren(); for (int i = 0; i < grandchildren.Count; i++) { if (grandchildren [i].GetNodeType() != Node.Type.TSEQUENCE) { throw new CloudMakefile.ParseException("TSRULE type should only have TSEQUENCE types after " + "TVAR type."); } } vars.Add(varName, grandchildren); return(ParseFRule(children [2], currentRule, vars)); case (Node.Type.TNRULE): children = fruleNode.GetChildren(); varName = ParseString(children [0]); if (vars.ContainsKey(varName)) { throw new CloudMakefile.ParseException("There should not be two variables with the same name " + "within the same rule."); } grandchildren = children [1].GetChildren(); if ((grandchildren.Count != 2) || (grandchildren [0].GetNodeType() != Node.Type.TSEQUENCE) || (grandchildren [1].GetNodeType() != Node.Type.TSEQUENCE)) { throw new CloudMakefile.ParseException("TNRULE should have two children under TSEQUENCE TYPE."); } int start = ParseInt(grandchildren [0]); int end = ParseInt(grandchildren [1]); vars.Add(varName, new List <Node> ()); for (int i = start; i <= end; i++) { vars [varName].Add(MakeNodeFromInt(i)); } return(ParseFRule(children [2], currentRule, vars)); case (Node.Type.TRULE): return(ParseMultipleRule(fruleNode, currentRule, vars)); default: throw new CloudMakefile.ParseException("The rule must be of type TSRULE, TNRULE or TRULE."); } }
public Node GetNearestFreeNodeOfType(Node.Type type, Vector3 position) { List <Node> nodes = new List <Node>(); root.GetFreeNodesOfType(type, ref nodes); if (nodes.Count == 0) { return(null); } return(nodes.OrderBy(n => (n.transform.position - position).sqrMagnitude).First()); }
public bool PlotPathToNodeType(Node.Type type, Node currentNode, ref Queue <Node> path, bool isMoving) { bool removeFirstElement = !isMoving; if (isMoving) { Node node; do { node = path.Dequeue(); // TODO remove "turn at next node" behaviour in case of 180° turn! removeFirstElement |= node.HasChild(currentNode); }while (path.Count > 0); node.Vacate(); } else { path.Clear(); } // makes most sense to measure from the midpoint or there will be weird moves Node endPoint = GetNearestFreeNodeOfType(type, root.transform.position); Debug.LogFormat("Endpoint is {0}", (endPoint ? endPoint.name : "null")); if (!endPoint) { return(false); } endPoint.Reserve(); currentNode.GetPathToRoot(ref path); // remove start node if not moving if (removeFirstElement) { path.Dequeue(); } Stack <Node> fromRoot = new Stack <Node>(); endPoint.GetPathToRootReversed(ref fromRoot); fromRoot.Pop(); while (fromRoot.Count > 0) { path.Enqueue(fromRoot.Pop()); } Debug.LogFormat("Path has {0} nodes!", path.Count); return(true); }
public bool TryMoveTo(Node.Type type) { if (node && type == node.GetNodeType()) { return(true); } if (office.PlotPathToNodeType(type, node, ref path, isMoving)) { // remove node.Vacate(); return(true); } return(false); }
public void SetNode(Node node, Node.Type nodeType) // public void SetNode(Coords nodeCoords, NodeType nodeType) { // Set the node accordingly node.type = nodeType; // Remove this node from its neighbors' lists of closed neighbors, if setting to open if (Node.Type.Path == nodeType || Node.Type.Room == nodeType) { // Add this node to the set of nodes with closed neighbors if not already there // This method should only get called by GeneratePath once at maximum per node if (Node.Type.Path == nodeType && node.closedNeighbors.Count > 0) { if (!Node.pathsWithClosedNeighbor.ContainsKey(node.index)) { Node.pathsWithClosedNeighbor.Add(node.index, node); } } // Removing node from neighbors' closedNeighbors foreach (Coords.Direction neighborDirection in node.neighbors) { // Get the neighboring node in each neighboring direction Coords neighborCoords = Coords.Displaced(node.coords, neighborDirection); Node neighbor = Node.nodes [Node.GetIndex(neighborCoords)]; if (neighbor.closedNeighbors.Count > 0) { // Remove the originating node from the neighbors' closedNeighbors lists Coords.Direction inverseDirection = Coords.Inverse(neighborDirection); if (neighbor.closedNeighbors.Contains(inverseDirection)) { neighbor.closedNeighbors.Remove(inverseDirection); // If no more closed neighbors, remove this node from the global list if (0 == neighbor.closedNeighbors.Count) { Node.pathsWithClosedNeighbor.Remove(Node.GetIndex(neighbor.coords)); } } } } } }
void ParseRelations(XmlNode node) { if (node.Name == "state" || node.Name == "parallel" || node.Name == "pseudo") { var state_name = node.Attributes["id"].Value; int index = name_to_node[state_name]; // Superstate int superstate = index == 0 ? -1 : name_to_node[node.ParentNode.Attributes["id"].Value]; // Components int components_start = relations.Count; foreach (XmlNode n in node.ChildNodes) { if (n.Name == "state" || n.Name == "parallel") { if (n.Attributes["id"] == null) { Debug.LogError("Attribute \"id\" missing in node " + node); } var comp_name = n.Attributes["id"].Value; relations.Add(name_to_node[comp_name]); } } // Data (default state) int data = 0; // Type Node.Type type = Node.Type.Error; if (components_start == relations.Count) { if (node.Name == "state") { type = Node.Type.Basic; } else if (node.Name == "pseudo") { type = Node.Type.Pseudo; } } else { if (node.Name == "state") { type = Node.Type.Compound; if (node.Attributes["initial"] == null) { Debug.LogError("Missing default state of " + state_name + " in " + scxml.name); } if (!name_to_node.TryGetValue(node.Attributes["initial"].Value, out data)) { Debug.LogError("The default state of " + state_name + " in " + scxml.name + " does not exist."); } } else if (node.Name == "parallel") { type = Node.Type.Parallel; } } // Name if (superstate >= 0) { node_to_name.Add(node_to_name[superstate] + "." + state_name); } else { node_to_name.Add(state_name); } name_to_node[node_to_name[index]] = index; nodes[index] = new Node(type, superstate, components_start, data, 0); foreach (XmlNode n in node.ChildNodes) { ParseRelations(n); } } else if (node.Name == "transition") { } else { Debug.LogError("Error: Unsupported XML name in statechart document: \"" + node.Name + "\""); } }
public void GenerateGrid() { grid = new Node[(int)size_x, (int)size_y]; for (int i = 0; i < size_x; i++) { for (int j = 0; j < size_y; j++) { Vector3 nodePosition = new Vector3(node_size * 0.5f + i * node_size - size_x / 2 * node_size, 0, node_size * 0.5f + j * node_size - size_y / 2 * node_size); Vector3 worldNodePosition = transform.position + nodePosition; Collider[] colliders = Physics.OverlapSphere(worldNodePosition, node_size * 0.2f); Node.Type objectType = Node.Type.water; bool isTransitable = false; for (int k = 0; k < colliders.Length; k++) { if (colliders[k].tag == "Island") { objectType = Node.Type.floor; isTransitable = true; } if (colliders[k].tag == "Tree") { objectType = Node.Type.tree; isTransitable = true; } if (colliders[k].tag == "Rock") { objectType = Node.Type.rock; isTransitable = true; } if (colliders[k].tag == "Enemy") { objectType = Node.Type.enemy; isTransitable = true; } if (colliders[k].tag == "Decoration") { objectType = Node.Type.decoration; isTransitable = true; } if (colliders[k].tag == "Village") { objectType = Node.Type.village; isTransitable = true; } if (colliders[k].tag == "Shore") { objectType = Node.Type.shore; isTransitable = true; } if (colliders[k].tag == "Entry") { objectType = Node.Type.entry; isTransitable = true; } if (colliders[k].tag == "Exit") { objectType = Node.Type.exit; isTransitable = true; } } grid[i, j] = new Node(i, j, node_size, worldNodePosition, objectType, isTransitable); } } }
//Aggiunge un nodo al grafo private void addNode(int nodeNumber, int roomNumber, Vector2 coordinates, Node.Type type) { Node node = new Node(nodeNumber, roomNumber, coordinates, type); addNode(node); }
public void AddIntersection(Node node, Road road1, Road road2, Segment seg1 = null, Segment seg2 = null, bool coerce = false, Node.Type type = Node.Type.turn) { UpdatedIntersectionPositions.Add(node.pos()); if (type == Node.Type.intersection && CheckForIntersection(node.pos())) { return; } //Intersection intersection = Instantiate(intersectionPrefab, transform); //intersection.transform.position = node.pos(); node.transform.name = road1.roadName + " and " + road2.roadName; //intersection.transform.parent = node.transform; nodes.Add(node); if (!road1.nodes.Contains(node)) { road1.nodes.Add(node); } if (!road2.nodes.Contains(node)) { road2.nodes.Add(node); } if (seg1 != null) { seg1.AddIntersection(node); } if (seg2 != null) { seg2.AddIntersection(node); } if (seg1 != null && seg2 != null) { node.Init(new List <Segment> { seg1, seg2 }); } else { node.Init(); } }
List <Node> FindAdjacent(Node head, List <Node> found, Node[,] nodes, Vector2[] directions, Node.Type type) { if (!head.visited) { head.visited = true; found.Add(head); foreach (Vector2 dir in directions) { Node child = nodes[(int)(head.position.x + dir.x), (int)(head.position.y + dir.y)]; if (child != null && !child.visited && child.nodeType == type) { FindAdjacent(child, found, nodes, directions, type); } } } return(found); }