Пример #1
0
        private static void PlayHitSequence(int row, int column, bool showAnimation)
        {
            if (showAnimation)
            {
                UtilityFunctions.AddExplosion(row, column);
            }

            Audio.PlaySoundEffect(GameResources.GameSound("Hit"));
            UtilityFunctions.DrawAnimationSequence();
        }
Пример #2
0
        private static void PlayMissSequence(int row, int column, bool showAnimation)
        {
            if (showAnimation)
            {
                UtilityFunctions.AddSplash(row, column);
            }

            Audio.PlaySoundEffect(GameResources.GameSound("Miss"));
            UtilityFunctions.DrawAnimationSequence();
        }
Пример #3
0
        // Checks for when an attack has been completed. If so, it displays
        // a message, plays a sound and redraws the screen.
        private static void AttackCompleted(object sender, AttackResult result)
        {
            bool isHuman;

            isHuman = (_theGame.Player == HumanPlayer);
            if (isHuman)
            {
                UtilityFunctions.Message = ("You " + result.ToString());
            }
            else
            {
                UtilityFunctions.Message = ("The AI " + result.ToString());
            }

            switch (result.Value)
            {
            case ResultOfAttack.Destroyed:
                GameController.PlayHitSequence(result.Row, result.Column, isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));
                break;

            case ResultOfAttack.GameOver:
                GameController.PlayHitSequence(result.Row, result.Column, isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));
                while (Audio.SoundEffectPlaying(GameResources.GameSound("Sink")))
                {
                    SwinGame.Delay(10);
                    SwinGame.RefreshScreen();
                }

                if (HumanPlayer.IsDestroyed)
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Lose"));
                }
                else
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Winner"));
                }

                break;

            case ResultOfAttack.Hit:
                GameController.PlayHitSequence(result.Row, result.Column, isHuman);
                break;

            case ResultOfAttack.Miss:
                GameController.PlayMissSequence(result.Row, result.Column, isHuman);
                break;

            case ResultOfAttack.ShotAlready:
                Audio.PlaySoundEffect(GameResources.GameSound("Error"));
                break;
            }
        }
Пример #4
0
        /// <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 static void AttackCompleted(object sender, AttackResult result)
        {
            bool isHuman = false;

            isHuman = ReferenceEquals(_theGame.Player, HumanPlayer);

            if (isHuman)
            {
                UtilityFunctions.Message = "You " + result.ToString();
            }
            else
            {
                UtilityFunctions.Message = "The AI " + result.ToString();
            }

            if (result.Value == ResultOfAttack.Destroyed)
            {
                PlayHitSequence(System.Convert.ToInt32(result.Row), System.Convert.ToInt32(result.Column), isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));
            }
            else if (result.Value == ResultOfAttack.GameOver)
            {
                PlayHitSequence(System.Convert.ToInt32(result.Row), System.Convert.ToInt32(result.Column), isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));

                while (Audio.SoundEffectPlaying(GameResources.GameSound("Sink")))
                {
                    SwinGame.Delay(10);
                    SwinGame.RefreshScreen();
                }

                if (HumanPlayer.IsDestroyed)
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Lose"));
                }
                else
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Winner"));
                }
            }
            else if (result.Value == ResultOfAttack.Hit)
            {
                PlayHitSequence(System.Convert.ToInt32(result.Row), System.Convert.ToInt32(result.Column), isHuman);
            }
            else if (result.Value == ResultOfAttack.Miss)
            {
                PlayMissSequence(System.Convert.ToInt32(result.Row), System.Convert.ToInt32(result.Column), isHuman);
            }
            else if (result.Value == ResultOfAttack.ShotAlready)
            {
                Audio.PlaySoundEffect(GameResources.GameSound("Error"));
            }
        }
Пример #5
0
        // Starts a new game, and creates an AI player based upon
        // the _aiSetting currently set
        public static void StartGame()
        {
            if (!(_theGame == null))
            {
                GameController.EndGame();
            }

            // Create the game
            _theGame = new BattleShipsGame();

            if (_playableShips.Count == 0)
            {
                foreach (ShipName name in Enum.GetValues(typeof(ShipName)))
                {
                    if (name != ShipName.None)
                    {
                        _playableShips.Add(name);
                    }
                }
            }

            // create the players
            switch (_aiSetting)
            {
            case AIOption.Easy:
                _ai = new AIEasyPlayer(_theGame, _playableShips);
                Audio.PlaySoundEffect(GameResources.GameSound("Easy"));
                break;

            case AIOption.Medium:
                _ai = new AIMediumPlayer(_theGame, _playableShips);
                Audio.PlaySoundEffect(GameResources.GameSound("Medium"));
                break;

            case AIOption.Hard:
                _ai = new AIHardPlayer(_theGame, _playableShips);
                Audio.PlaySoundEffect(GameResources.GameSound("Hard"));
                break;

            default:
                _ai = new AIMediumPlayer(_theGame, _playableShips);
                Audio.PlaySoundEffect(GameResources.GameSound("Medium"));
                break;
            }
            _human = new Player(_theGame, _playableShips);
            // AddHandler _human.PlayerGrid.Changed, AddressOf GridChanged
            _ai.PlayerGrid.Changed   += GridChanged;
            _theGame.AttackCompleted += AttackCompleted;
            GameController.AddNewState(GameState.Deploying);

            // Stops listening to the old game once a new game has been started
        }
Пример #6
0
        private static void PlayHitSequence(int row, int column, bool showAnimation)
        {
            Console.WriteLine("in playHitSequence");
            if (showAnimation)
            {
                Console.WriteLine("playHitSequence show animation");
                UtilityFunctions.AddExplosion(row, column);
                Console.WriteLine("playHitSequence finish animation");
            }
            Console.WriteLine("playHitSequence play sound");
            Audio.PlaySoundEffect(GameResources.GameSound("Hit"));

            if (showAnimation)
            {
                Console.WriteLine("playHitSequence draw animation");
                UtilityFunctions.DrawAnimationSequence();
            }
        }
Пример #7
0
        // If the user has clicked somewhere on the screen, check if its a deployment
        // and, if it's true, deploy the currently selected ship with the
        // direction indicated by the controller.

        private static void DoDeployClick()
        {
            Point2D mouse;

            mouse = SwinGame.MousePosition();

            // Calculate the row and column selected

            int row;
            int col;

            row = Convert.ToInt32(Math.Floor(((mouse.Y - UtilityFunctions.FIELD_TOP)
                                              / (UtilityFunctions.CELL_HEIGHT + UtilityFunctions.CELL_GAP))));
            col = Convert.ToInt32(Math.Floor(((mouse.X - UtilityFunctions.FIELD_LEFT)
                                              / (UtilityFunctions.CELL_WIDTH + UtilityFunctions.CELL_GAP))));
            if (((row >= 0) &&
                 (row < GameController.HumanPlayer.PlayerGrid.Height)))
            {
                if (((col >= 0) &&
                     (col < GameController.HumanPlayer.PlayerGrid.Width)))
                {
                    // If the click is within the area, try to deploy

                    try
                    {
                        GameController.HumanPlayer.PlayerGrid.MoveShip(row, col, _selectedShip, _currentDirection);
                    }
                    catch (Exception ex)
                    {
                        Audio.PlaySoundEffect(GameResources.GameSound("Error"));
                        UtilityFunctions.Message = ex.Message;

                        if (ex.Message.ToLower() == "The given key was not present in the dictionary.".ToLower())
                        {
                            UtilityFunctions.Message = string.Format("The current ship maximum is of size {0}", GameController.PlayableShips.Count);
                        }
                    }
                }
            }
        }