/// <summary> /// Shoot will swap between players and check if a player has been killed. /// It also allows the current player to hit on the enemygrid. /// </summary> /// <param name="row">the row fired upon</param> /// <param name="col">the column fired upon</param> /// <returns>The result of the attack</returns> public AttackResult Shoot(int row, int col) { AttackResult newAttack; int otherPlayer = (_playerIndex + 1) % 2; newAttack = Player.Shoot(row, col); // Will exit the game when all players ships are destroyed if (_players[otherPlayer].IsDestroyed) { newAttack = new AttackResult(ResultOfAttack.GameOver, newAttack.Ship, newAttack.Text, row, col); } AttackCompleted?.Invoke(this, newAttack); // change player if the last hit was a miss if (newAttack.Value == ResultOfAttack.Miss) { _playerIndex = otherPlayer; } return(newAttack); }
/// <summary> /// Listens for attacks to be completed. /// </summary> /// <param name="sender">the game</param> /// <param name="result">the result of the attack</param> /// <remarks> /// Displays a message, plays sound and redraws the screen /// </remarks> private void AttackCompleted(object sender, AttackResult result) { bool isHuman; isHuman = _theGame.Player == HumanPlayer; if (isHuman) { if (result.Value == ResultOfAttack.Destroyed) { UtilityFunctions.MESSAGE_COLOR = Color.Green; UtilityFunctions.Instance.Message = "You " + result.ToString(); } else { UtilityFunctions.MESSAGE_COLOR = Color.White; } UtilityFunctions.Instance.Message = "You " + result.ToString(); } else { if (result.Value == ResultOfAttack.Destroyed) { UtilityFunctions.MESSAGE_COLOR = Color.Red; } else { UtilityFunctions.MESSAGE_COLOR = Color.Blue; } UtilityFunctions.Instance.Message = "The AI " + result.ToString(); } switch (result.Value) { case ResultOfAttack.Destroyed: { PlayHitSequence(result.Row, result.Column, isHuman); Color flashColor; if (isHuman) { flashColor = SwinGame.RGBAColor(0, 255, 0, 127); } else { flashColor = SwinGame.RGBAColor(255, 0, 0, 127); }; Audio.PlaySoundEffect(GameResources.Instance.GameSound("Sink")); SwinGame.FillRectangle(flashColor, 0, 0, 800, 600); SwinGame.RefreshScreen(); SwinGame.Delay(150); UtilityFunctions.DrawBackground(); DiscoveryController.DrawDiscovery(); SwinGame.RefreshScreen(); SwinGame.Delay(100); SwinGame.FillRectangle(flashColor, 0, 0, 800, 600); SwinGame.RefreshScreen(); SwinGame.Delay(150); break; } case ResultOfAttack.GameOver: { PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameResources.Instance.GameSound("Sink")); while (Audio.SoundEffectPlaying(GameResources.Instance.GameSound("Sink"))) { //SwinGame.Delay(10); SwinGame.RefreshScreen(); } if (HumanPlayer.IsDestroyed) { Audio.PlaySoundEffect(GameResources.Instance.GameSound("Lose")); } else { Audio.PlaySoundEffect(GameResources.Instance.GameSound("Winner")); } break; } case ResultOfAttack.Hit: { PlayHitSequence(result.Row, result.Column, isHuman); break; } case ResultOfAttack.Miss: { PlayMissSequence(result.Row, result.Column, isHuman); break; } case ResultOfAttack.ShotAlready: { Audio.PlaySoundEffect(GameResources.Instance.GameSound("Error")); break; } } }
/// <summary> /// The last shot had the following result. Child classes can use this /// to prepare for the next shot. /// </summary> /// <param name="result">The result of the shot</param> /// <param name="row">the row shot</param> /// <param name="col">the column shot</param> protected abstract void ProcessShot(int row, int col, AttackResult result);