public async Task <bool> CastSpell(Tile target, MouseEventArgs e) { var currentPlayerIndex = gameEngine.Players.IndexOf(gameEngine.CurrentPlayer); var finishedCasting = currentPlayerIndex + 1 == gameEngine.Players.Count; var spell = gameEngine.GetCurrentSpell(); if (e.Button == MouseButtons.Right) { gameEngine.CurrentPlayer = gameEngine.SwitchPlayer(); } else if (spell.CanCastOnNothing && target.GetOccupant() is Nothing && !this.finishedCasting && MonsterActions.isActionLegal(gameEngine.GetWizardCoordinates(), target.GetCoordinates())) { var monsterFromSpell = gameEngine.monsterGenerator.GetMonsterByName(spell.Caption, gameEngine.CurrentPlayer); monsterFromSpell.Owner = gameEngine.CurrentPlayer; targetField = target; SoundEngine.PlaySound("SingleCast"); await PlayAnimation(targetField, monsterFromSpell.Sprite, "Casting"); target.SetOccupant(monsterFromSpell); gameEngine.CurrentPlayer = gameEngine.SwitchPlayer(); } else if (spell.CanCastOnMonster && target.GetOccupant() is Monster && !this.finishedCasting) { var spellTarget = target.GetOccupant() as Monster; targetField = target; await ApplySpellEffect(spell, spellTarget); gameEngine.CurrentPlayer = gameEngine.SwitchPlayer(); } else { return(false); } if (finishedCasting) { this.finishedCasting = true; return(finishedCasting); } return(false); }
public GameEngine(int NumberOfPlayers, Gameboard gameboard, GameForm gameForm, int turnsLimit, bool autogenerate = true) { this.gameboard = gameboard; this.turnsLimit = turnsLimit * NumberOfPlayers; actions = new MonsterActions(gameboard, this); SetTileEvents(); if (autogenerate) { GenerateWizards(NumberOfPlayers); CurrentPlayer = Players[0]; } gameboard.players = Players; spellcasting = new Spellcasting(gameboard, this); this.gameForm = gameForm; gameForm.GetDescriptionPanel.Click += HideDescriptionPanel; }
private async Task TileClicked(Tile clickSource, MouseEventArgs e) { if (DescriptionMode) { if (clickSource.GetOccupant() is Monster) { gameForm.GetDescriptionPanel.Controls.Clear(); gameForm.GetDescriptionPanel.Controls.AddRange( new DescriptionPanel((Monster)clickSource.GetOccupant()) .GetControls()); gameForm.GetDescriptionPanel.Visible = true; gameForm.GetDescriptionPanel.BringToFront(); return; } } if (gamePhase == GamePhase.Casting && spellcasting.CastSpell(clickSource, e)) { CurrentPlayer = Players[0]; ResetMonsterMovement(); ChangePhase(GamePhase.Moving); await gameboard.HighlightMonstersOfPlayer(CurrentPlayer); return; } // Checks whether Tile we are clicking is occupied by entity with type other than "Nothing", // we set the clicked Tile to be a context for our further operations (eg. decision making // on what happens on second mouse click) if (gamePhase == GamePhase.Moving && firstClick && clickSource.GetOccupant().Owner == CurrentPlayer) { GetSourceField = clickSource; GetSelectedMonster = GetSourceField.GetOccupant() as Monster; gameboard.MovesLeftLabel.Text = $"Moves: {GetSelectedMonster.MovesRemaining}/{GetSelectedMonster.Moves}"; firstClick = false; SoundEngine.Speak(GetSelectedMonster); } // If we click the same tile twice, raise resetEventDataMethod to clean information else if (GetSourceField == clickSource) { resetEventData(); } // Gets the source of second mouse click, then decides what to do based on types of clicked objects else if (!firstClick) { GetTargetField = clickSource; if (!(GetTargetField.GetOccupant() is Monster)) { if (actions.Move(GetSourceField, GetTargetField)) { GetSourceField = GetTargetField; gameboard.MovesLeftLabel.Text = $@"Moves: {GetSelectedMonster.MovesRemaining}/{GetSelectedMonster.Moves}"; } } // Check if target tile is an enemy Monster, if so call Attack() on it else if (GetTargetField.GetOccupant() is Monster && GetTargetField.GetOccupant().Owner != GetSourceField.GetOccupant().Owner&& MonsterActions.isActionLegal(GetSourceField.GetCoordinates(), GetTargetField.GetCoordinates()) && GetSelectedMonster.canAttack) { await actions.Attack((Monster)GetSourceField.GetOccupant(), (Monster)GetTargetField.GetOccupant()); resetEventData(); } else { resetEventData(); } } }