Beispiel #1
0
    private void Reset()
    {
        cells = new Maze.Cell[Maze.MazeSize.x, Maze.MazeSize.y];
        for (yPos = 0; yPos < Maze.MazeSize.y; yPos++)
        {
            for (xPos = 0; xPos < Maze.MazeSize.x; xPos++)
            {
                cells[xPos, yPos] = new Maze.Cell(xPos, yPos);
            }
        }

        Maze.Cell start = Maze.Cells[0, 0];
        orientation = MouseOrientation.North;
        if (start.HasWall(Maze.Wall.North))
        {
            orientation = MouseOrientation.East;
        }

        GameObject.FindGameObjectsWithTag("Wall").ToList().ForEach(go => go.GetComponent <Renderer>().material.color   = Color.white);
        GameObject.FindGameObjectsWithTag("Ground").ToList().ForEach(go => go.GetComponent <Renderer>().material.color = Color.black);
        GameObject.FindGameObjectsWithTag("End").ToList().ForEach(go => go.GetComponent <Renderer>().material.color    = Color.gray);

        xPos = 0;
        yPos = 0;

        UpdateTransform();
    }
Beispiel #2
0
	private void DrawMaze() {
		for (float y = 0; y < maze.Height; y++) {
			for (float x = 0; x < maze.Width; x++) {
               
				Maze.Cell cell = maze.GetCell((int)x, (int)y);

				if (!cell.wayNorth && y == 0) {
					float wallX = x * wallSize - maze.Width * wallSize / 2f + wallSize / 2f;
					float wallY = -y * wallSize + maze.Height * wallSize / 2f - wallThin / 2f;
					this.DrawHorizontalWall(wallX, wallY, GetRandomMaterial());
				}

				if (!cell.waySouth) {
					float wallX = x * wallSize - maze.Width * wallSize / 2f + wallSize / 2f;
					float wallY = -y * wallSize + maze.Height * wallSize / 2f - wallSize + wallThin / 2f;
					this.DrawHorizontalWall(wallX, wallY, GetRandomMaterial());
				}
               
				if (!cell.wayWest && x == 0) {
					float wallX = x * wallSize - maze.Width * wallSize / 2f + wallThin / 2f;
					float wallY = -y * wallSize + maze.Height * wallSize / 2f - wallSize / 2f;
					this.DrawVerticalWall(wallX, wallY, GetRandomMaterial());
				}

				if (!cell.wayEast) {
					float wallX = x * wallSize - maze.Width * wallSize / 2f + wallSize - wallThin / 2f;
					float wallY = -y * wallSize + maze.Height * wallSize / 2f - wallSize / 2f;
					this.DrawVerticalWall(wallX, wallY, GetRandomMaterial());
				}


			}
		}
	}
Beispiel #3
0
    bool GenerateStep()
    {
        if (_stack.Count == 0)
        {
            return(true);
        }

        Maze.Cell c = _stack.Peek();
        //SetMousePosition(c.x, c.y);

        var neighbours = maze.GetNeighbours(c.x, c.y);

        if (neighbours.Count != 0)
        {
            var index = 0;
            if (neighbours.Count > 1)
            {
                index = UnityEngine.Random.Range(0, neighbours.Count);
            }
            var       item      = neighbours[index];
            Maze.Cell neighbour = item.Item2;
            neighbour.visited = true;
            maze.RemoveCellWall(c.x, c.y, item.Item1);

            _stack.Push(neighbour);
            //SetMousePosition(neighbour.x, neighbour.y);
        }
        else
        {
            _stack.Pop();
        }
        return(false);
    }
Beispiel #4
0
    // Start is called before the first frame update
    void Start()
    {
        START_Y       = -rows / 2;
        START_X       = -cols / 2;
        maze          = new Maze(rows, cols);
        mCellGameObjs = new GameObject[cols, rows];
        for (int i = 0; i < cols; ++i)
        {
            for (int j = 0; j < rows; ++j)
            {
                GameObject obj  = Instantiate(mCellPrefab);
                Maze.Cell  cell = maze.GetCell(i, j);
                cell.OnSetDirFlag      = OnCellSetDirFlag;
                obj.transform.position = new Vector3(START_X + cell.x, START_Y + cell.y, 1.0f);
                mCellGameObjs[i, j]    = obj;
            }
        }

        mMouse = Instantiate(mMousePrefab);
        SetMousePosition(0, 0);
        //mMouse.transform.position = new Vector3(START_X, START_Y, 0.0f);

        _stack.Push(maze.GetCell(mousex, mousey));
        //StartCoroutine(Coroutine_Generate());
    }
