Exemplo n.º 1
0
    public bool TrySnakeEatFood(Vector2Int snakeGridPosition)
    {
        // If snake took food0
        if (snakeGridPosition == foodGridPositionArr[0])
        {
            GameSound.PlayGoodFoodSound();
            Object.Destroy(foodGameObjectArr[0]);
            SpawnFood0();
            GameHandler.AddScore(10);
            GameHandler.AddProgressBar();
            GameHandler.SetProgressImage("Good");
            return(true);
        }
        foodTimerArr[0]--;
        if (foodTimerArr[0] <= 0 && (!stopSpawningFoods))   //Relocate food0
        {
            Object.Destroy(foodGameObjectArr[0]);
            SpawnFood0();
            foodTimerArr[0] = 31;
        }


        // If snake took food1
        if (snakeGridPosition == foodGridPositionArr[1])
        {
            GameSound.PlayGoodFoodSound();
            Object.Destroy(foodGameObjectArr[1]);
            SpawnFood1();
            GameHandler.AddScore(10);
            GameHandler.AddProgressBar();
            GameHandler.SetProgressImage("Good");
            return(true);
        }

        foodTimerArr[1]--;
        if (foodTimerArr[1] <= 0 && (!stopSpawningFoods))    //Relocate food1
        {
            Object.Destroy(foodGameObjectArr[1]);
            SpawnFood1();
            foodTimerArr[1] = 22;
        }

        // If snake took Bad food0
        if (snakeGridPosition == badFoodGridPositionArr[0])
        {
            GameSound.PlayBadFoodSound();
            Object.Destroy(badFoodGameObjectArr[0]);
            SpawnBad0();
            GameHandler.ReduceScore(10);
            GameHandler.ReduceProgressBar();
            GameHandler.SetProgressImage("Bad");
            return(true);
        }
        badFoodTimerArr[0]--;
        if (badFoodTimerArr[0] <= 0 && (!stopSpawningFoods))  //Relocate bad food0
        {
            Object.Destroy(badFoodGameObjectArr[0]);
            SpawnBad0();
            badFoodTimerArr[0] = 28;
        }

        // If snake took Bad food1
        if (snakeGridPosition == badFoodGridPositionArr[1])
        {
            GameSound.PlayBadFoodSound();
            Object.Destroy(badFoodGameObjectArr[1]);
            SpawnBad1();
            GameHandler.ReduceScore(10);
            GameHandler.ReduceProgressBar();
            GameHandler.SetProgressImage("Bad");
            return(true);
        }

        badFoodTimerArr[1]--;
        if (badFoodTimerArr[1] <= 0 && (!stopSpawningFoods))  //Relocate bad food1
        {
            Object.Destroy(badFoodGameObjectArr[1]);
            SpawnBad1();
            badFoodTimerArr[1] = 16;
        }
        ////////////////////////////////////////////
        //     Prize handler  //
        //If took Prise0
        if (snakeGridPosition == prizeGridPositionArr[0])
        {
            Object.Destroy(prizeGameObjectArr[0]);
            GameHandler.prizesLeftToWin--;
            return(true);
        }
        //If took Prise1
        if (snakeGridPosition == prizeGridPositionArr[1])
        {
            Object.Destroy(prizeGameObjectArr[1]);
            GameHandler.prizesLeftToWin--;
            return(true);
        }

        return(false);//Ate nothing
    }
Exemplo n.º 2
0
    private void HandleGridMovement()
    {
        gridMoveTimer += Time.deltaTime;
        if (gridMoveTimer >= gridMoveTimerMax)
        {
            gridMoveTimer -= gridMoveTimerMax;


            if (!GameHandler.isPrizesSpwaned)
            {
                GameHandler.SetProgressTimer();
            }
            else
            {
                GameHandler.SetProgressTimerToMax();
            }



            //Setting start size
            if (GameHandler.startSize > 0)
            {
                snakeBodySize++;
                CreateSnakeBodyPart();
                GameHandler.startSize--;
            }

            SoundManager.PlaySound(SoundManager.Sound.SnakeMove);

            SnakeMovePosition previousSnakeMovePosition = null;
            if (snakeMovePositionList.Count > 0)
            {
                previousSnakeMovePosition = snakeMovePositionList[0];
            }

            SnakeMovePosition snakeMovePosition = new SnakeMovePosition(previousSnakeMovePosition, gridPosition, gridMoveDirection);
            snakeMovePositionList.Insert(0, snakeMovePosition);

            Vector2Int gridMoveDirectionVector;

            switch (gridMoveDirection)
            {
            default:
            case Direction.Right:   gridMoveDirectionVector = new Vector2Int(+1, 0); break;

            case Direction.Left:    gridMoveDirectionVector = new Vector2Int(-1, 0); break;

            case Direction.Up:      gridMoveDirectionVector = new Vector2Int(0, +1); break;

            case Direction.Down:    gridMoveDirectionVector = new Vector2Int(0, -1); break;
            }

            gridPosition += gridMoveDirectionVector;

            gridPosition = levelGrid.ValidateGridPosition(gridPosition);

            bool snakeAteFood = levelGrid.TrySnakeEatFood(gridPosition);
            if (snakeAteFood)
            {
                // Snake ate food, grow body
                snakeBodySize++;
                CreateSnakeBodyPart();
                SoundManager.PlaySound(SoundManager.Sound.SnakeEat);

                if (GameHandler.curProgPart <= 8 && GameSound.happyIsPlayed())
                {
                    GameSound.StopHappy();
                    GameSound.PlaySad();
                }

                else if (GameHandler.curProgPart > 8 && GameSound.sadIsPlayed())
                {
                    GameSound.StopSad();
                    GameSound.PlayHappy();
                }


                if (GameHandler.prizesLeftToWin <= 0)
                {
                    state = State.Dead;
                    GameHandler.OnWin();
                    SoundManager.PlaySound(SoundManager.Sound.SnakeDie);//TODO Change it to winning music
                }
            }
            // On Win - Clear evrything on field (do funtion in levelgrid), then summon final prizes
            if (GameHandler.curProgPart > 16 && !GameHandler.isPrizesSpwaned)
            {
                GameHandler.isPrizesSpwaned = true;
                levelGrid.DestroyAll();
                levelGrid.SpawnPrizes();
            }

            updateSizeCounter++;
            if (updateSizeCounter >= 10)
            {
                snakeBodySize++;
                CreateSnakeBodyPart();
                updateSizeCounter = 0;
            }

            if (snakeMovePositionList.Count >= snakeBodySize + 1)
            {
                snakeMovePositionList.RemoveAt(snakeMovePositionList.Count - 1);
            }

            UpdateSnakeBodyParts();

            foreach (SnakeBodyPart snakeBodyPart in snakeBodyPartList)
            {
                Vector2Int snakeBodyPartGridPosition = snakeBodyPart.GetGridPosition();
                if (gridPosition == snakeBodyPartGridPosition)
                {
                    //Game Over!
                    state = State.Dead;
                    GameHandler.SnakeDied();
                    SoundManager.PlaySound(SoundManager.Sound.SnakeDie);
                }
            }


            transform.position    = new Vector3(gridPosition.x, gridPosition.y);
            transform.eulerAngles = new Vector3(0, 0, GetAngleFromVector(gridMoveDirectionVector) - 90);



            GameHandler.AddScore(1);
            GameHandler.AddProgressBar();
        }
    }