예제 #1
0
    private void HandleCollision()
    {
        //Debug.Log("MySnakePosition:" + mySnakePartArray[0].getPosition());
        //Debug.Log("My Food Position:" + myFood.getPosition());



        if (Mathf.Abs(mySnakePartArray[0].getPosition().x - myFood.getPosition().x) < 0.01 && Mathf.Abs(mySnakePartArray[0].getPosition().y - myFood.getPosition().y) < 0.1)
        {
            myFood.Destroy();
            mySnakePartArray = SnakeMovement.Grow(mySnakePartArray, snakeDirection, myFood.getPosition(), myScale);


            myFood.SpawnRandomSpot(mainBackgroundWidth, mainBackgroundHeight, myScale, mySnakePartArray);
            FindObjectOfType <AudioHandler>().Play(AudioHandler.SoundName.SnakeAte);
            Debug.Log("Snek ate food!");
        }

        for (int i = 0; i < mySnakePartArray.Length - 1; i++) // handles collision between snake itself
        {
            if (Mathf.Abs(mySnakePartArray[0].getPosition().x - mySnakePartArray[i + 1].getPosition().x) < 0.1 && Mathf.Abs(mySnakePartArray[0].getPosition().y - mySnakePartArray[i + 1].getPosition().y) < 0.1)
            {
                Debug.Log("snek collided with itself!");
                GameOver();
            }
        }
        if (mySnakePartArray[0].getPosition().x > mainBackgroundWidth / 2 || mySnakePartArray[0].getPosition().x <-mainBackgroundWidth / 2 || mySnakePartArray[0].getPosition().y> mainBackgroundHeight / 2 || mySnakePartArray[0].getPosition().y < -mainBackgroundHeight / 2)
        // handles whether the snake is within bounds.
        {
            Debug.Log("snek out of bounds! it's on the loose!");
            GameOver();
        }
    }