private void SpawnEnemyBullet(EnemyAction enemyAction, BasicEnemy sender) { if (enemyAction == EnemyAction.FireBullet) { // Spawn Bullet EnemyBullet bullet = new EnemyBullet(this, spriteBatch, spriteDictionary["waterBullet"], sender.Position); bullet.SetDirection(sender.Rotation, 5); Components.Add(bullet); } }
/// <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(); } //Game State Menu if (gameState == GameState.Menu) { gameState = GameState.Play; } IEnumerable <BasicEnemy> enemies = Components.OfType <BasicEnemy>(); IEnumerable <BasicBullet> bullets = Components.OfType <BasicBullet>(); //Game State Play if (gameState == GameState.Play) { //Play Legit Music if (!playingLegitMusic) { SoundEffectInstance soundinstance = legitMusic.CreateInstance(); soundinstance.IsLooped = true; soundinstance.Play(); playingLegitMusic = true; } //give player ammo //Give enemy player position foreach (BasicEnemy enemy in enemies) { enemy.GetPlayerPosition(player.Position); } //enemies.ForEach(enemy => enemy.GetPlayerPosition(player.Position)); if (this.player.collisionRectangle.Intersects(ammoDrawRectangle)) { if (canAmmo) { this.player.Ammo += 10; canAmmo = false; ammoTimer.Start(ammoTimeSpan); } } ammoTimer.Update(gameTime.ElapsedGameTime); //check for Collisions List <DrawableGameComponent> removals = new List <DrawableGameComponent>(); foreach (DrawableGameComponent collider in Components) { foreach (DrawableGameComponent other in Components) { #region Player - Bullet Collision if (collider is Player && other is EnemyBullet) { Player player = collider as Player; EnemyBullet bullet = other as EnemyBullet; //checks collision if (player.Enabled && bullet.Enabled && player.collisionRectangle.Intersects(bullet.collisionRectangle)) { player.Health -= 10; //turns them off components if (player.Health <= 0) { player.Health = 0; player.Enabled = false; removals.Add(player); gameState = GameState.Dead; } bullet.Enabled = false; //add to list of things to remove removals.Add(bullet); } } #endregion #region Enemy - Bullet Collision if (collider is BasicEnemy && other is PlayerBullet) { BasicEnemy enemy = collider as BasicEnemy; PlayerBullet bullet = other as PlayerBullet; //checks collision if (enemy.Enabled && bullet.Enabled && enemy.collisionRectangle.Intersects(bullet.collisionRectangle)) { if (bullet.weaponType == WeaponType.WaterGun) { //damage, 5 shots kills or one burst enemy.Health -= 20; } else if (bullet.weaponType == WeaponType.SlingShot) { // damage, 2 shots kills and slows enemy enemy.Health -= 50; enemy.LinearVelocity *= 0.5f; } else if (bullet.weaponType == WeaponType.DonutGun) { // damage, 1 shot kills enemy.Health -= 100; } else { // Incase something goes wrong, still does damage enemy.Health -= 10; } //turns them off components if (enemy.Health <= 0) { //update score score += 100; enemy.Enabled = false; removals.Add(enemy); } bullet.Enabled = false; //add to list of things to remove removals.Add(bullet); } } #endregion #region Enemy - Player Collission if (collider is BasicEnemy && other is Player) { BasicEnemy enemy = collider as BasicEnemy; Player player = other as Player; //check collision if (enemy.Enabled && player.Enabled && player.collisionRectangle.Intersects(enemy.collisionRectangle)) { // lower health this.player.Health -= 1; } //check if player is dead if (player.Health <= 0) { //turn off player and remove to list to remove player.Enabled = false; removals.Add(player); gameState = GameState.Dead; } } #endregion } } //spawn enemies if spawn timer expires if (canSpawn) { //enemies for (int i = 1; i <= 5; i++) { BasicEnemy enemy = new BasicEnemy(this, spriteBatch, spriteDictionary["player"], new Vector2(Screen.Width - (150 * i), 0)); enemy.EnemyActionTriggeredEvent += SpawnEnemyBullet; Components.Add(enemy); } //restart timer canSpawn = false; spawnTimer.Start(spawnTimeSpan); } //remove list of components to be removed foreach (DrawableGameComponent removal in removals) { Components.Remove(removal); } //update health and score //healthText = HEALTH_STRING + this.player.Health; scoreText = SCORE_STRING + score; ammoText = AMMO_STRING + this.player.Ammo; //update timer spawnTimer.Update(gameTime.ElapsedGameTime); } if (gameState == GameState.Dead) { foreach (BasicEnemy enemy in enemies) { enemy.LayerDepth = -1; } foreach (BasicBullet bullet in bullets) { bullet.LayerDepth = -1; } } base.Update(gameTime); }
/// <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(); } //Give enemy player position IEnumerable <BasicEnemy> enemies = Components.OfType <BasicEnemy>(); foreach (BasicEnemy enemy in enemies) { enemy.GetPlayerPosition(player.Position); } //enemies.ForEach(enemy => enemy.GetPlayerPosition(player.Position)); //check for Collisions List <DrawableGameComponent> removals = new List <DrawableGameComponent>(); foreach (DrawableGameComponent collider in Components) { foreach (DrawableGameComponent other in Components) { #region Player - Bullet Collision if (collider is Player && other is EnemyBullet) { Player player = collider as Player; EnemyBullet bullet = other as EnemyBullet; //checks collision if (player.Enabled && bullet.Enabled && player.collisionRectangle.Intersects(bullet.collisionRectangle)) { player.Health -= 10; //turns them off components if (player.Health <= 0) { player.Health = 0; player.Enabled = false; removals.Add(player); } bullet.Enabled = false; //add to list of things to remove removals.Add(bullet); } } #endregion #region Enemy - Bullet Collision if (collider is BasicEnemy && other is PlayerBullet) { BasicEnemy enemy = collider as BasicEnemy; PlayerBullet bullet = other as PlayerBullet; //checks collision if (enemy.Enabled && bullet.Enabled && enemy.collisionRectangle.Intersects(bullet.collisionRectangle)) { enemy.Health -= 20; //turns them off components if (enemy.Health <= 0) { enemy.Enabled = false; removals.Add(enemy); } bullet.Enabled = false; //add to list of things to remove removals.Add(bullet); //update score score += 10; } } #endregion #region Enemy - Player Collission if (collider is BasicEnemy && other is Player) { BasicEnemy enemy = collider as BasicEnemy; Player player = other as Player; //check collision if (enemy.Enabled && player.Enabled && player.collisionRectangle.Intersects(enemy.collisionRectangle)) { // lower health this.player.Health -= 1; } //check if player is dead if (player.Health <= 0) { //turn off player and remove to list to remove player.Enabled = false; removals.Add(player); } } #endregion } } //spawn enemies if spawn timer expires if (canSpawn) { //enemies for (int i = 1; i <= 5; i++) { BasicEnemy enemy = new BasicEnemy(this, spriteBatch, spriteDictionary["player"], new Vector2(Screen.Width - (150 * i), 0)); enemy.EnemyActionTriggeredEvent += SpawnEnemyBullet; Components.Add(enemy); } //restart timer canSpawn = false; spawnTimer.Start(spawnTimeSpan); } //remove list of components to be removed foreach (DrawableGameComponent removal in removals) { Components.Remove(removal); } //update health and score healthText = HEALTH_STRING + this.player.Health; scoreText = SCORE_STRING + score; ammoText = AMMO_STRING + this.player.Ammo; //update timer spawnTimer.Update(gameTime.ElapsedGameTime); base.Update(gameTime); }