/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { padOneState = GamePad.GetState(PlayerIndex.One); this.gameTime = gameTime.TotalGameTime; switch (gameState) { case GameState.running: if (Gru.Destination == null) { gamepad.Update(); } Gru.Movement(); if (minions.Count != 0) { minions[2].Movement(); } if (Gru.CheckCollisionWithPowerUp(currentPowerUp) || currentPowerUp.HasExpired) { currentPowerUp = CreateNewPowerUP(); if (minions.Count > 0) { ThiefMinion tempMinion = (ThiefMinion)minions[1]; tempMinion.bonusTile = currentPowerUp.actualTile; } if (Gru.Armed) nbOfShots = 6; } for (int i = 0; i < projectilesList.Count; i++) { projectilesList[i].Move(); if (projectilesList[i].hasExpired) { projectilesList.RemoveAt(i); i--; } } if (minions.Count != 0) { minions[0].Movement(); minions[1].Movement(); } foreach (Police police in policeList) { police.Movement(); foreach (Projectile projectile in projectilesList) { if (projectile.CheckCollisionWithPolice(police)) { RespawnPolice(police); ScoreBoard.GetInstance().AddPolicePoints(); } } if (Gru.CheckCollisionWithPolice(police)) { if (Gru.StarPower) { RespawnPolice(police); ScoreBoard.GetInstance().AddPolicePoints(); } else { Gru.OnBeenCaught(gameTime.TotalGameTime); ResetLevel(); } } if (minions.Count != 0) { if (minions[0].CheckCollisionWithPolice(police) || minions[2].CheckCollisionWithPolice(police)) { RespawnPolice(police); ScoreBoard.GetInstance().AddPolicePoints(); } } } if (!Gru.Alive) gameState = GameState.GameOver; if (labyrinth.CheckIfAllMoneyWasTaken()) { ship.IsVisible = true; } if (ship.IsVisible && ship.Position == Gru.actualTile.GetPosition()) { gameState = GameState.changeLevel; level++; } break; case GameState.pause: gamepad.Update(); break; case GameState.changeLevel: ResetLevel(); for (int i = policeList.Count(); i < level; i++) { AddNewPoliceToList(); } ship = new ExitShip(Content.Load<Texture2D>(GameGraphics.SHIP), labyrinth.GetRandomTile()); labyrinth.ResetMonies(); gameState = GameState.running; currentPowerUp = CreateNewPowerUP(); minions.Clear(); break; case GameState.GameOver: ProcessEnterScore(padOneState, gameTime.TotalGameTime); break; case GameState.highscore: ProcessHighScore(padOneState, gameTime.TotalGameTime); break; case GameState.menu: ProcessMainMenu(padOneState, gameTime.TotalGameTime); break; } if (gameState != GameState.menu) { if (Keyboard.GetState().IsKeyDown(Keys.Escape) || padOneState.Buttons.Back == ButtonState.Pressed) { if (lastTimeWasPressed == null || gameTime.TotalGameTime - lastTimeWasPressed >= DELAY_BEFORE_PRESSING_START) { gameState = GameState.menu; } } } base.Update(gameTime); }
/// <summary> /// Checks if there is a collision with a power up. /// </summary> /// <param name="powerUp">The power up.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool CheckCollisionWithPowerUp(PowerUp powerUp) { if (actualTile == powerUp.actualTile) { return true; } return false; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); gameState = GameState.menu; horizontalWall = Content.Load<Texture2D>(GameGraphics.HWALL); verticalWall = Content.Load<Texture2D>(GameGraphics.VWALL); textFont = Content.Load<SpriteFont>("Fonts/gamefont"); // TODO: use this.Content to load your game content here Gru = new PlayableCharacter ( Content.Load<Texture2D>(GameGraphics.GRU), new Vector2(labyrinth.GetTile(START_X, START_Y).GetPosition().X, labyrinth.GetTile(START_X, START_Y).GetPosition().Y), labyrinth.GetTile(START_X, START_Y) ); ship = new ExitShip(Content.Load<Texture2D>(GameGraphics.SHIP), labyrinth.GetRandomTile()); policeList = new List<Police>(); //L'entrée du téléporteur warpEntrance = Content.Load<Texture2D>(GameGraphics.WARP1); warpEntrancePos = new Vector2(labyrinth.GetTile(7, 4).GetPosition().X - Tile.TAILLE_LIGNE, labyrinth.GetTile(7, 4).GetPosition().Y + Tile.TAILLE_LIGNE); //Les sorties du téléporteur for (int i = 0; i < warpExit.Length; i++) { warpExit[i] = Content.Load<Texture2D>(GameGraphics.WARP2); } warpExitPos[0] = new Vector2(labyrinth.GetTile(0, 0).GetPosition().X, labyrinth.GetTile(0, 0).GetPosition().Y); warpExitPos[1] = new Vector2(labyrinth.GetTile(Labyrinth.WIDTH - 1, 0).GetPosition().X, labyrinth.GetTile(Labyrinth.WIDTH - 1, 0).GetPosition().Y); warpExitPos[2] = new Vector2(labyrinth.GetTile(0, Labyrinth.HEIGHT - 1).GetPosition().X, labyrinth.GetTile(0, Labyrinth.HEIGHT - 1).GetPosition().Y); warpExitPos[3] = new Vector2(labyrinth.GetTile(Labyrinth.WIDTH - 1, Labyrinth.HEIGHT - 1).GetPosition().X, labyrinth.GetTile(Labyrinth.WIDTH - 1, Labyrinth.HEIGHT - 1).GetPosition().Y); labyrinth.SpawnMoney(Content.Load<Texture2D>(GameGraphics.MONEY)); currentPowerUp = CreateNewPowerUP(); projectilesList = new List<Projectile>(); gamepad = new Gamepad(); gamepad.RegisterCommand(Buttons.DPadUp, new GruMoveUp(Gru)); gamepad.RegisterCommand(Buttons.DPadDown, new GruMoveDown(Gru)); gamepad.RegisterCommand(Buttons.DPadLeft, new GruMoveLeft(Gru)); gamepad.RegisterCommand(Buttons.DPadRight, new GruMoveRight(Gru)); gamepad.RegisterCommand(Buttons.LeftShoulder, new SpawnMinions(this)); gamepad.RegisterCommand(Buttons.RightShoulder, new GruShoot(this)); gamepad.RegisterCommand(Buttons.Back, new BackToMenu(this)); gamepad.RegisterCommand(Buttons.Start, new PauseGame(this)); }
/// <summary> /// Checks if there is a collision with a power up. /// </summary> /// <param name="powerUp">The power up.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool CheckCollisionWithPowerUp(PowerUp powerUp) { if (actualTile == powerUp.actualTile) { if (powerUp.Type == PowerUp.PowerUpType.star) { if (duration != null) duration.Close(); onStarPower = true; duration = new Timer(POWERSTAR_LIFESPAN); duration.Elapsed += ExpirationTimeHasPassed; duration.Enabled = true; powerUpHasExpired = false; } else { armed = true; } return true; } return false; }