Exemple #1
0
    private void ToEat()
    {
        justAte = true;
        eatedFoods++;

        //check eated foods to increase speed
        if (eatedFoods % 3 == 0)
        {
            movementTimeoutLevel = Mathf.Clamp(movementTimeoutLevel + 1, Constants.MIN_LEVEL, Constants.MAX_LEVEL);
            SetMovementTimeout(movementTimeoutLevel);
        }

        //get the last body and tail
        int       tailIndex     = snakeParts.Count - 1;
        int       lastBodyIndex = snakeParts.Count - 2;
        SnakePart tail          = snakeParts[tailIndex];
        SnakePart lastBody      = snakeParts[lastBodyIndex];

        //get position and rotation for new body part
        Vector2Int pos = Vector2Int.FloorToInt(lastBody.rb.position);
        Quaternion rot = lastBody.transform.rotation;

        //get directions for new body part
        ScriptableDirection from = snakeParts[tailIndex].currentDirection;
        ScriptableDirection to   = snakeParts[lastBodyIndex].currentDirection;

        //spawning and setting up the new body part
        SnakePart newPart = snakePool.Spawn <SnakePart> (pos, Quaternion.identity);

        newPart.currentDirection = from;
        newPart.UpdateSnakePart(pos, to, from);
        snakeParts.Insert(lastBodyIndex, newPart);
        onEetFood.Call();
    }
Exemple #2
0
    private void SnakeUpdate()
    {
        SnakePart tail         = snakeParts[snakeParts.Count - 1];
        SnakePart lastBodyPart = snakeParts[snakeParts.Count - 2];

        //Update Tail
        if (!justAte)
        {
            grid.AddAvaliableCell(Vector2Int.FloorToInt(tail.rb.position));
            Vector2Int tailPosition = Vector2Int.FloorToInt(lastBodyPart.rb.position);
            tail.UpdateSnakePart(tailPosition, lastBodyPart.currentDirection);
        }
        else
        {
            justAte = false;
        }

        //update Neck
        lastBodyPart.UpdateSnakePart(Vector2Int.FloorToInt(rb.position), nextDirection, currentDirection);
        snakeParts.Remove(lastBodyPart);
        snakeParts.Insert(0, lastBodyPart);

        //Update head
        Vector2Int headPosition = Vector2Int.FloorToInt(rb.position) + nextDirection.Value;

        UpdateSnakePart(headPosition, nextDirection, currentDirection);
        grid.RemoveAvaliableCell(headPosition);
    }