Beispiel #5
0
    private void SetupFloodFill()
    {
        floodFill   = new FloodCell[Maze.MazeSize.x, Maze.MazeSize.y];
        floodStatus = FloodStatus.Find;

        for (int y = 0; y < Maze.MazeSize.y; y++)
        {
            for (int x = 0; x < Maze.MazeSize.x; x++)
            {
                floodFill[x, y]          = new FloodCell(x, y);
                floodFill[x, y].textMesh = GameObject.Find(x + "," + y).GetComponentInChildren <TextMesh>();
            }
        }

        previousCell = null;
    }
Beispiel #6
0
    public static Wall GetWallBetween(Maze.Coord currentCell, Maze.Coord nextCell)
    {
        Maze.Cell cellA = Maze.Cells[currentCell.x, currentCell.y];
        Maze.Cell cellB = Maze.Cells[nextCell.x, nextCell.y];

        if (cellA.x + 1 == cellB.x)
        {
            if (cellA.HasWall(Maze.Wall.East) && cellB.HasWall(Maze.Wall.West))
            {
                return(Maze.Wall.East);
            }
        }

        if (cellB.x + 1 == cellA.x)
        {
            if (cellA.HasWall(Maze.Wall.West) && cellB.HasWall(Maze.Wall.East))
            {
                return(Maze.Wall.West);
            }
        }

        if (cellA.y + 1 == cellB.y)
        {
            if (cellA.HasWall(Maze.Wall.North) && cellB.HasWall(Maze.Wall.South))
            {
                return(Maze.Wall.North);
            }
        }

        if (cellB.y + 1 == cellA.y)
        {
            if (cellA.HasWall(Maze.Wall.South) && cellB.HasWall(Maze.Wall.North))
            {
                return(Maze.Wall.South);
            }
        }

        return(Maze.Wall.None);
    }
Beispiel #7
0
    private void ModifiedFloodFillStep()
    {
        var groundTag = DetectGround(paint: false);

        if (groundTag == "End" && floodStatus == FloodStatus.Find)
        {
            floodStatus = FloodStatus.Return;
        }
        else if (groundTag == "Start" && floodStatus == FloodStatus.Return)
        {
            floodStatus  = FloodStatus.Finished;
            pauseExplore = true;
            return;
        }

        if (floodStatus == FloodStatus.Find)
        {
            CalculateModifiedFloodFill(Maze.GetEndCells());
        }
        else if (floodStatus == FloodStatus.Return)
        {
            CalculateModifiedFloodFill(new Maze.Coord(0, 0));
        }

        var front = orientation;
        var left  = GetOrientation(orientation, -1);
        var right = GetOrientation(orientation, 1);

        int deltaRotation = 1;

        var hasFrontWall = cells[xPos, yPos].HasWall(GetWallFromMouseOrientation(front));
        var hasLeftWall  = cells[xPos, yPos].HasWall(GetWallFromMouseOrientation(left));
        var hasRightWall = cells[xPos, yPos].HasWall(GetWallFromMouseOrientation(right));

        int lowestFlood = Int32.MaxValue;

        Maze.Cell cell = null;

        cell = GetAdjacentCell(front);
        if (!hasFrontWall && cell != null)
        {
            var adjacentFlood = floodFill[cell.x, cell.y].value;
            if (adjacentFlood < lowestFlood)
            {
                lowestFlood   = adjacentFlood;
                deltaRotation = 0;
            }
        }

        cell = GetAdjacentCell(left);
        if (!hasLeftWall && cell != null)
        {
            var adjacentFlood = floodFill[cell.x, cell.y].value;
            if (adjacentFlood < lowestFlood)
            {
                lowestFlood   = adjacentFlood;
                deltaRotation = -1;
            }
        }

        cell = GetAdjacentCell(right);
        if (!hasRightWall && cell != null)
        {
            var adjacentFlood = floodFill[cell.x, cell.y].value;
            if (adjacentFlood < lowestFlood)
            {
                lowestFlood   = adjacentFlood;
                deltaRotation = 1;
            }
        }

        if (previousCell != null)
        {
            var adjacentFlood = floodFill[previousCell.x, previousCell.y].value;
            if (adjacentFlood < lowestFlood)
            {
                lowestFlood   = adjacentFlood;
                deltaRotation = -2;
            }
        }

        previousCell = cells[xPos, yPos];

        Rotate(deltaRotation);
        forceMove = true;
    }
 public static Vector2Int ToVecMazePos(this Maze.Cell pos)
 {
     return(new Vector2Int(pos.X, pos.Y));
 }
