Beispiel #1
0
        //Move a cobra
        void Move()
        {
            if (desiredDirection != GlobalEnums.E_Direction.None)
            {
                Vector2Int newSnakeHeadPos = Vector2Int.zero;

                newSnakeHeadPos = snakeBody[0] + GetVectorDirection(desiredDirection);

                Vector2Int tailPos = snakeBody[snakeBody.Count - 1];

                for (int i = snakeBody.Count - 1; i > 0; i--)
                {
                    snakeBody[i] = snakeBody[i - 1];
                }
                if (bodyPartsAdded > 0)
                {
                    snakeBody.Add(tailPos);
                    bodyPartsAdded--;
                }
                snakeBody[0] = newSnakeHeadPos;
                EventManager.BroadcastEvent("SnakeMoved", new Hashtable {
                    { "Pos", newSnakeHeadPos }
                });
            }
            display.UpdateSprites(snakeBody, desiredDirection);
            lastDirection = desiredDirection;
        }
Beispiel #2
0
        //Recebe o evento de botao de movimentacao pressionado
        void MovementKeyPressed(Hashtable eventParam)
        {
            if (eventParam == null)
            {
                return;
            }
            if (eventParam.ContainsKey("Code"))
            {
                switch (eventParam["Code"])
                {
                case "Left":
                    if (lastDirection != GlobalEnums.E_Direction.Right)
                    {
                        desiredDirection = GlobalEnums.E_Direction.Left;
                        if (moveOnKeyPress && canMove)
                        {
                            Move(); moveTimer = 0;
                        }
                    }
                    break;

                case "Right":
                    if (lastDirection != GlobalEnums.E_Direction.Left)
                    {
                        desiredDirection = GlobalEnums.E_Direction.Right;
                        if (moveOnKeyPress && canMove)
                        {
                            Move(); moveTimer = 0;
                        }
                    }
                    break;

                case "Up":
                    if (lastDirection != GlobalEnums.E_Direction.Down)
                    {
                        desiredDirection = GlobalEnums.E_Direction.Up;
                        if (moveOnKeyPress && canMove)
                        {
                            Move(); moveTimer = 0;
                        }
                    }

                    break;

                case "Down":
                    if (lastDirection != GlobalEnums.E_Direction.Up)
                    {
                        desiredDirection = GlobalEnums.E_Direction.Down;
                        if (moveOnKeyPress && canMove)
                        {
                            Move(); moveTimer = 0;
                        }
                    }
                    break;
                }
            }
        }
Beispiel #3
0
 //Reinicia a cobra
 public void ResetSnake(Vector2Int pos)
 {
     canMove = false;
     if (snakeBody != null)
     {
         snakeBody.Clear();
     }
     snakeBody = new List <Vector2Int>();
     snakeBody.Add(pos);
     snakeBody.Add(new Vector2Int(pos.x, pos.y - 1));
     desiredDirection = GlobalEnums.E_Direction.None;
     lastDirection    = GlobalEnums.E_Direction.None;
     display.UpdateSprites(snakeBody, GlobalEnums.E_Direction.Up);
 }
Beispiel #4
0
        [SerializeField] Pool spritesPool = default;                          //Referencia para a piscina de objetos

        //Atualiza a cobra na tela
        public void UpdateSprites(List <Vector2Int> bodyParts, GlobalEnums.E_Direction headDirection)
        {
            if (bodyParts.Count == 0)
            {
                return;
            }

            //Garantindo que temos objetos suficientes
            while (bodyParts.Count > displayedSprites.Count)
            {
                GameObject     obj    = spritesPool.GetObjFromPool();
                SpriteRenderer render = obj.GetComponent <SpriteRenderer>();
                if (render == null)
                {
                    Debug.LogWarning("No Sprite Render Found on pool object."); return;
                }
                displayedSprites.Add(render);
                obj.SetActive(true);
                obj.transform.SetParent(transform);
            }

            //Garantindo que nao temos mais objetos que precisamos
            while (bodyParts.Count < displayedSprites.Count)
            {
                spritesPool.ReturnObjToPool(displayedSprites[displayedSprites.Count - 1].gameObject);
                displayedSprites.RemoveAt(displayedSprites.Count - 1);
            }

            //Cabeca
            displayedSprites[0].transform.position = new Vector3(bodyParts[0].x - boardCenterDisplacement.x, bodyParts[0].y - boardCenterDisplacement.y, 0);
            displayedSprites[0].transform.rotation = RotationFromDirection(headDirection);
            if (displayedSprites[0].sprite == null)
            {
                displayedSprites[0].sprite = SkinController.instance.selectedSkin.snakeSkin.headSprite;
            }

            //Corpo e rabo
            for (int i = 1; i < bodyParts.Count; i++)
            {
                displayedSprites[i].transform.position = new Vector3(bodyParts[i].x - boardCenterDisplacement.x, bodyParts[i].y - boardCenterDisplacement.y, 0);
                SetBodySpriteAndRotation(bodyParts, i);
            }
        }
