void checkNeighbors() { validNeighbors.Clear(); guideCell = maze.cells [(int)(transform.position.x + 0.5f), (int)(transform.position.y + 0.5f)]; CellManagement cellProps = guideCell.GetComponent <CellManagement> (); int currentX = (int)guideCell.transform.position.x; int currentY = (int)guideCell.transform.position.y; //check north neighbor. First check if in bounds, then check if there is a wall blocking or not. if (!cellProps.north && ((currentX >= 0 && currentX < maze.width) && (currentY + 1 >= 0 && currentY + 1 < maze.height))) { validNeighbors.Add(NORTH); } //check south if (!cellProps.south && ((currentX >= 0 && currentX < maze.width) && (currentY - 1 >= 0 && currentY - 1 < maze.height))) { validNeighbors.Add(SOUTH); } //check east if (!cellProps.east && ((currentX + 1 >= 0 && currentX + 1 < maze.width) && (currentY >= 0 && currentY < maze.height))) { validNeighbors.Add(EAST); } //check west if (!cellProps.west && ((currentX - 1 >= 0 && currentX - 1 < maze.width) && (currentY >= 0 && currentY < maze.height))) { validNeighbors.Add(WEST); } }
void checkNeighbors() { validNeighbors.Clear(); CellManagement cellProps = guideCell.GetComponent <CellManagement> (); int currentX = (int)guideCell.transform.position.x; int currentY = (int)guideCell.transform.position.y; //check north neighbor. First check if in bounds, then check if there is a wall blocking or not. if (!cellProps.north && ((currentX >= 0 && currentX < maze.width) && (currentY + 1 >= 0 && currentY + 1 < maze.height))) { //only add if neighbor not pathVisited yet if (!maze.cells [currentX, currentY + 1].GetComponent <CellManagement> ().pathVisited) { validNeighbors.Add(NORTH); } } //check south if (!cellProps.south && ((currentX >= 0 && currentX < maze.width) && (currentY - 1 >= 0 && currentY - 1 < maze.height))) { //only add if neighbor not pathVisited yet if (!maze.cells [currentX, currentY - 1].GetComponent <CellManagement> ().pathVisited) { validNeighbors.Add(SOUTH); } } //check east if (!cellProps.east && ((currentX + 1 >= 0 && currentX + 1 < maze.width) && (currentY >= 0 && currentY < maze.height))) { //only add if neighbor not pathVisited yet if (!maze.cells [currentX + 1, currentY].GetComponent <CellManagement> ().pathVisited) { validNeighbors.Add(EAST); } } //check west if (!cellProps.west && ((currentX - 1 >= 0 && currentX - 1 < maze.width) && (currentY >= 0 && currentY < maze.height))) { //only add if neighbor not pathVisited yet if (!maze.cells [currentX - 1, currentY].GetComponent <CellManagement> ().pathVisited) { validNeighbors.Add(WEST); } } Debug.Log(validNeighbors.Count + " valid neighbors to " + string.Format("({0},{1})", guideCell.transform.position.x, guideCell.transform.position.y)); }
void Awake() { pathHistory = new Stack(); //initialize grid for maze if (useMazeSize) { width = GameManager.Instance.mazeSize; height = GameManager.Instance.mazeSize; } cells = new GameObject[width, height]; GameObject parent = GameObject.FindGameObjectWithTag("Maze"); Debug.Log("Level: " + (GameManager.Instance.mazeSize - 4)); //fill maze with unvisited cell prefabs for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Vector3 spawnPoint = new Vector3(x, y, parent.transform.position.z); cells [x, y] = Instantiate(cellPrefab, spawnPoint, Quaternion.identity, parent.transform); cells [x, y].tag = "UnvisitedCell"; cellProps = cells [x, y].GetComponent <CellManagement> (); cellProps.visited = false; cellProps.north = true; cellProps.south = true; cellProps.east = true; cellProps.west = true; } } //end grid for maze initialization unvisitedCells = GameObject.FindGameObjectsWithTag("UnvisitedCell").Length; //guideCell = cells [width / 2, height / 2];//center. These are really easy paths to the fish, though sometimes long. //guideCell = cells [0, height - 1];//top left. Moderate difficulty guideCell = cells[Random.Range((int)(width * 0.75f), width), Random.Range((int)(width * 0.75f), height)]; //Random cell. These are the most difficult paths //comment out following lines to not generate maze if (generateMaze) { createMaze(); spawnPlayer(); spawnFish(); } }
void tryWest() { int currentX = (int)guideCell.transform.position.x - 1; int currentY = (int)guideCell.transform.position.y; if ((currentX >= 0 && currentX < width) && (currentY >= 0 && currentY < height)) { if (cells [currentX, currentY].GetComponent <CellManagement> ().visited == false) { pathHistory.Push(guideCell); guideCell.tag = "VisitedCell"; cellProps = guideCell.GetComponent <CellManagement> (); cellProps.visited = true; cellProps.west = false; cellProps = cells [currentX, currentY].GetComponent <CellManagement> (); cellProps.visited = true; cellProps.east = false; guideCell = cells [currentX, currentY]; } } }
void trySouth() { int currentX = (int)guideCell.transform.position.x; int currentY = (int)guideCell.transform.position.y - 1; if ((currentX >= 0 && currentX < width) && (currentY >= 0 && currentY < height)) { if (cells [currentX, currentY].GetComponent <CellManagement> ().visited == false) { pathHistory.Push(guideCell); guideCell.tag = "VisitedCell"; //it gets a bit confusing but here's how it works: cellProps = guideCell.GetComponent <CellManagement>(); //set cellProps to be the props of the guideCell cellProps.visited = true; //mark it visited cellProps.south = false; //get rid of it's south wall because we are moving south. This will always be the direction we are moving cellProps = cells[currentX, currentY].GetComponent <CellManagement>(); //set cellProps to the new cell cellProps.visited = true; //mark this one as visited cellProps.north = false; //get rid of it's NORTH wall since we just came from the north. This will always be the opposite direction we are moving guideCell = cells [currentX, currentY]; //only set new guideCell if we can move. Otherwise try again } } }
} //end createMaze //these methods will check to see if direction given is valid, if it is it will set all properties acordingly, if not it will do nothing. void tryNorth() { int currentX = (int)guideCell.transform.position.x; int currentY = (int)guideCell.transform.position.y + 1; if ((currentX >= 0 && currentX < width) && (currentY >= 0 && currentY < height)) { //coords are in bounds, now check if cell at that position has been visited or not if (cells [currentX, currentY].GetComponent <CellManagement> ().visited == false) { pathHistory.Push(guideCell); guideCell.tag = "VisitedCell"; cellProps = guideCell.GetComponent <CellManagement> (); cellProps.visited = true; cellProps.north = false; cellProps = cells [currentX, currentY].GetComponent <CellManagement> (); cellProps.visited = true; cellProps.south = false; guideCell = cells [currentX, currentY]; } } }