public void setupHexGrid() { int count = 0; _hexNodes = new hexNode[width, height]; Debug.Log("node count: " + _hexNodes.Length); for (int y = 0; y < height; y++) { if (y % 2 == 0) { for (int x = 0; x < width - 1; x++) { hexNode tempNode = new hexNode(x, y); if (x == width - 2) { tempNode.isEnd = true; } _hexNodes[x, y] = tempNode; count++; } } else { for (int x = 0; x < width; x++) { hexNode tempNode = new hexNode(x, y); if (x == width - 1) { tempNode.isEnd = true; } tempNode.isWide = true; _hexNodes[x, y] = tempNode; count++; } } } /* * for (int x = 0; x < width * height; x++) * { * hexNode tempNode = new hexNode(count, actualY); * _hexNodes[x] = tempNode; * * count++; * if (actualY % 2 == 0) * { * if (count == width - 2) * { * count = 0; * actualY++; * } * } * else * { * if (count == width - 1) * { * count = 0; * actualY++; * } * } * } */ }
public IEnumerator backTraceMazeHex() //TODO change possible to vector2, change hexnode[x] to hexnode[x,y] { System.Random rng = new System.Random(); int choice; Vector2 index = new Vector2(_gridWidth / 2, (_gridLength - 1) / 2); Vector2 newIndex = new Vector2(0, 0); hexNode currentNode = _grid._hexNodes[(int)index.x, (int)index.y]; List <Vector2> stackIndexes = new List <Vector2>(); List <Vector2> visited = new List <Vector2>(); List <int> possible = new List <int>(); visited.Add(index); //each node visited should be added to avoid repeating nodes. int cellsPerLinePair = _gridWidth * 2 - 1; Debug.Log(cellsPerLinePair); do { possible.Clear(); if (index.x == 0) //LEFT { if (index.y == 0) //BOTTOM { possible.Add(1); possible.Add(3); possible.Add(11); } else if (index.y == _gridLength - 1) //TOP { if (currentNode.wideRow) { possible.Add(3); possible.Add(5); } else { possible.Add(3); possible.Add(5); possible.Add(7); } } else { if (currentNode.isWide) { possible.Add(1); possible.Add(3); possible.Add(5); } else { possible.Add(1); possible.Add(3); possible.Add(5); possible.Add(7); possible.Add(11); } } } else if (currentNode.endNode) //RIGHT { if (index.y == 0) //BOTTOM { possible.Add(1); possible.Add(9); possible.Add(11); } else if (index.y == _gridLength - 1) //TOP { if (currentNode.wideRow) { possible.Add(7); possible.Add(9); } else { possible.Add(5); possible.Add(7); possible.Add(9); } } else { if (currentNode.isWide) { possible.Add(7); possible.Add(9); possible.Add(11); } else { possible.Add(1); possible.Add(5); possible.Add(7); possible.Add(9); possible.Add(11); } } } else if (index.y == 0) //BOTTOM { possible.Add(1); possible.Add(3); possible.Add(9); possible.Add(11); } else if (index.y == _gridLength - 1) //TOP { possible.Add(3); possible.Add(5); possible.Add(7); possible.Add(9); } else { possible.Add(1); possible.Add(3); possible.Add(5); possible.Add(7); possible.Add(9); possible.Add(11); } if (currentNode.isEnd) { possible.Remove(3); if (currentNode.isWide) { possible.Remove(1); possible.Remove(5); } } checkPossible: if (possible.Count != 0) { if (!stackIndexes.Contains(index)) { stackIndexes.Add(index); } choice = rng.Next(0, possible.Count); if (possible[choice] == 1) { if (currentNode.isWide) { newIndex = new Vector2(index.x, index.y + 1); } else { newIndex = new Vector2(index.x + 1, index.y + 1); } if (visited.Contains(newIndex)) { possible.Remove(1); goto checkPossible; } Debug.Log("choice = 1"); currentNode.One.SendMessage("DestroyParent"); } else if (possible[choice] == 3) { newIndex = new Vector2(index.x + 1, index.y); if (visited.Contains(newIndex)) { possible.Remove(3); goto checkPossible; } Debug.Log("choice = 3"); currentNode.Three.SendMessage("DestroyParent"); } else if (possible[choice] == 5) { if (currentNode.isWide) { newIndex = new Vector2(index.x, index.y - 1); } else { newIndex = new Vector2(index.x + 1, index.y - 1); } if (visited.Contains(newIndex)) { possible.Remove(5); goto checkPossible; } Debug.Log("choice = 5"); currentNode.Five.SendMessage("DestroyParent"); } else if (possible[choice] == 7) { if (currentNode.isWide) { newIndex = new Vector2(index.x - 1, index.y - 1); } else { newIndex = new Vector2(index.x, index.y - 1); } if (visited.Contains(newIndex)) { possible.Remove(7); goto checkPossible; } Debug.Log("choice = 7"); currentNode.Seven.SendMessage("DestroyParent"); } else if (possible[choice] == 9) { newIndex = new Vector2(index.x - 1, index.y); if (visited.Contains(newIndex)) { possible.Remove(9); goto checkPossible; } Debug.Log("choice = 9"); currentNode.Nine.SendMessage("DestroyParent"); } else if (possible[choice] == 11) { if (currentNode.isWide) { newIndex = new Vector2(index.x - 1, index.y + 1); } else { newIndex = new Vector2(index.x, index.y + 1); } if (visited.Contains(newIndex)) { possible.Remove(11); goto checkPossible; } Debug.Log("choice = 11"); currentNode.Eleven.SendMessage("DestroyParent"); } Debug.Log(string.Format("moving from node: {0} to {1}", index, newIndex)); index = newIndex; currentNode = _grid._hexNodes[(int)index.x, (int)index.y]; visited.Add(index); yield return(new WaitForSeconds(0.01f)); } else //current cell has no unvisited neighbours, remove it from the stack and backtrack. { Debug.Log("no possibles"); if (stackIndexes.Count > 0) { stackIndexes.Remove(index); if (stackIndexes.Count != 0) { index = stackIndexes[stackIndexes.Count - 1]; currentNode = _grid._hexNodes[(int)index.x, (int)index.y]; } } } } while (stackIndexes.Count > 0); visited.Clear(); }