Beispiel #5
0
        //Pega uma direcao e retorna o vetor da direcao
        Vector2Int GetVectorDirection(GlobalEnums.E_Direction direction)
        {
            switch (direction)
            {
            case GlobalEnums.E_Direction.Left:
                return(Vector2Int.left);

            case GlobalEnums.E_Direction.Right:
                return(Vector2Int.right);

            case GlobalEnums.E_Direction.Up:
                return(Vector2Int.up);

            case GlobalEnums.E_Direction.Down:
                return(Vector2Int.down);

            default:
                return(Vector2Int.zero);
            }
        }
Beispiel #6
0
        Quaternion RotationFromDirection(GlobalEnums.E_Direction direction)
        {
            switch (direction)
            {
            case GlobalEnums.E_Direction.Up:
                return(Quaternion.Euler(0, 0, 0));

            case GlobalEnums.E_Direction.Down:
                return(Quaternion.Euler(0, 0, 180));

            case GlobalEnums.E_Direction.Left:
                return(Quaternion.Euler(0, 0, 90));

            case GlobalEnums.E_Direction.Right:
                return(Quaternion.Euler(0, 0, -90));

            default:
                return(Quaternion.Euler(0, 0, 0));
            }
        }
Beispiel #7
0
        void SetBodySpriteAndRotation(List <Vector2Int> bodyParts, int i)
        {
            GlobalEnums.E_Direction direction = GlobalEnums.E_Direction.None;

            //Calcula a direcao do sprite
            if (bodyParts[i].x < bodyParts[i - 1].x)
            {
                direction = GlobalEnums.E_Direction.Right;
            }
            else if (bodyParts[i].x > bodyParts[i - 1].x)
            {
                direction = GlobalEnums.E_Direction.Left;
            }
            else if (bodyParts[i].y > bodyParts[i - 1].y)
            {
                direction = GlobalEnums.E_Direction.Down;
            }
            else if (bodyParts[i].y < bodyParts[i - 1].y)
            {
                direction = GlobalEnums.E_Direction.Up;
            }

            if (i != bodyParts.Count - 1)
            {
                //Seguindo Reto
                if ((bodyParts[i - 1].x <bodyParts[i].x && bodyParts[i + 1].x> bodyParts[i].x) ||
                    (bodyParts[i + 1].x <bodyParts[i].x && bodyParts[i - 1].x> bodyParts[i].x) ||
                    (bodyParts[i - 1].y <bodyParts[i].y && bodyParts[i + 1].y> bodyParts[i].y) ||
                    (bodyParts[i + 1].y <bodyParts[i].y && bodyParts[i - 1].y> bodyParts[i].y))
                {
                    displayedSprites[i].sprite             = SkinController.instance.selectedSkin.snakeSkin.bodySprite;
                    displayedSprites[i].transform.rotation = RotationFromDirection(direction);
                }

                //Cima Esquerda ou Direita Baixo
                else if ((bodyParts[i - 1].y < bodyParts[i].y && bodyParts[i + 1].x < bodyParts[i].x) || (bodyParts[i + 1].y < bodyParts[i].y && bodyParts[i - 1].x < bodyParts[i].x))
                {
                    displayedSprites[i].sprite             = SkinController.instance.selectedSkin.snakeSkin.bodyTurnSprite;
                    displayedSprites[i].transform.rotation = Quaternion.Euler(0, 0, -90);
                }

                //Cima - Direita ou Esquerda - Baixo
                else if ((bodyParts[i - 1].x > bodyParts[i].x && bodyParts[i + 1].y < bodyParts[i].y) || (bodyParts[i + 1].x > bodyParts[i].x && bodyParts[i - 1].y < bodyParts[i].y))
                {
                    displayedSprites[i].sprite             = SkinController.instance.selectedSkin.snakeSkin.bodyTurnSprite;
                    displayedSprites[i].transform.rotation = Quaternion.Euler(0, 0, 0);
                }

                //Baixo - Esquerda ou Direita - Cima
                else if ((bodyParts[i - 1].x <bodyParts[i].x && bodyParts[i + 1].y> bodyParts[i].y) || (bodyParts[i + 1].x <bodyParts[i].x && bodyParts[i - 1].y> bodyParts[i].y))
                {
                    displayedSprites[i].sprite             = SkinController.instance.selectedSkin.snakeSkin.bodyTurnSprite;
                    displayedSprites[i].transform.rotation = Quaternion.Euler(0, 0, -180);
                }

                //Baixo - Direita ou Esquerda - Cima
                else if ((bodyParts[i - 1].y > bodyParts[i].y && bodyParts[i + 1].x > bodyParts[i].x) || (bodyParts[i + 1].y > bodyParts[i].y && bodyParts[i - 1].x > bodyParts[i].x))
                {
                    displayedSprites[i].sprite             = SkinController.instance.selectedSkin.snakeSkin.bodyTurnSprite;
                    displayedSprites[i].transform.rotation = Quaternion.Euler(0, 0, -270);
                }
            }
            //Rabo
            else
            {
                displayedSprites[i].sprite             = SkinController.instance.selectedSkin.snakeSkin.tailSprite;
                displayedSprites[i].transform.rotation = RotationFromDirection(direction);
            }
        }