Ejemplo n.º 1
0
    /// <summary>
    /// Moves snake to new position according to direction of movement.
    /// </summary>
    /// <param name="direction">direction of movement</param>
    /// <param name="extend">if true snake will be extended by one segment</param>
    public void Move(Vector2Int direction)
    {
        var newHead = NextHeadPosition(direction);


        if (!LogicElement.IsWalkable(board[newHead].Content))
        {
            return;
        }

        bool extend = LogicElement.IsExtend(board[newHead].Content);

        body.AddLast(newHead);


        if (extend)
        {
            // body.AddLast(newHead);
        }
        else
        {
            board[body.First.Value].SetTile(LogicElement.LogicElementType.None, true);
            body.RemoveFirst();
        }

        UpdateSnakeState();
    }
Ejemplo n.º 2
0
    public Vector2Int GetDirection()
    {
        List <Vector2Int> avaliableDirections = new List <Vector2Int>
        {
            Vector2Int.left,
            Vector2Int.right,
            Vector2Int.up
        };

        List <Vector2Int> worldAvaliableDirections = new List <Vector2Int>();

        //remove not walkable tiles
        for (int i = 2; i >= 0; i--)
        {
            Vector2Int dir      = avaliableDirections[i];
            Vector2Int worldDir = HeadToBoard(this, dir);

            //Debug.Log((Head + worldDir)+" "+ board[Head + worldDir].Content);
            if (LogicElement.IsWalkable(board[Head + worldDir].Content))
            {
                worldAvaliableDirections.Add(worldDir);
            }
        }

        //return if ther is no walkable tiles near
        if (worldAvaliableDirections.Count == 0)
        {
            return(Vector2Int.zero);
        }

        worldAvaliableDirections = GetDirectionByModules(worldAvaliableDirections, this);


        int randomValue = Mathf.RoundToInt(UnityEngine.Random.Range(0, worldAvaliableDirections.Count));

        return(worldAvaliableDirections[randomValue]);
    }