예제 #1
0
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // We don't want XNA calling this method each time we resume from the menu,
            // unfortunately, it'll call it whatever we try. So the only thing
            // we can do is check if it has been called already and return. Yes, it's ugly.
            if (spriteBatch_ != null)
            {
                GhostSoundsManager.ResumeLoops();
                return;
            }
            // Otherwise, this is the first time this component is Initialized, so proceed.

            GhostSoundsManager.Init(Game);

            Grid.Reset();
            Constants.Level = 1;
            spriteBatch_    = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            graphics_       = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));
            soundBank_      = (SoundBank)Game.Services.GetService(typeof(SoundBank));

            scoreFont_      = Game.Content.Load <SpriteFont>("Score");
            scoreEventFont_ = Game.Content.Load <SpriteFont>("ScoreEvent");
            xlife_          = Game.Content.Load <Texture2D>("sprites/ExtraLife");
            ppill_          = Game.Content.Load <Texture2D>("sprites/PowerPill");
            crump_          = Game.Content.Load <Texture2D>("sprites/Crump");
            board_          = Game.Content.Load <Texture2D>("sprites/Board");
            boardFlash_     = Game.Content.Load <Texture2D>("sprites/BoardFlash");
            bonusEaten_     = new Dictionary <string, int>();
            bonus_          = new Dictionary <string, Texture2D>(9)
            {
                { "Apple", Game.Content.Load <Texture2D>("bonus/Apple") },
                { "Banana", Game.Content.Load <Texture2D>("bonus/Banana") },
                { "Bell", Game.Content.Load <Texture2D>("bonus/Bell") },
                { "Cherry", Game.Content.Load <Texture2D>("bonus/Cherry") },
                { "Key", Game.Content.Load <Texture2D>("bonus/Key") },
                { "Orange", Game.Content.Load <Texture2D>("bonus/Orange") },
                { "Pear", Game.Content.Load <Texture2D>("bonus/Pear") },
                { "Pretzel", Game.Content.Load <Texture2D>("bonus/Pretzel") },
                { "Strawberry", Game.Content.Load <Texture2D>("bonus/Strawberry") }
            };

            scoreEvents_  = new List <ScoreEvent>(5);
            bonusPresent_ = false;
            bonusSpawned_ = 0;
            eatenGhosts_  = 0;
            Score         = 0;
            xlives_       = 2;
            paChomp_      = true;
            playerDied_   = false;
            player_       = new Player(Game);
            ghosts_       = new List <Ghost> {
                new Ghost(Game, player_, Ghosts.Blinky), new Ghost(Game, player_, Ghosts.Clyde),
                new Ghost(Game, player_, Ghosts.Inky), new Ghost(Game, player_, Ghosts.Pinky)
            };
            ghosts_[2].SetBlinky(ghosts_[0]); // Oh, dirty hack. Inky needs this for his AI.
            soundBank_.PlayCue("Intro");
            LockTimer = TimeSpan.FromMilliseconds(4500);

            base.Initialize();
        }
예제 #2
0
 void PlaySound()
 {
     if (State == GhostState.Attack || State == GhostState.Scatter || State == GhostState.Home)
     {
         if (Grid.NumCrumps < 50)
         {
             GhostSoundsManager.playLoopAttackVeryFast();
         }
         else if (Grid.NumCrumps < 120)
         {
             GhostSoundsManager.playLoopAttackFast();
         }
         else
         {
             GhostSoundsManager.playLoopAttack();
         }
     }
     else if (State == GhostState.Blue)
     {
         GhostSoundsManager.playLoopBlue();
     }
     else if (State == GhostState.Dead)
     {
         GhostSoundsManager.playLoopDead();
     }
 }
