public override void Update(Microsoft.Xna.Framework.GameTime gameTime) { if (playerBeenShot && this.Visible == true && timePlayerIsKilled < gameTime.TotalGameTime) { //player has been shot, reset this.Visible = false; this.Texture = this.Game.ShipTexture; playerBeenShot = false; this.Enabled = false; return; } counter--; if (counter == 0) { this.Enabled = false; this.Visible = false; } //player ship input controls KeyboardState keyboardState = Keyboard.GetState(); if (this.Enabled && !playerBeenShot && this.Visible) { if (keyboardState.IsKeyDown(Keys.Left)) { this.Position = new Vector2(this.Position.X - 5, this.Position.Y); } if (keyboardState.IsKeyDown(Keys.Right)) { this.Position = new Vector2(this.Position.X + 5, this.Position.Y); } if (this.Position.X < 0) { this.Position = new Vector2(0, this.Position.Y); } if (this.Position.X > this.Game.Graphics.PreferredBackBufferWidth - this.Texture.Width) { this.Position = new Vector2(this.Game.Graphics.PreferredBackBufferWidth - this.Texture.Width, this.Position.Y); } } if (keyboardState.IsKeyDown(Keys.LeftControl) && lastFireTimeFriend < gameTime.TotalGameTime && !playerBeenShot && this.Visible) { BulletSprite bullet = new BulletSprite(this.Game.FriendBullet, this.Game, true, TimeSpan.FromSeconds(1)); bullet.Position = new Vector2(this.Position.X + 17, this.Position.Y - 16); this.Game.Components.Add(bullet); this.Game.Sound = this.Game.Content.Load <SoundEffect>("fire"); this.Game.Sound.Play(); lastFireTimeFriend = gameTime.TotalGameTime.Add(TimeSpan.FromSeconds(0.2)); } }
public void Update(GameTime gameTime) { switch (this.game.CurrentGameState) { case Main.GameState.MainMenu: break; case Main.GameState.Playing: //IF NOT BOSS DO THIS... if (bossOrNot == 1) { //Logic for manuevouring if (!this.startTime.HasValue) { this.startTime = gameTime.TotalGameTime; } else { if (enemiesGoingLeft) { float percentProgress = (float)(gameTime.TotalGameTime - this.startTime.Value).TotalMilliseconds / (float)this.journeyTime.TotalMilliseconds; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].Position = new Vector2(enemyList[i].StartPosition.X - (percentProgress * distanceToTravel), enemyList[i].StartPosition.Y); } if (percentProgress > 1) { this.startTime = gameTime.TotalGameTime; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].StartPosition = new Vector2(enemyList[i].Position.X, enemyList[i].Position.Y); } enemiesGoingLeft = false; } } else { float percentProgress = (float)(gameTime.TotalGameTime - this.startTime.Value).TotalMilliseconds / (float)this.journeyTime.TotalMilliseconds; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].Position = new Vector2(enemyList[i].StartPosition.X + (percentProgress * distanceToTravel), enemyList[i].StartPosition.Y); } if (percentProgress > 1) { this.startTime = gameTime.TotalGameTime; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].StartPosition = new Vector2(enemyList[i].Position.X, enemyList[i].Position.Y); } enemiesGoingLeft = true; } } } //fire enemy bullets if (lastFireTimeEnemy < gameTime.TotalGameTime && enemyList.Count > 0) { BulletSprite bullet = new BulletSprite(shipImage2, game, false, TimeSpan.FromSeconds(game.level), 34, 26); bullet.Position = new Vector2(game.EnemyConductor.enemyList[generateRandom(0, game.EnemyConductor.enemyList.Count)].Position.X + 17, game.EnemyConductor.enemyList[generateRandom(0, game.EnemyConductor.enemyList.Count)].Position.Y + 32); game.Components.Add(bullet); lastFireTimeEnemy = gameTime.TotalGameTime.Add(TimeSpan.FromSeconds(0.5)); } } //IF BOSS DO THIS... else if (bossOrNot != 1) { //Anything but the alien if (monster >= 3) { //Logic for manuevouring if (!this.startTime.HasValue) { this.startTime = gameTime.TotalGameTime; } else { if (enemiesGoingLeft) { float percentProgress = (float)(gameTime.TotalGameTime - this.startTime.Value).TotalMilliseconds / (float)this.journeyTime.TotalMilliseconds; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].Position = new Vector2(enemyList[i].StartPosition.X - (percentProgress * distanceToTravel), enemyList[i].StartPosition.Y); } if (percentProgress > 1) { this.startTime = gameTime.TotalGameTime; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].StartPosition = new Vector2(enemyList[i].Position.X, enemyList[i].Position.Y); } enemiesGoingLeft = false; } } else { float percentProgress = (float)(gameTime.TotalGameTime - this.startTime.Value).TotalMilliseconds / (float)this.journeyTime.TotalMilliseconds; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].Position = new Vector2(enemyList[i].StartPosition.X + (percentProgress * distanceToTravel), enemyList[i].StartPosition.Y); } if (percentProgress > 1) { this.startTime = gameTime.TotalGameTime; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].StartPosition = new Vector2(enemyList[i].Position.X, enemyList[i].Position.Y); } enemiesGoingLeft = true; } } } //fire boss bullets if (lastFireTimeEnemy < gameTime.TotalGameTime && enemyList.Count > 0) { for (int i = 1; i <= 3; i++) { float posX = game.EnemyConductor.enemyList[generateRandom(0, game.EnemyConductor.enemyList.Count)].Position.X; float posY = game.EnemyConductor.enemyList[generateRandom(0, game.EnemyConductor.enemyList.Count)].Position.Y; BulletSprite bullet = new BulletSprite(shipImage2, game, false, TimeSpan.FromSeconds(generateRandom(1, 5)), 34, 26); bullet.Position = new Vector2((posX + i) + (17), (posY + i) + (32)); game.Components.Add(bullet); lastFireTimeEnemy = gameTime.TotalGameTime.Add(TimeSpan.FromSeconds(0.5)); } } } //if boss is alien else if (monster < 3) { //Logic for manuevouring if (!this.startTime.HasValue) { this.startTime = gameTime.TotalGameTime; } else { if (enemiesGoingLeft) { float percentProgress = (float)(gameTime.TotalGameTime - this.startTime.Value).TotalMilliseconds / (float)this.journeyTime.TotalMilliseconds; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].Position = new Vector2(enemyList[i].StartPosition.X - (percentProgress * distanceToTravel), enemyList[i].StartPosition.Y); } if (percentProgress > 1) { this.startTime = gameTime.TotalGameTime; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].StartPosition = new Vector2(enemyList[i].Position.X, enemyList[i].Position.Y); } enemiesGoingLeft = false; } } else { float percentProgress = (float)(gameTime.TotalGameTime - this.startTime.Value).TotalMilliseconds / (float)this.journeyTime.TotalMilliseconds; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].Position = new Vector2(enemyList[i].StartPosition.X + (percentProgress * distanceToTravel), enemyList[i].StartPosition.Y); } if (percentProgress > 1) { this.startTime = gameTime.TotalGameTime; for (int i = 0; i < enemyList.Count; ++i) { enemyList[i].StartPosition = new Vector2(enemyList[i].Position.X, enemyList[i].Position.Y); } enemiesGoingLeft = true; } } if (timer == 0) { bossGetsMad = gameTime.TotalGameTime.Add(TimeSpan.FromSeconds(15)); timer += 1; } //fire boss bullets if (lastFireTimeEnemy < gameTime.TotalGameTime && enemyList.Count > 0) { for (int i = 1; i <= 3; i++) { float posX = game.EnemyConductor.enemyList[generateRandom(0, game.EnemyConductor.enemyList.Count)].Position.X; float posY = game.EnemyConductor.enemyList[generateRandom(0, game.EnemyConductor.enemyList.Count)].Position.Y; BulletSprite bullet = new BulletSprite(shipImage2, game, false, TimeSpan.FromSeconds(generateRandom(1, 5)), 34, 26); bullet.Position = new Vector2((generateRandom(0, 1024)), (posY + i) + (32)); game.Components.Add(bullet); lastFireTimeEnemy = gameTime.TotalGameTime.Add(TimeSpan.FromSeconds(0.5)); } } if (bossGetsMad < gameTime.TotalGameTime && timer == 1) { for (int i = 0; i <= 14; ++i) { int left = 50 + (i * 45); createEnemy(shipImage6, left, 240, 2); createEnemy(shipImage5, left, 200, 2); } timer = 3; } } } } break; //End of Second Case case Main.GameState.Ending: break; } }