/// <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; } }
private void OpenDialog(Point cellClicked, Point screenPosition) { game.GameState = MizJam1Game.GameStates.OpenDialog; dialog = new UIMenu(screenPosition, new Point(500, 500), true) { SpaceBetweenChildren = 5, Vertical = true }; Unit enemy; bool attackDirectly = false; if ((enemy = GetUnit(cellClicked)) != null && enemy.Enemy) { UIImage attack = new UIImage(game.Dialogs[MizJam1Game.Actions.Attack], game.SelectedDialogs[MizJam1Game.Actions.Attack]); Point moveTarget = threatenedPositions[cellClicked].From; MoveUnitCommand moveUnit = new MoveUnitCommand(this, SelectedUnit.Position, moveTarget); MoveAndAttackCommand moveAndAttackCommand = new MoveAndAttackCommand(moveUnit, this, SelectedUnit, enemy); attack.AddCommand(moveAndAttackCommand); dialog.AddChild(attack); attackDirectly = true; } if (!attackDirectly) { foreach (var threat in threatenedPositions) { if ((enemy = GetUnit(threat.Key)) != null && enemy.Enemy) { UIImage attackSelect = new UIImage(game.Dialogs[MizJam1Game.Actions.Attack], game.SelectedDialogs[MizJam1Game.Actions.Attack]); SelectAttackCommand selectAttackCommand = new SelectAttackCommand(this); attackSelect.AddCommand(selectAttackCommand); dialog.AddChild(attackSelect); break; } } } if (cellClicked == SelectedUnit.Position) { UIImage defend = new UIImage(game.Dialogs[MizJam1Game.Actions.Defend], game.SelectedDialogs[MizJam1Game.Actions.Defend]); DefendCommand defendCommand = new DefendCommand(this, SelectedUnit.Position); defend.AddCommand(defendCommand); dialog.AddChild(defend); } MoveUnitCommand moveCommand = null; if (canGoPositions.Contains(cellClicked)) { UIImage move = new UIImage(game.Dialogs[MizJam1Game.Actions.Move], game.SelectedDialogs[MizJam1Game.Actions.Move]); moveCommand = new MoveUnitCommand(this, SelectedUnit.Position, cellClicked); move.AddCommand(moveCommand); dialog.AddChild(move); canGoPositions = new HashSet <Point>() { cellClicked }; } if (!attackDirectly) { UIImage reroll = new UIImage(game.Dialogs[MizJam1Game.Actions.Reroll], game.SelectedDialogs[MizJam1Game.Actions.Reroll]); if (moveCommand == null) { moveCommand = new MoveUnitCommand(this, SelectedUnit.Position, SelectedUnit.Position); } ICommand rerollCommand = new MoveAndRerollCommand(moveCommand, this, SelectedUnit); reroll.AddCommand(rerollCommand); dialog.AddChild(reroll); } if (cellClicked == SelectedUnit.Position) { UIImage wait = new UIImage(game.Dialogs[MizJam1Game.Actions.Wait], game.SelectedDialogs[MizJam1Game.Actions.Wait]); WaitCommand waitCommand = new WaitCommand(this, cellClicked); wait.AddCommand(waitCommand); dialog.AddChild(wait); } UIImage cancel = new UIImage(game.Dialogs[MizJam1Game.Actions.Cancel], game.SelectedDialogs[MizJam1Game.Actions.Cancel]); ICommand cancelCommand = new CancelCommand(this); cancel.AddCommand(cancelCommand); dialog.AddChild(cancel); dialog.SetScale(3); if (dialog.Position.X + dialog.Size.X > 1500) { dialog.Position = new Point(screenPosition.X - dialog.Size.X, dialog.Position.Y); } if (dialog.Position.Y + dialog.Size.Y > 1080) { dialog.Position = new Point(dialog.Position.X, screenPosition.Y - dialog.Size.Y); } }