Ejemplo n.º 1
0
        public static MoveDirectionTypesEnum FindMoveDirection(Vector3 a, Vector3 b)
        {
            MoveDirectionTypesEnum moveDirection = MoveDirectionTypesEnum.None;

            float angle = Mathf.Atan2(b.y - a.y, b.x - a.x) * 180 / Mathf.PI;

            if (angle > -45 && angle <= 45)
            {
                moveDirection = MoveDirectionTypesEnum.Right;
            }
            else if (angle > 45 && angle <= 135)
            {
                moveDirection = MoveDirectionTypesEnum.Up;
            }
            else if (angle > 135 || angle <= -135)
            {
                moveDirection = MoveDirectionTypesEnum.Left;
            }
            else if (angle >= -135 && angle < -45)
            {
                moveDirection = MoveDirectionTypesEnum.Down;
            }

            return(moveDirection);
        }
        public void OnEvent(EventTypesEnum eventTypeEnum, object messageData)
        {
            switch (eventTypeEnum)
            {
            case EventTypesEnum.LMB_Down:
                _clickA = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                break;

            case EventTypesEnum.LMB_Up:
                _clickB = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                if (_gameStateEnum == GameStatesEnum.Ready)
                {
                    if (_clickA.x >= 0 && _clickA.x < _board.Width && _clickA.y >= 0 && _clickA.y < _board.Height &&
                        (Mathf.Abs(_clickB.x - _clickA.x) > Strings.SWIPE_SENSITIVITY ||
                         Mathf.Abs(_clickB.y - _clickA.y) > Strings.SWIPE_SENSITIVITY))
                    {
                        MoveDirectionTypesEnum swipeDirection = Helper.FindMoveDirection(_clickA, _clickB);
                        SwipeCells(swipeDirection);
                    }
                }

                break;

            case EventTypesEnum.Swipe:
                _gameStateEnum = GameStatesEnum.Wait;

                SetMacroCommand((ICommand[])messageData);
                ExecuteMacroCommand();
                break;

            case EventTypesEnum.BOARD_collapse:
                ExecuteMacroCommand();
                break;

            case EventTypesEnum.BOARD_EndDestroyMatchedCells:
                if (_powersDictionary.Count > 0)
                {
                    Vector2          pos             = _powersDictionary.First().Key;
                    PowerUpTypesEnum powerUpTypeEnum = _powersDictionary.First().Value;

                    List <ICell> cellsList = new List <ICell>(_checkManager.PowerCheck(powerUpTypeEnum, pos));
                    ICell        cell      = _board.Cells[(int)pos.x, (int)pos.y];

                    _matchedCellsDictionary.Add(cellsList, AxisTypesEnum.Undefined);
                    _matchedCellsWithAxisDictionary.Add(cell, _matchedCellsDictionary);

                    _powersDictionary.Remove(_powersDictionary.First());

                    StartCoroutine(MarkAndDestroy(_matchedCellsWithAxisDictionary));
                }
                else
                {
                    StartCoroutine(RefillBoard());
                }

                break;

            case EventTypesEnum.CELL_EndMove:
                TryCheckSwipedCells((ICell)messageData);
                break;

            case EventTypesEnum.CELL_EndMoveBack:
                ICell cellBack = (ICell)messageData;

                _board.Cells[cellBack.TargetX, cellBack.TargetY] = cellBack;
                cellBack.CellStateEnum = CellStatesEnum.Wait;

                _gameStateEnum = GameStatesEnum.Ready;
                break;

            case EventTypesEnum.CELL_Fall:
                ICell cellFall = (ICell)messageData;

                cellFall.CellStateEnum = CellStatesEnum.Wait;

                if (cellFall == _lastFallCell)
                {
                    CheckBoard();
                }
                break;

            case EventTypesEnum.CELL_Destroy:
                string cellTag = (string)messageData;
                Notify(EventTypesEnum.CELL_Destroy, cellTag);
                break;

            case EventTypesEnum.POWER_Use:
                ArrayList arr = (ArrayList)messageData;

                PowerUpTypesEnum powerUp  = Helper.StringToPowerType(arr[0].ToString());
                Vector3          position = (Vector3)arr[1];

                _powersDictionary.Add(position, powerUp);
                break;

            case EventTypesEnum.TASK_Finished:
                if (_gameStateEnum != GameStatesEnum.End)
                {
                    _navigationManager.MasterManager.UpdateManager.IsUpdate = false;
                    _gameStateEnum = GameStatesEnum.End;
                    Notify(EventTypesEnum.TASK_Finished, null);
                }
                break;

            default:
                Debug.Log("EVENT NOT FOUND");
                break;
            }
        }
        private void SwipeCells(MoveDirectionTypesEnum direction)
        {
            int xPos = (int)Mathf.Round(_clickA.x);
            int yPos = (int)Mathf.Round(_clickA.y);

            ICell cellA = _board.Cells[xPos, yPos];

            if (cellA.CellTypeEnum == CellTypesEnum.Normal && cellA != null && cellA.CurrentGameObject != null)
            {
                switch (direction)
                {
                case MoveDirectionTypesEnum.Up:
                    if (yPos < _board.Height - 1)
                    {
                        ICell cellB = _board.Cells[xPos, yPos + 1];
                        if (cellB.CellTypeEnum == CellTypesEnum.Normal && cellB.CurrentGameObject != null)
                        {
                            _board.Cells[xPos, yPos + 1] = cellA;
                            _board.Cells[xPos, yPos]     = cellB;

                            ICommand[] commands = { new SwipeUpCommand(cellA), new SwipeDownCommand(cellB), };

                            OnEvent(EventTypesEnum.Swipe, commands);
                        }
                    }

                    break;

                case MoveDirectionTypesEnum.Down:
                    if (yPos > 0)
                    {
                        ICell cellB = _board.Cells[xPos, yPos - 1];
                        if (cellB.CellTypeEnum == CellTypesEnum.Normal && cellB.CurrentGameObject != null)
                        {
                            _board.Cells[xPos, yPos - 1] = cellA;
                            _board.Cells[xPos, yPos]     = cellB;

                            ICommand[] commands = { new SwipeDownCommand(cellA), new SwipeUpCommand(cellB), };

                            OnEvent(EventTypesEnum.Swipe, commands);
                        }
                    }

                    break;

                case MoveDirectionTypesEnum.Left:
                    if (xPos > 0)
                    {
                        ICell cellB = _board.Cells[xPos - 1, yPos];
                        if (cellB.CellTypeEnum == CellTypesEnum.Normal && cellB.CurrentGameObject != null)
                        {
                            _board.Cells[xPos - 1, yPos] = cellA;
                            _board.Cells[xPos, yPos]     = cellB;

                            ICommand[] commands = { new SwipeLeftCommand(cellA), new SwipeRightCommand(cellB), };

                            OnEvent(EventTypesEnum.Swipe, commands);
                        }
                    }

                    break;

                case MoveDirectionTypesEnum.Right:
                    if (xPos < _board.Width - 1)
                    {
                        ICell cellB = _board.Cells[xPos + 1, yPos];
                        if (cellB.CellTypeEnum == CellTypesEnum.Normal && cellB.CurrentGameObject != null)
                        {
                            _board.Cells[xPos + 1, yPos] = cellA;
                            _board.Cells[xPos, yPos]     = cellB;

                            ICommand[] commands = { new SwipeRightCommand(cellA), new SwipeLeftCommand(cellB), };

                            OnEvent(EventTypesEnum.Swipe, commands);
                        }
                    }

                    break;
                }
            }
        }