Beispiel #9
0
    public void FollowDijkstraPath(Maze.Cell currentCell, int index, Direction direction)
    {
        switch (direction)
        {
        case Direction.NORTH:
            // If no north neighbor
            Debug.Log("-------- GOING NORTH ------------");
            Debug.Log("Current Cell Index in allNodes: " + allNodes.IndexOf(currentCell));
            Debug.Log("If path, cells index: " + System.Array.IndexOf(cells, currentCell));
            Debug.Log("Using this index: " + index);
            Debug.Log("--------------");
            if (currentCell.north != null || currentCell.northNeighbor.visited)
            {
                FollowDijkstraPath(currentCell, index, Direction.WEST);
            }
            Maze.Cell tempCell = new Maze.Cell();
            tempCell = currentCell.northNeighbor;
            if (tempCell.isNode)
            {
                tempCell.visited = true;
                Debug.Log("Tempcell index: dtree next index -- " + allNodes.IndexOf(tempCell) + " : " + d_tree[index + 1]);
                // If this is our next Node in path
                if (allNodes.IndexOf(tempCell) == d_tree[index + 1])
                {
                    // color all path behind Green, remove until potential path empty
                    for (int i = 0; i < potentialDijkstraPath.Count; i++)
                    {
                        potentialDijkstraPath[i].cellFloor.GetComponent <Renderer>().material.color = Color.green;
                        potentialDijkstraPath.Remove(potentialDijkstraPath[i]);
                    }
                    currentCell.cellFloor.GetComponent <Renderer>().material.color = Color.green;
                    tempCell.cellFloor.GetComponent <Renderer>().material.color    = Color.green;
                    // If final node
                    if (allNodes.IndexOf(tempCell) == d_tree[d_tree.Count - 1])
                    {
                        Debug.Log("Path complete"); break;
                    }
                    index++;
                    FollowDijkstraPath(tempCell, index, Direction.NORTH);
                }
                else     // Not next node in path, backtrack and go West
                {
                    tempCell.cellFloor.GetComponent <Renderer>().material.color = Color.gray;
                    tempIndex = potentialDijkstraPath.Count;

                    //for (int i = 0; i < tempIndex; i++)
                    //{
                    //    potentialDijkstraPath[tempIndex].cellFloor.GetComponent<Renderer>().material.color = Color.gray;
                    //    //yield return new WaitForSeconds(0.1f);
                    //    potentialDijkstraPath.Remove(potentialDijkstraPath[tempIndex]);
                    //    tempIndex--;
                    //}
                    Debug.Log("Dtree at index: " + d_tree[index]);
                    var llc = d_tree[index];
                    FollowDijkstraPath(allNodes[d_tree[index]], index, Direction.WEST);
                }
            }
            else     // Not a node
            {
                currentCell.visited = true;
                potentialDijkstraPath.Add(currentCell);
                FollowDijkstraPath(tempCell, index, Direction.NORTH);
            }
            break;

        case Direction.WEST:
            // If no west neighbor
            // If no north neighbor
            Debug.Log("-------- GOING WEST ------------");
            Debug.Log("Current Cell Index in allNodes: " + allNodes.IndexOf(currentCell));
            Debug.Log("If path, cells index: " + System.Array.IndexOf(cells, currentCell));
            Debug.Log("Using this index: " + index);
            Debug.Log("--------------");
            if (currentCell.west != null || currentCell.northNeighbor.visited)
            {
                FollowDijkstraPath(currentCell, index, Direction.EAST);
            }
            tempCell = currentCell.westNeighbor;
            if (tempCell.isNode)
            {
                tempCell.visited = true;
                // If this is our next Node in path
                if (allNodes.IndexOf(tempCell) == d_tree[index + 1])
                {
                    // color all path behind Green, remove until potential path empty
                    for (int i = 0; i < potentialDijkstraPath.Count; i++)
                    {
                        potentialDijkstraPath[i].cellFloor.GetComponent <Renderer>().material.color = Color.green;
                        potentialDijkstraPath.Remove(potentialDijkstraPath[i]);
                    }
                    tempCell.cellFloor.GetComponent <Renderer>().material.color    = Color.green;
                    currentCell.cellFloor.GetComponent <Renderer>().material.color = Color.green;
                    // If final node
                    if (allNodes.IndexOf(tempCell) == d_tree[d_tree.Count - 1])
                    {
                        Debug.Log("Path complete"); break;
                    }
                    index++;
                    FollowDijkstraPath(tempCell, index, Direction.NORTH);
                }
                else     // Not next node in path, backtrack and go East
                {
                    tempCell.cellFloor.GetComponent <Renderer>().material.color = Color.gray;
                    tempIndex = potentialDijkstraPath.Count;     //-1

                    //if (tempIndex > 0)
                    //{
                    //    while (potentialDijkstraPath.Count > 0)
                    //    {
                    //        potentialDijkstraPath[tempIndex].cellFloor.GetComponent<Renderer>().material.color = Color.gray;
                    //        //yield return new WaitForSeconds(0.1f);
                    //        potentialDijkstraPath.Remove(potentialDijkstraPath[tempIndex]);
                    //        tempIndex--;
                    //    }
                    //}
                    Debug.Log("Dtree at index: " + d_tree[index]);
                    var llc = d_tree[index];
                    FollowDijkstraPath(allNodes[d_tree[index]], index, Direction.EAST);
                }
            }
            else     // Not a node
            {
                currentCell.visited = true;
                potentialDijkstraPath.Add(currentCell);
                FollowDijkstraPath(tempCell, index, Direction.WEST);
            }
            break;

        case Direction.EAST:
            // If no east neighbor
            if (currentCell.east != null || currentCell.northNeighbor.visited)
            {
                FollowDijkstraPath(currentCell, index, Direction.SOUTH);
            }
            tempCell = currentCell.eastNeighbor;
            if (tempCell.isNode)
            {
                tempCell.visited = true;
                // If this is our next Node in path
                if (allNodes.IndexOf(tempCell) == d_tree[index + 1])
                {
                    // color all path behind Green, remove until potential path empty
                    for (int i = 0; i < potentialDijkstraPath.Count; i++)
                    {
                        potentialDijkstraPath[i].cellFloor.GetComponent <Renderer>().material.color = Color.green;
                        potentialDijkstraPath.Remove(potentialDijkstraPath[i]);
                    }
                    tempCell.cellFloor.GetComponent <Renderer>().material.color    = Color.green;
                    currentCell.cellFloor.GetComponent <Renderer>().material.color = Color.green;
                    // If final node
                    if (allNodes.IndexOf(tempCell) == d_tree[d_tree.Count - 1])
                    {
                        Debug.Log("Path complete"); break;
                    }
                    index++;
                    FollowDijkstraPath(tempCell, index, Direction.NORTH);
                }
                else     // Not next node in path, backtrack and go South
                {
                    tempCell.cellFloor.GetComponent <Renderer>().material.color = Color.gray;
                    tempIndex = potentialDijkstraPath.Count;     //-1

                    //if (tempIndex > 0)
                    //{
                    //    while (potentialDijkstraPath.Count > 0)
                    //    {
                    //        potentialDijkstraPath[tempIndex].cellFloor.GetComponent<Renderer>().material.color = Color.gray;
                    //        //yield return new WaitForSeconds(0.1f);
                    //        potentialDijkstraPath.Remove(potentialDijkstraPath[tempIndex]);
                    //        tempIndex--;
                    //    }
                    //}
                    Debug.Log("Dtree at index: " + d_tree[index]);
                    var llc = d_tree[index];
                    FollowDijkstraPath(allNodes[d_tree[index]], index, Direction.SOUTH);
                }
            }
            else     // Not a node
            {
                currentCell.visited = true;
                potentialDijkstraPath.Add(currentCell);
                FollowDijkstraPath(tempCell, index, Direction.EAST);
            }
            break;

        case Direction.SOUTH:
            // If no south neighbor
            if (currentCell.south != null || currentCell.northNeighbor.visited)
            {
                FollowDijkstraPath(currentCell, index, Direction.NORTH);
            }
            tempCell = currentCell.southNeighbor;
            if (tempCell.isNode)
            {
                tempCell.visited = true;
                // If this is our next Node in path
                if (allNodes.IndexOf(tempCell) == d_tree[index + 1])
                {
                    // color all path behind Green, remove until potential path empty
                    for (int i = 0; i < potentialDijkstraPath.Count; i++)
                    {
                        potentialDijkstraPath[i].cellFloor.GetComponent <Renderer>().material.color = Color.green;
                        potentialDijkstraPath.Remove(potentialDijkstraPath[i]);
                    }
                    tempCell.cellFloor.GetComponent <Renderer>().material.color    = Color.green;
                    currentCell.cellFloor.GetComponent <Renderer>().material.color = Color.green;
                    // If final node
                    if (allNodes.IndexOf(tempCell) == d_tree[d_tree.Count - 1])
                    {
                        Debug.Log("Path complete"); break;
                    }
                    index++;
                    FollowDijkstraPath(tempCell, index, Direction.NORTH);
                }
                else     // Not next node in path, backtrack and go North
                {
                    tempCell.cellFloor.GetComponent <Renderer>().material.color = Color.gray;
                    tempIndex = potentialDijkstraPath.Count;     //-1

                    //if (tempIndex > 0)
                    //{
                    //    while (potentialDijkstraPath.Count > 0)
                    //    {
                    //        potentialDijkstraPath[tempIndex].cellFloor.GetComponent<Renderer>().material.color = Color.gray;
                    //        //yield return new WaitForSeconds(0.1f);
                    //        potentialDijkstraPath.Remove(potentialDijkstraPath[tempIndex]);
                    //        tempIndex--;
                    //    }
                    //}
                    Debug.Log("Dtree at index: " + d_tree[index]);
                    var llc = d_tree[index];
                    FollowDijkstraPath(allNodes[d_tree[index]], index, Direction.NORTH);
                }
            }
            else     // Not a node
            {
                currentCell.visited = true;
                potentialDijkstraPath.Add(currentCell);
                FollowDijkstraPath(tempCell, index, Direction.SOUTH);
            }
            break;

        default:
            Debug.Log("Invalid path");
            break;
        }
    }
