void MoveThroughMaze() { if (UpdateMovement(_nextCoord)) { // if character has reached its target, check if it can still go in the same direction _currentCoord = Coordinate; DoWhenCellReached(); _lastCoord = _currentCoord; if (gm.GetNode(Coordinate).HasNeighbor(Direction)) { // if there is a cell in the current direction, then start moving towards it _nextCoord = Coordinate + Direction; UpdateAnimator(); } } else { // if character has not arrived to its target, check if it is going through a tunnel Node nodeAtStart; if (gm.GetNode(Coordinate) == null && (nodeAtStart = gm.GetNode(_currentCoord)).Type == NodeType.Tunnel) { // if going through a tunnel, move the character from one end of the tunnel to the other while keeping the relative worldPosition to the last cell Vector3 startPos = gm.CellToWorld(_currentCoord); NodeTunnel nt1 = nodeAtStart as NodeTunnel; NodeTunnel nt2 = nt1.LinkedTunnel; Vector2Int newStartCoord = nt2.Coordinate + Vector2Int.left * (int)Mathf.Sign(nt1.Coordinate.x - nt2.Coordinate.x); Vector3 newStartPos = gm.CellToWorld(newStartCoord); transform.position -= startPos - newStartPos; _currentCoord = newStartCoord; _nextCoord = nt2.Coordinate; _lastCoord = _currentCoord; } } }
public NodeTunnel(Node node) : base(node.Coordinate) { Type = NodeType.Tunnel; Neighbors = node.Neighbors; Depth = node.Depth; Parent = node.Parent; LinkedTunnel = null; }
public void LinkTunnel(NodeTunnel node, bool linkBoth = true) { LinkedTunnel = node; Neighbors.Add(node); if (linkBoth) { node.LinkTunnel(this, false); } }
void InitializeGraph() { // +++ Generate Nodes +++ // gridNodes = new Dictionary <Vector2Int, Node>(); tilemapPath.CompressBounds(); // get rid of empty rows and columns in the cellBounds BoundsInt bounds = tilemapPath.cellBounds; // iterate over the tilemap from the bottom left corner to the top right corner for (int x = bounds.xMin; x < bounds.xMax; x++) { for (int y = bounds.yMin; y < bounds.yMax; y++) { Vector3Int gridPos = new Vector3Int(x, y, 0); if (tilemapPath.GetTile(gridPos) != null) { // if there is a tile at these coordinates, create a node for it and add it to the appropriate dictionary Vector2Int coord = new Vector2Int(gridPos.x, gridPos.y); gridNodes[coord] = new Node(coord); } } } // +++ Link Nodes +++ // List <NodeTunnel> nodeTunnels = new List <NodeTunnel>(); // iterate over every node in the dictionary foreach (Vector2Int coord in gridNodes.Keys) { Node n = gridNodes[coord]; // iterate over every adjacent node for (int i = 0; i < PacTools.v2IntDirections.Length; i++) { Vector2Int c = coord + PacTools.v2IntDirections[i]; if (gridNodes.ContainsKey(c)) { // if the neighboring node exists at these coordinates, link it as a neighbor of the node n.Neighbors.Add(gridNodes[c]); } } if (n.Neighbors.Count == 0) { Debug.LogError("Lonely Node found at " + coord.ToString()); } else if (n.Neighbors.Count == 1) { // if the node is a tunnel, replace it to reflect it n = new NodeTunnel(n); nodeTunnels.Add(n as NodeTunnel); } else if (n.Neighbors.Count == 2) { if ((n.Neighbors[0].Coordinate.x == n.Neighbors[1].Coordinate.x) || (n.Neighbors[0].Coordinate.y == n.Neighbors[1].Coordinate.y)) { n.Type = NodeType.Hallway; } else { n.Type = NodeType.Corner; } } else { n.Type = NodeType.Crossroad; } } // iterate over every tunnel node and compare it with every other tunnel nodes foreach (NodeTunnel n1 in nodeTunnels) { gridNodes[n1.Coordinate] = n1; if (n1.LinkedTunnel == null) { // if this tunnel has not yet been linked foreach (NodeTunnel n2 in nodeTunnels) { if (n2.LinkedTunnel == null && (n1.Coordinate.y == n2.Coordinate.y)) { // if the other tunnel node has not yet been linked and is on the same line n1.LinkTunnel(n2); // link them together } } } } /*/ +++ Clean Graph +++ // * //List<Node> hallwayNodes = new List<Node>(); * List<Node> extremityNodes = new List<Node>(); * // iterate over every node and check if they are hallway * foreach(Node n in gridNodes.Values) { * Node[] neighbors = n.Neighbors.ToArray(); * if(n.IsHallway) { * // if node is hallway, rewire neighbors so that in the end all corner/crossroad/tunnel nodes are only linked to each other * //hallwayNodes.Add(n); * int index0 = neighbors[0].Neighbors.IndexOf(n); * neighbors[0].Neighbors[index0] = neighbors[1]; * int index1 = neighbors[1].Neighbors.IndexOf(n); * neighbors[1].Neighbors[index1] = neighbors[0]; * } * } * // I let this unused code stay because I thought it was pretty smart.*/ }