Exemplo n.º 1
0
        /// <summary>
        /// Provides a very basic finite-state machine to control the interactions
        /// with the game. When a case is clicked, there are several possible
        /// actions according to the UI's current state.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CaseClicked(object sender, CaseClickedEventArgs e)
        {
            switch (_state)
            {
            case CommandState.Selecting:
                var select = new SelectCaseCommand(_game, _gameControl);
                if (select.CanExectute(e.ClickedCase))
                {
                    select.Execute(e.ClickedCase);
                }
                else
                {
                    _gameControl.DisplayInvalidCommandOn(e.ClickedCase);
                }

                break;

            case CommandState.Moving:
                var unitToMove = ((UnitView)_units.SelectedItem).Unit;
                var move       = new MoveUnitCommand(_game, unitToMove);

                if (move.CanExectute(e.ClickedCase))
                {
                    move.Execute(e.ClickedCase);
                    ResetUIState();
                    _gameControl.SelectCase(e.ClickedCase);
                }
                else
                {
                    _gameControl.DisplayInvalidCommandOn(e.ClickedCase);
                }

                break;

            case CommandState.Attacking:
                var attackWithUnit = ((UnitView)_units.SelectedItem).Unit;
                var attack         = new AttackCommand(attackWithUnit);

                if (attack.CanExectute(e.ClickedCase))
                {
                    attack.Execute(e.ClickedCase);
                    ResetUIState();
                    _gameControl.SelectCase(e.ClickedCase);
                }
                else
                {
                    _gameControl.DisplayInvalidCommandOn(e.ClickedCase);
                }
                break;

            default:
                _state = CommandState.Selecting;
                CaseClicked(sender, e);
                break;
            }
        }