private bool StartRotation(GridCube.Direction direction)
    {
        Vector3 rotation;

        switch (direction)
        {
        case GridCube.Direction.UP:
            rotation = new Vector3(-1, 0, 0);
            break;

        case GridCube.Direction.DOWN:
            rotation = new Vector3(1, 0, 0);
            break;

        case GridCube.Direction.LEFT:
            rotation = new Vector3(0, -1, 0);
            break;

        case GridCube.Direction.RIGHT:
            rotation = new Vector3(0, 1, 0);
            break;

        default:
            Debug.LogWarning("Unable to rotate grid!");
            return(false);
        }

        rotationDirection = rotation;
        startTime         = Time.time;
        lastVal           = 0;
        isRotating        = true;
        return(true);
    }
    private bool AreOpposite(GridCube.Direction a, GridCube.Direction b)
    {
        if ((a == GridCube.Direction.DOWN && b == GridCube.Direction.UP) ||
            (a == GridCube.Direction.UP && b == GridCube.Direction.DOWN))
        {
            return(true);
        }

        if ((a == GridCube.Direction.RIGHT && b == GridCube.Direction.LEFT) ||
            (a == GridCube.Direction.LEFT && b == GridCube.Direction.RIGHT))
        {
            return(true);
        }

        return(false);
    }
Exemple #3
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);
    }
    void Update()
    {
        if (!playing)
        {
            return;
        }


        GridCube.Direction dir = ReadInput();

        if (dir == GridCube.Direction.NONE || AreOpposite(dir, lastMovedDirection))
        {
            dir = lastDirection;
        }

        if (lastResult == GameGrid.MoveResult.ROTATING)
        {
            dir = lastMovedDirection;
        }

        lastDirection = dir;

        lastInputTime += Time.deltaTime * speed;
        if (lastInputTime > inputCoolDown)
        {
            lastInputTime = 0;

            GameGrid.MoveResult result = gameGrid.MoveHead(dir);

            if (result == GameGrid.MoveResult.MOVED || result == GameGrid.MoveResult.ATE)
            {
                lastMovedDirection = dir;
            }

            switch (result)
            {
            case GameGrid.MoveResult.DIED:
                playing = false;

                int topScore = PlayerPrefs.GetInt("TopScore", 0);
                if (score > topScore)
                {
                    PlayerPrefs.SetInt("TopScore", score);
                }

                guiController.RemoveNotifications();
                guiController.SetGameOverPanelActive(true);
                break;

            case GameGrid.MoveResult.ERROR:
                Debug.Log("An error occured.");
                gameObject.SetActive(false);
                break;

            case GameGrid.MoveResult.ATE:
                gameGrid.PlaceNewApple();
                if (rotationEnabled && Random.value < NEW_HOLE_PROBABILITY)
                {
                    gameGrid.PlaceNewHole();
                }

                //TODO: Win if no more space is available
                speed += speedFactor;
                score++;
                guiController.SetScore(score);

                inputCoolDown -= COOLDOWN_STEP;
                if (inputCoolDown < MIN_INPUT_COOLDOWN)
                {
                    inputCoolDown = MIN_INPUT_COOLDOWN;
                }

                break;

            case GameGrid.MoveResult.ROTATING:
            default:
                // pass
                break;
            }

            lastResult = result;
        }
    }