예제 #3
0
 /// <summary>
 /// AAAARRRGH
 /// </summary>
 public void KillPacMan()
 {
     player_.State = State.Dying;
     GhostSoundsManager.StopLoops();
     soundBank_.PlayCue("Death");
     LockTimer     = TimeSpan.FromMilliseconds(1811);
     playerDied_   = true;
     bonusPresent_ = false;
     bonusSpawned_ = 0;
 }
예제 #4
0
        public void KillGosht(Ghost ghost)
        {
            GhostSoundsManager.StopLoops();
            soundBank_.PlayCue("EatGhost");
            ghost.State = GhostState.Dead;
            eatenGhosts_++;
            var bonus = (int)(100 * Math.Pow(2, eatenGhosts_));

            Score += bonus;
            scoreEvents_.Add(new ScoreEvent(ghost.Position, DateTime.Now, bonus));
            LockTimer = TimeSpan.FromMilliseconds(900);
        }
예제 #5
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // Some events (death, new level, etc.) lock the game for a few moments.
            if (DateTime.Now - eventTimer_ < LockTimer)
            {
                ghosts_.ForEach(i => i.LockTimer(gameTime));
                // Also we need to do the same thing for our own timer concerning bonuses
                bonusSpawnedTime_ += gameTime.ElapsedGameTime;
                return;
            }

            // Remove special events older than 5 seconds
            scoreEvents_.RemoveAll(i => DateTime.Now - i.When > TimeSpan.FromSeconds(5));

            // If the player had died, spawn a new one or end game.
            if (playerDied_)
            {
                // extra lives are decremented here, at the same time the pac man is spawned; this makes those
                // events seem linked.
                xlives_--;
                //xlives_++; // Give infinite lives to the evil developer;
                if (xlives_ >= 0)
                {
                    playerDied_ = false;
                    player_     = new Player(Game);
                    ghosts_.ForEach(i => i.Reset(false, player_));
                    scoreEvents_.Clear();
                }
                else   // The game is over
                {
                    Menu.SaveHighScore(Score);
                    Game.Components.Add(new Menu(Game, null));
                    Game.Components.Remove(this);
                    GhostSoundsManager.StopLoops();
                    return;
                }
            }

            // When all crumps have been eaten, wait a few seconds and then spawn a new level
            if (noCrumpsLeft())
            {
                if (Constants.Level < 21)
                {
                    bonusSpawned_ = 0;
                    Grid.Reset();
                    player_ = new Player(Game);
                    ghosts_.ForEach(i => i.Reset(true, player_));
                    soundBank_.PlayCue("NewLevel");
                    LockTimer = TimeSpan.FromSeconds(2);
                    Constants.Level++;
                    return;
                }
                // Game over, you win.
                Menu.SaveHighScore(Score);
                Game.Components.Add(new Menu(Game, null));
                Game.Components.Remove(this);
                GhostSoundsManager.StopLoops();
                return;
            }

            Keys[] inputKeys = Keyboard.GetState().GetPressedKeys();
            // The user may escape to the main menu with the escape key
            if (inputKeys.Contains(Keys.Escape))
            {
                Game.Components.Add(new Menu(Game, this));
                Game.Components.Remove(this);
                GhostSoundsManager.PauseLoops(); // will be resumed in Initialize(). No need for stopping them
                // if the player subsequently quits the game, since we'll re-initialize GhostSoundManager in
                // Initialize() if the player wants to start a new game.
                return;
            }

            // Eat crumps and power pills.
            if (player_.Position.DeltaPixel == Point.Zero)
            {
                Point playerTile = player_.Position.Tile;
                if (Grid.TileGrid[playerTile.X, playerTile.Y].HasCrump)
                {
                    soundBank_.PlayCue(paChomp_ ? "PacMAnEat1" : "PacManEat2");
                    paChomp_ = !paChomp_;
                    Score   += 10;
                    Grid.TileGrid[playerTile.X, playerTile.Y].HasCrump = false;
                    if (Grid.TileGrid[playerTile.X, playerTile.Y].HasPowerPill)
                    {
                        Score       += 40;
                        eatenGhosts_ = 0;
                        foreach (Ghost ghost in ghosts_)
                        {
                            if (ghost.State == GhostState.Attack || ghost.State == GhostState.Scatter ||
                                ghost.State == GhostState.Blue)
                            {
                                ghost.State = GhostState.Blue;
                            }
                        }
                        Grid.TileGrid[playerTile.X, playerTile.Y].HasPowerPill = false;
                    }

                    // If that was the last crump, lock the game for a while
                    if (noCrumpsLeft())
                    {
                        GhostSoundsManager.StopLoops();
                        LockTimer = TimeSpan.FromSeconds(2);
                        return;
                    }
                }
            }

            // Eat bonuses
            if (bonusPresent_ && player_.Position.Tile.Y == 17 &&
                ((player_.Position.Tile.X == 13 && player_.Position.DeltaPixel.X == 8) ||
                 (player_.Position.Tile.X == 14 && player_.Position.DeltaPixel.X == -8)))
            {
                LockTimer = TimeSpan.FromSeconds(1.5);
                Score    += Constants.BonusScores();
                scoreEvents_.Add(new ScoreEvent(player_.Position, DateTime.Now, Constants.BonusScores()));
                soundBank_.PlayCue("fruiteat");
                bonusPresent_ = false;
                if (bonusEaten_.ContainsKey(Constants.BonusSprite()))
                {
                    bonusEaten_[Constants.BonusSprite()]++;
                }
                else
                {
                    bonusEaten_.Add(Constants.BonusSprite(), 1);
                }
            }

            // Remove bonus if time's up
            if (bonusPresent_ && ((DateTime.Now - bonusSpawnedTime_) > TimeSpan.FromSeconds(10)))
            {
                bonusPresent_ = false;
            }

            // Detect collision between ghosts and the player
            foreach (Ghost ghost in ghosts_)
            {
                var playerArea = new Rectangle((player_.Position.Tile.X * 16) + player_.Position.DeltaPixel.X,
                                               (player_.Position.Tile.Y * 16) + player_.Position.DeltaPixel.Y,
                                               26,
                                               26);
                var ghostArea = new Rectangle((ghost.Position.Tile.X * 16) + ghost.Position.DeltaPixel.X,
                                              (ghost.Position.Tile.Y * 16) + ghost.Position.DeltaPixel.Y,
                                              22,
                                              22);
                if (!Rectangle.Intersect(playerArea, ghostArea).IsEmpty)
                {
                    // If collision detected, either kill the ghost or kill the pac man, depending on state.
                    if (ghost.State == GhostState.Blue)
                    {
                        //TODO Work on the logic of the educational game
                        KillGosht(ghost);
                        return;
                    }
                    if (ghost.State != GhostState.Dead)
                    {
                        Game.Components.Add(new Menu(Game, this, ghost));
                        Game.Components.Remove(this);
                        GhostSoundsManager.PauseLoops();
                        return;
                    }
                    // Otherwise ( = the ghost is dead), don't do anything special.
                }
            }

            // Periodically spawn a fruit, when the player isn't on the spawn location
            // otherwise we get an infinite fruit spawning bug
            if ((Grid.NumCrumps == 180 || Grid.NumCrumps == 80) && bonusSpawned_ < 2 &&
                !(player_.Position.Tile.Y == 17 &&
                  ((player_.Position.Tile.X == 13 && player_.Position.DeltaPixel.X == 8) ||
                   (player_.Position.Tile.X == 14 && player_.Position.DeltaPixel.X == -8))))
            {
                bonusPresent_ = true;
                bonusSpawned_++;
                bonusSpawnedTime_ = DateTime.Now;
            }

            // Now is the time to move player based on inputs and ghosts based on AI
            // If we have returned earlier in the method, they stay in place
            player_.Update(gameTime);
            ghosts_.ForEach(i => i.Update(gameTime));

            base.Update(gameTime);
        }