public override void Update(GameTime gameTime) { KeyboardState keyState = Keyboard.GetState(); GamePadState Controler = GamePad.GetState(PlayerIndex.One); // If the Tank is not alive, then the Turret is not alive. if (TD_Game.Instance.playerTank.isAlive == false) { this.isAlive = false; } // If the Tank has finished the level, then the Turret is not alive. if (TD_Game.Instance.level_1.bFinish == true) { this.isAlive = false; } float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds; // The position of the Turrent. this.pos.X = TD_Game.Instance.playerTank.pos.X + (TD_Game.Instance.playerTank.sprite.Width / 2) - 10; this.pos.Y = TD_Game.Instance.playerTank.pos.Y + (TD_Game.Instance.playerTank.sprite.Height / 2) + 10; // Rotation of the Turret to the right/down. if (keyState.IsKeyDown(Keys.Right) || Controler.ThumbSticks.Right.X > 0) { if (rot <= 3.14f / 8) { rot += timeDelta * speed; } } // Rotation of the turret to the left/up. if (keyState.IsKeyDown(Keys.Left) || Controler.ThumbSticks.Right.X < 0) { if (rot >= -3.14 / 5) { rot -= timeDelta * speed; } } // The input and bullet code for post firing. if (keyState.IsKeyDown(Keys.Enter) || Controler.Triggers.Right >= 0.5f) { TD_GameActor Bullet = new TD_Fire(); // If enough time has passed, then the Tank can fire again. if (lastTimeAttack + intervalBetweenAttack1 < gameTime.TotalGameTime) { Bullet.sprite = TD_Game.Instance.Content.Load <Texture2D>("Bullet_1"); soundEffect.Play(1.0f, 0, 0); Bullet.look.X = (float)Math.Sin((rot + (3.14 / 2))); Bullet.look.Y = -(float)Math.Cos((rot + (3.14 / 2))); Bullet.pos.X = this.pos.X; Bullet.pos.Y = this.pos.Y; Bullets.Add(Bullet); // Once a bullet has left the playable screen, remove it from the List. foreach (TD_GameActor Check in this.Bullets) { if (Check.box.X > TD_Game.Instance.GraphicsDevice.DisplayMode.Width || Check.box.Y > TD_Game.Instance.GraphicsDevice.DisplayMode.Height || Check.box.X < 0 || Check.box.Y < 0) { Check.Bullets.Remove(Bullet); } } lastTimeAttack = gameTime.TotalGameTime; } } }
public override void Update(GameTime gametime) { float timeDelta = (float)gametime.ElapsedGameTime.TotalSeconds; // If the player is dead, look to the origin, // else look towards the center of the Turret, // which is at the center of the Tank. if (TD_Game.Instance.turret.isAlive == false) { look = new Vector2(); } else { look = TD_Game.Instance.turret.pos - this.pos; } if (TD_Game.Instance.level_1.Level_Scroll_L || TD_Game.Instance.level_2.Level_Scroll_L) { this.pos.X += (speed + 2.0f); } else if (TD_Game.Instance.level_1.Level_Scroll_R || TD_Game.Instance.level_2.Level_Scroll_R) { this.pos.X -= (speed + 2.0f); } if (look.Length() > 450.0f) { //code to detect if dino is hit for (int i = 0; i < TD_Game.Instance.Bullets.Count; i++) { if (this.box.Intersects(TD_Game.Instance.Bullets[i].box)) { this.isAlive = false; TD_Game.Instance.Bullets[i].isAlive = false; TD_Game.Instance.turret.score += 125; } } } else { look.Normalize(); // Check if a bullet Intersects with a Dino and if it does, // then flag the bullet and Dino as not alive and give // the player points. for (int i = 0; i < TD_Game.Instance.Bullets.Count; i++) { if (this.box.Intersects(TD_Game.Instance.Bullets[i].box)) { this.isAlive = false; TD_Game.Instance.Bullets[i].isAlive = false; TD_Game.Instance.turret.score += 200; } } // Rotation code for the Dino on the Y axis. if (TD_Game.Instance.turret.pos.Y < pos.Y) { rot = (float)Math.Atan(look.X / -look.Y); } else { rot = (float)(Math.Atan(-look.X / look.Y)); rot = rot + 3.14f; } TD_GameActor BGBullet = new TD_Fire(0.5f); // If enough time has passed, then the Tank can fire again. if (lastTimeAttack + intervalBetweenAttack1 < gametime.TotalGameTime) { BGBullet.sprite = TD_Game.Instance.Content.Load <Texture2D>("Dino_Bullet"); //soundEffect.Play(1.0f, 0, 0); BGBullet.look.X = (float)Math.Sin(rot); BGBullet.look.Y = -(float)Math.Cos(rot); BGBullet.pos.X = this.pos.X; BGBullet.pos.Y = this.pos.Y; this.Bullets.Add(BGBullet); // Once a bullet has left the playable screen, remove it from the List. foreach (TD_GameActor Check in this.Bullets) { if (Check.box.X > TD_Game.Instance.GraphicsDevice.DisplayMode.Width || Check.box.Y > TD_Game.Instance.GraphicsDevice.DisplayMode.Height || Check.box.X < 0 || Check.box.Y < 0) { Check.Bullets.Remove(BGBullet); } } lastTimeAttack = gametime.TotalGameTime; } look.Normalize(); } }