Esempio n. 1
0
 void Start()
 {
     // Move the Snake every 300ms
     InvokeRepeating("Move", 0.3f, 0.3f);
     snakeDirection = currentDirection.right;
 }
Esempio n. 2
0
    void Update()
    {
        currentSnakeX = (int)transform.position.x;
        currentSnakeY = (int)transform.position.y;

        /*     Debug.DrawLine(new Vector2(currentSnakeX, currentSnakeY), new Vector2(currentSnakeX + 1, currentSnakeY), Color.red);
         *   Debug.DrawLine(new Vector2(currentSnakeX, currentSnakeY), new Vector2(currentSnakeX, currentSnakeY + 1), Color.red);
         *   Debug.DrawLine(new Vector2(currentSnakeX, currentSnakeY), new Vector2(currentSnakeX, currentSnakeY - 1), Color.red); */

        switch (snakeDirection)
        {
        case currentDirection.right:
            // Move right
            if (currentDistanceToFood(currentSnakeX + 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY + 1) &&
                currentDistanceToFood(currentSnakeX + 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY - 1))
            {
                dir            = Vector2.right;
                snakeDirection = currentDirection.right;
            }

            // Move Down
            else if (currentDistanceToFood(currentSnakeX, currentSnakeY - 1) < currentDistanceToFood(currentSnakeX + 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX, currentSnakeY - 1) < currentDistanceToFood(currentSnakeX, currentSnakeY + 1))
            {
                dir            = -Vector2.up;
                snakeDirection = currentDirection.down;
            }

            // Move Up
            else if (currentDistanceToFood(currentSnakeX, currentSnakeY + 1) < currentDistanceToFood(currentSnakeX + 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX, currentSnakeY + 1) < currentDistanceToFood(currentSnakeX, currentSnakeY - 1))
            {
                dir            = Vector2.up;
                snakeDirection = currentDirection.up;
            }
            break;

        case currentDirection.left:
            // Move left
            if (currentDistanceToFood(currentSnakeX - 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY + 1) &&
                currentDistanceToFood(currentSnakeX - 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY - 1))
            {
                dir            = Vector2.left;
                snakeDirection = currentDirection.left;
            }

            // Move Up
            else if (currentDistanceToFood(currentSnakeX, currentSnakeY + 1) < currentDistanceToFood(currentSnakeX - 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX, currentSnakeY + 1) < currentDistanceToFood(currentSnakeX, currentSnakeY - 1))
            {
                dir            = Vector2.up;
                snakeDirection = currentDirection.up;
            }

            // Move down
            else if (currentDistanceToFood(currentSnakeX, currentSnakeY - 1) < currentDistanceToFood(currentSnakeX - 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX, currentSnakeY - 1) < currentDistanceToFood(currentSnakeX, currentSnakeY + 1))
            {
                dir            = -Vector2.up;
                snakeDirection = currentDirection.down;
            }
            break;

        case currentDirection.up:
            // Move left
            if (currentDistanceToFood(currentSnakeX - 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY + 1) &&
                currentDistanceToFood(currentSnakeX - 1, currentSnakeY) < currentDistanceToFood(currentSnakeX + 1, currentSnakeY))
            {
                dir            = Vector2.left;
                snakeDirection = currentDirection.left;
            }

            // Move Up
            else if (currentDistanceToFood(currentSnakeX, currentSnakeY + 1) < currentDistanceToFood(currentSnakeX + 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX, currentSnakeY + 1) < currentDistanceToFood(currentSnakeX - 1, currentSnakeY))
            {
                dir            = Vector2.up;
                snakeDirection = currentDirection.up;
            }

            // Move right
            else if (currentDistanceToFood(currentSnakeX + 1, currentSnakeY) < currentDistanceToFood(currentSnakeX - 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX + 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY + 1))
            {
                dir            = Vector2.right;
                snakeDirection = currentDirection.right;
            }
            break;

        case currentDirection.down:
            // Move left
            if (currentDistanceToFood(currentSnakeX - 1, currentSnakeY) < currentDistanceToFood(currentSnakeX + 1, currentSnakeY) &&
                currentDistanceToFood(currentSnakeX - 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY - 1))
            {
                dir            = Vector2.left;
                snakeDirection = currentDirection.left;
            }

            // Move Down
            else if (currentDistanceToFood(currentSnakeX, currentSnakeY - 1) < currentDistanceToFood(currentSnakeX + 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX, currentSnakeY - 1) < currentDistanceToFood(currentSnakeX - 1, currentSnakeY))
            {
                dir            = -Vector2.up;
                snakeDirection = currentDirection.down;
            }

            // Move right
            else if (currentDistanceToFood(currentSnakeX + 1, currentSnakeY) < currentDistanceToFood(currentSnakeX - 1, currentSnakeY) &&
                     currentDistanceToFood(currentSnakeX + 1, currentSnakeY) < currentDistanceToFood(currentSnakeX, currentSnakeY - 1))
            {
                dir            = Vector2.right;
                snakeDirection = currentDirection.right;
            }
            break;
        }
    }
Esempio n. 3
0
 Direction.Right => ((currentPosition.Item1, currentPosition.Item2), this.ApplyShipRight(currentDirection, instruction.Value)),