Beispiel #10
0
    public void Move(Maze.Direction direction, Quaternion rot)
    {
        //回転中は入力を受け付けない
        if (isRotate)
        {
            return;
        }

        var currentPoint = transform.localPosition;
        var nextPoint    = Vector3.zero;
        var rotatePoint  = Vector3.zero;
        var rotateAxis   = Vector3.zero;

        switch (direction)
        {
        case Maze.Direction.Right:
        {
            nextPoint   = currentPoint + rot * new Vector3(cubeSize, 0f, 0f);
            rotatePoint = currentPoint + rot * new Vector3(cubeSizeHalf, -cubeSizeHalf, 0f);
            rotateAxis  = rot * new Vector3(0, 0, -1);
        }
        break;

        case Maze.Direction.Left:
        {
            nextPoint   = currentPoint + rot * new Vector3(-cubeSize, 0f, 0f);
            rotatePoint = currentPoint + rot * new Vector3(-cubeSizeHalf, -cubeSizeHalf, 0f);
            rotateAxis  = rot * new Vector3(0, 0, 1);
        }
        break;

        case Maze.Direction.Down:
        {
            nextPoint   = currentPoint + rot * new Vector3(0f, 0f, -cubeSize);
            rotatePoint = currentPoint + rot * new Vector3(0f, -cubeSizeHalf, -cubeSizeHalf);
            rotateAxis  = rot * new Vector3(-1, 0, 0);
        }
        break;

        case Maze.Direction.Up:
        {
            nextPoint   = currentPoint + rot * new Vector3(0f, 0f, cubeSize);
            rotatePoint = currentPoint + rot * new Vector3(0f, -cubeSizeHalf, cubeSizeHalf);
            rotateAxis  = rot * new Vector3(1, 0, 0);
        }
        break;
        }

        // 入力がない時はコルーチンを呼び出さないようにする
        if (rotatePoint == Vector3.zero)
        {
            return;
        }

        var nextRotation = Quaternion.AngleAxis(90, rotateAxis) * transform.localRotation;
        var nextPos      = nextPoint.ToMazePos();

        // いけるかどうか
        var floor = FloorBehaviour.GetInstance();
        var tile  = floor.Get(nextPos);

        if (tile == null || tile.tileId != GetSideId(nextRotation))
        {
            //var currentPoint = currentPoint;
            //var currentPos = currentPoint.ToMazePos();
            //Debug.LogFormat("Cannot Move\n  Current(pos={0}, id={1}, rot={2})\n  Next(pos={3}, id={4}, rot={5})\n  CurrentTile(pos={6}, id={7})\n  NextTile(pos={8}, id={9})",
            //    currentPoint, GetSideId(transform.localRotation), transform.localRotation.eulerAngles,
            //    nextPoint, GetSideId(nextRotation), nextRotation.eulerAngles,
            //    currentPos, FloorBehaviour.GetInstance().Get(currentPos.x, currentPos.y).tileId,
            //    nextPos, tile.tileId);

            {
                var pos1    = GetCurrentPos().ToCellPos();
                var pos2    = nextPos.ToCellPos();
                var mazepos = new Maze.Cell()
                {
                    X = (pos1.X + pos2.X) / 2, Y = (pos1.Y + pos2.Y) / 2
                }.ToVecMazePos();
                var map = floor.maze.GetMaze();
                if (0 <= mazepos.y && mazepos.y < map.GetLength(1) && 0 <= mazepos.x && mazepos.x < map.GetLength(0))
                {
                    if (map[mazepos.x, mazepos.y] == Maze.Wall)
                    {
                        floor.CreateWall(GetCurrentPos(), nextPos);
                    }
                }
            }

            var modelRenderer = transform.Find("CubeModel").Find("Model").GetComponent <Renderer>();
            var tileId        = CubeBehaviour.GetSideId(CubeBehaviour.GetMoveRotation(direction, transform.localRotation));
            StartCoroutine(UnableMove(rotatePoint, rotateAxis, new ChangeColor[]
            {
                (Color diffuse, Color emission) => { if (tile != null)
                                                     {
                                                         tile.material.color = diffuse; tile.material.SetColor("_EmissionColor", emission);
                                                     }
                },
                (Color diffuse, Color emission) => { modelRenderer.materials[tileId].color = diffuse; modelRenderer.materials[tileId].SetColor("_EmissionColor", emission); },
            }, () => { }));
            GameStats.currentStats.miss++;
            Camera.main.GetComponent <AudioSource>().PlayOneShot(audioMiss);
        }
        else
        {
            var goal  = floor.goal;
            var coins = floor.coins;
            StartCoroutine(MoveCube(rotatePoint, rotateAxis, () =>
            {
                coins.ForEach(e =>
                {
                    if (e.pos == nextPos)
                    {
                        Camera.main.GetComponent <AudioSource>().PlayOneShot(audioCoin);
                        Destroy(e.obj);
                        GameStats.currentStats.coin++;
                    }
                });
                coins.RemoveAll(e => e.pos == nextPos);

                if (nextPos == goal)
                {
                    if (TimeAttack.currentState != null)
                    {
                        onGoalInTimeAttack.Invoke();
                    }
                    else
                    {
                        onGoal.Invoke();
                    }
                }
            }));
        }
    }