예제 #1
0
        public static MoveDirectionTypes FindMoveDirection(Vector3 a, Vector3 b)
        {
            MoveDirectionTypes moveDirection = MoveDirectionTypes.None;

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

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

            return(moveDirection);
        }
예제 #2
0
        public void OnEvent(EventTypes eventType, object messageData)
        {
            switch (eventType)
            {
            case EventTypes.LMB_Down:
                _clickA = (Vector3)messageData;
                break;

            case EventTypes.LMB_Up:
                _clickB = (Vector3)messageData;

                if (_gameState == GameStates.Ready)
                {
                    if (_clickA.x >= 0 && _clickA.x < _board.Width && _clickA.y >= 0 && _clickA.y < _board.Height &&
                        (Mathf.Abs(_clickB.x - _clickA.x) > StringsAndConst.SWIPE_SENSITIVITY ||
                         Mathf.Abs(_clickB.y - _clickA.y) > StringsAndConst.SWIPE_SENSITIVITY))
                    {
                        MoveDirectionTypes swipeDirection = Helper.FindMoveDirection(_clickA, _clickB);
                        SwipeCells(swipeDirection);
                    }
                }

                break;

            case EventTypes.Swipe:
                _gameState = GameStates.Wait;
                ExecuteMacroCommand();
                break;

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

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

                _board.Cells[cellBack.TargetX, cellBack.TargetY] = cellBack;
                cellBack.CellState = CellStates.Wait;

                _gameState = GameStates.Ready;
                break;

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

                cellFall.CellState = CellStates.Wait;

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

                break;

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

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

                _powersDictionary.Add(position, powerUp);

                break;

            case EventTypes.BOARD_Collapse:
                ExecuteMacroCommand();
                break;

            case EventTypes.BOARD_EndDestroyMatchedCells:
                if (_powersDictionary.Count > 0)
                {
                    _hasPowerUps = true;
                }

                if (_hasPowerUps)
                {
                    foreach (var power in _powersDictionary)
                    {
                        Vector2      pos         = power.Key;
                        PowerUpTypes powerUpType = power.Value;

                        if (powerUpType == PowerUpTypes.Gravity)
                        {
                            _reverseGravity = !_reverseGravity;
                        }

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

                        if (_matchedCellsDictionary.ContainsKey(cell) == false)
                        {
                            _matchedCellsDictionary.Add(cell, cellsList);
                        }
                    }

                    _powersDictionary.Clear();
                    _hasPowerUps = false;

                    StartCoroutine(DestroyMatchedCells(_matchedCellsDictionary));
                }
                else
                {
                    StartCoroutine(RefillBoard());
                }

                break;

            default:
                Debug.Log(StringsAndConst.EVENT_NOT_FOUND);
                break;
            }
        }
예제 #3
0
        private void SwipeCells(MoveDirectionTypes direction)
        {
            int xPos = (int)Mathf.Round(_clickA.x);
            int yPos = (int)Mathf.Round(_clickA.y);

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

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

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

                            OnEvent(EventTypes.Swipe, null);
                        }
                    }

                    break;

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

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

                            OnEvent(EventTypes.Swipe, null);
                        }
                    }

                    break;

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

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

                            OnEvent(EventTypes.Swipe, null);
                        }
                    }

                    break;

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

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

                            OnEvent(EventTypes.Swipe, null);
                        }
                    }

                    break;
                }
            }
        }