예제 #1
0
    private void PlaceNewObject(GridCube.CubeState state)
    {
        bool done = false;

        while (!done)
        {
            GridCube cube = cubes.ElementAt(Random.Range(0, cubes.Count));
            if (!cube.isEmpty() || (cube.SameSideAs(SnakeHead()) && rotationEnabled))
            {
                continue;
            }

            cube.SetCubeState(state);
            done = true;
        }
    }
예제 #2
0
    public MoveResult MoveHead(GridCube.Direction direction)
    {
        if (isRotating)
        {
            return(MoveResult.ROTATING);
        }

        GridCube next = SnakeHead().GetNextCube(direction, out var changedSide);

        if (next == null)
        {
            next        = FindSnakeBound(SnakeHead());
            changedSide = false;
            if (next == null)
            {
                return(MoveResult.ERROR);
            }
        }

        if (next.IsSnake() || next.IsHole())
        {
            return(MoveResult.DIED);
        }

        if (changedSide)
        {
            bool ok = StartRotation(direction);
            return(ok ? MoveResult.ROTATING : MoveResult.ERROR);
        }

        bool ateApple = next.IsApple();

        next.SetCubeState(GridCube.CubeState.SNAKE);
        snake.AddFirst(next);

        GridCube last = snake.Last.Value;

        if (!ateApple)
        {
            last.SetCubeState(GridCube.CubeState.EMPTY);
            snake.RemoveLast();
            return(MoveResult.MOVED);
        }

        return(MoveResult.ATE);
    }
예제 #3
0
    public void SetupGrid(bool enableRotation, int appleCount)
    {
        if (cubes != null)
        {
            foreach (GridCube c in cubes)
            {
                Destroy(c.gameObject);
            }
        }

        snake           = new LinkedList <GridCube>();
        cubes           = new List <GridCube>();
        isRotating      = false;
        rotationEnabled = enableRotation;

        if (gridSize % 2 == 0)
        {
            gridSize++;
        }

        gridSize = Mathf.Max(gridSize, 5);

        float finalGridSize = GetGridSizeWorld();
        float halfGridSize  = finalGridSize / 2;

        int zDepth = rotationEnabled ? gridSize : 1;

        for (int i = 0; i < gridSize; i++)
        {
            for (int j = 0; j < gridSize; j++)
            {
                for (int k = 0; k < zDepth; k++)
                {
                    // Dont add cubes at center of 3d grid
                    if ((k != 0 && k != gridSize - 1) && (j != 0 && j != gridSize - 1) && (i != 0 && i != gridSize - 1))
                    {
                        continue;
                    }

                    GameObject cubeGameObject = Instantiate(gridCubeClone);
                    cubeGameObject.transform.SetParent(transform);

                    Vector3 size   = cubeGameObject.transform.localScale;
                    float   offset = halfGridSize - size.x / 2;
                    cubeGameObject.transform.Translate(i * size.x - offset, j * size.x - offset, k * size.x - offset);

                    int      centerPos = (int)halfGridSize;
                    GridCube cube      = cubeGameObject.GetComponent <GridCube>();

                    if (i == centerPos && j == centerPos && k == 0)
                    {
                        // Set up starting cell
                        cube.SetCubeState(GridCube.CubeState.SNAKE);
                        snake.AddFirst(cube);
                    }
                    else
                    {
                        cube.SetCubeState(GridCube.CubeState.EMPTY);
                    }

                    if (i == 0)
                    {
                        cube.AddCubeSide(GridCube.CubeSide.LEFT);
                    }
                    else if (i == gridSize - 1)
                    {
                        cube.AddCubeSide(GridCube.CubeSide.RIGHT);
                    }

                    if (j == 0)
                    {
                        cube.AddCubeSide(GridCube.CubeSide.BOTTOM);
                    }
                    else if (j == gridSize - 1)
                    {
                        cube.AddCubeSide(GridCube.CubeSide.TOP);
                    }

                    if (k == 0)
                    {
                        cube.AddCubeSide(GridCube.CubeSide.FRONT);
                    }
                    else if (k == gridSize - 1)
                    {
                        cube.AddCubeSide(GridCube.CubeSide.BACK);
                    }

                    cubes.Add(cube);
                }
            }
        }

        for (int i = 0; i < appleCount; i++)
        {
            PlaceNewApple();
        }
    }