/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // TODO: Add your update logic here float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; LevelManager.Update(elapsedTime); GameObjectManager.Update(elapsedTime); // Process collisions foreach (CollisionPair pair in GameObjectManager.Collisions) { // Player collisions if (pair.A == Player.ID || pair.B == Player.ID) { uint colliderID = (pair.A == Player.ID) ? pair.B : pair.A; GameObject collider = GameObjectManager.GetObject(colliderID); // Process powerup collisions Powerup powerup = collider as Powerup; if (powerup != null) { Player.ApplyPowerup(powerup.Type); GameObjectManager.DestroyObject(colliderID); } //NOTE: Apply to more than the kamikaze enemy? // Process kamakaze collisions Enemy enemy = collider as Enemy; if (enemy != null && enemy.GetType() == typeof(Kamikaze)) { //Player take damage GameObjectManager.DestroyObject(colliderID); GameObjectManager.CreateExplosion(colliderID); } } } base.Update(gameTime); }
/// <summary> /// Helper method for processing gameobject collisions /// </summary> void ProcessCollisions() { // Process collisions foreach (CollisionPair pair in GameObjectManager.Collisions) { GameObject objectA = GameObjectManager.GetObject(pair.A); GameObject objectB = GameObjectManager.GetObject(pair.B); // Player collisions if (objectA.ObjectType == ObjectType.Player || objectB.ObjectType == ObjectType.Player) { PlayerShip player = ((objectA.ObjectType == ObjectType.Player) ? objectA : objectB) as PlayerShip; GameObject collider = (objectA.ObjectType == ObjectType.Player) ? objectB : objectA; // Process powerup collisions switch (collider.ObjectType) { case ObjectType.Powerup: Powerup powerup = collider as Powerup; player.ApplyPowerup(powerup.Type); GameObjectManager.DestroyObject(collider.ID); break; case ObjectType.Enemy: Enemy enemy = collider as Enemy; if (enemy.GetType() == typeof(Kamikaze) || enemy.GetType() == typeof(Mandible) || enemy.GetType() == typeof(SuicideBomber) || enemy.GetType() == typeof(Mine)) { //Player take damage GameObjectManager.DestroyObject(collider.ID); GameObjectManager.CreateExplosion(collider.ID); } break; case ObjectType.EnemyProjectile: Projectile projectile = collider as Projectile; // Damage player player.Health -= projectile.Damage; if (player.Health <= 0) { GameObjectManager.DestroyObject(player.ID); GameObjectManager.CreateExplosion(player.ID); } GameObjectManager.DestroyObject(collider.ID); break; } } // Player Projectile collisions else if (objectA.ObjectType == ObjectType.PlayerProjectile || objectB.ObjectType == ObjectType.PlayerProjectile) { Projectile playerProjectile = ((objectA.ObjectType == ObjectType.PlayerProjectile) ? objectA : objectB) as Projectile; GameObject collider = (objectA.ObjectType == ObjectType.Player) ? objectB : objectA; // Process collisions switch (collider.ObjectType) { case ObjectType.Enemy: Enemy enemy = collider as Enemy; //Enemy take damage enemy.Health -= playerProjectile.Damage; // If health <= 0, kill enemy if (enemy.Health <= 0) { GameObjectManager.DestroyObject(collider.ID); GameObjectManager.CreateExplosion(collider.ID); } // Destroy projectile // Note, if there are special things for the bullet, add them here GameObjectManager.DestroyObject(playerProjectile.ID); break; case ObjectType.Boss: Boss boss = collider as Boss; // Boss take damage boss.Health -= playerProjectile.Damage; // If health <= 0, kill boss if (boss.Health <= 0) { GameObjectManager.DestroyObject(collider.ID); GameObjectManager.CreateExplosion(collider.ID); } // Destroy projectile // Note, if there are special things for the bullet, add them here GameObjectManager.DestroyObject(playerProjectile.ID); break; } } } }