public override void Update(GameTime gameTime) { GamePadState currentState = GamePad.GetState(PlayerIndex.One); KeyboardState keyState = Keyboard.GetState(); // move camera left and right if (currentState.ThumbSticks.Right.X < 0 || keyState.IsKeyDown(Keys.Left)) { yaw(MathHelper.ToRadians(0.5f)); } if (currentState.ThumbSticks.Right.X > 0 || keyState.IsKeyDown(Keys.Right)) { yaw(MathHelper.ToRadians(-0.5f)); } //move camera forward and back if (currentState.ThumbSticks.Left.Y > 0 || keyState.IsKeyDown(Keys.Up)) { forward(1); } if (currentState.ThumbSticks.Left.Y < 0 || keyState.IsKeyDown(Keys.Down)) { backward(0.35f); } // limit the amount of bullets that can be spawned with last shot if ((currentState.Triggers.Right >0||keyState.IsKeyDown(Keys.Space)) && lastShot > 0.9) { //add offset to the bullet vector3 to center it in the crosshairs Bullet tempBullet = new Bullet(this, new Vector3(pos.X+0.06f, pos.Y-0.12f, pos.Z-1.5f), look); //load bullets loadcontent or it wont draw tempBullet.LoadContent(); Game1.getInstance().setBullet(tempBullet); lastShot = 0; Shoot.Play(); } lastShot += (float)gameTime.ElapsedGameTime.TotalSeconds; //collision detection stuff bs.Center = pos; if (collidesWith(Game1.getInstance().getEnemy().getBoundingSphere(), Game1.getInstance().getEnemy().getWorld()) && Game1.getInstance().getEnemy().isAlive()) { hitCount++; } //check for collisions with bullets for (int i = 0; i < Game1.getInstance().getNumBullets();i++) { Bullet tempBullet = Game1.getInstance().getBullet(i); if (collidesWith(tempBullet.getBoundingSphere(), tempBullet.getWorld()) && tempBullet.getCreator() is Enemy) { GettingHit.Play(); tempBullet.setAlive(false); hitCount++; health -=2; Game1.getInstance().CheckForRumble(gameTime); if (health <= 0) { alive = false; } } } //check for collisions with obstacles for (int i = 0; i < Game1.getInstance().getNumObstacles(); i++) { Obstacle tempObstacle = Game1.getInstance().getObstacle(i); if (collidesWith(tempObstacle.getBoundingSphere(), tempObstacle.getWorld())) { Crash.Play(); //player cant drive through obstaclesgit collisionString = "collision: x: " + tempObstacle.getPos().X + " y: " + tempObstacle.getPos().Y + " Z: " + tempObstacle.getPos().Z; if (currentState.ThumbSticks.Left.Y > 0 || keyState.IsKeyDown(Keys.Up)) { backward(1.0f); } if (currentState.ThumbSticks.Left.Y < 0 || keyState.IsKeyDown(Keys.Down)) { forward(1); } } } //end of collision detection view = Matrix.CreateLookAt(pos, pos + look, up); world = Matrix.Identity; //projection is the view space, anything out of this range is not drawn projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), Game1.getInstance().getGraphics().GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f); }
public override void Update(GameTime gameTime) { Vector3 direction = pathPoints[currentPoint] - pos; float distance = direction.LengthSquared(); direction.Normalize(); if (distance > 5) { spinY = (float)Math.Atan2(direction.X, direction.Z); pos += direction* 0.55f; } else { direction = Game1.getInstance().getPlayer().getPos() - pos; direction.Normalize(); spinY = (float)Math.Atan2(direction.X, direction.Z); if (lastShot > 2.0f && !dying) { //add offset to bullet to position it near the barrell of the tank Bullet tempBullet = new Bullet(this, pos + (world.Left*3)+(world.Up *5), direction); tempBullet.LoadContent(); Game1.getInstance().setBullet(tempBullet); Shoot.Play(); lastShot = 0; if (currentPoint < (pathPoints.Count - 1)) { currentPoint++; } else { currentPoint = 0; } } lastShot += (float)gameTime.ElapsedGameTime.TotalSeconds; } //center the bounding sphere on the tanks position bs.Center = pos + (world.Left * 5); //check for collisions with bullets for (int i = 0; i < Game1.getInstance().getNumBullets(); i++) { Bullet tempBullet = Game1.getInstance().getBullet(i); if (collidesWith(tempBullet.getBoundingSphere(), tempBullet.getWorld()) && tempBullet.getCreator() is Player) { health--; Hit.Play(); if (health == 0) { EnemyDying.Play(); dying = true; } tempBullet.setAlive(false); } if (dying) { break; } } if (dying) { pos -= up * 2; } //check for collisions with obstacles for(int i = 0; i < Game1.getInstance().getNumObstacles(); i++) { Obstacle tempObstacle = Game1.getInstance().getObstacle(i); if (collidesWith(tempObstacle.getBoundingSphere(), tempObstacle.getWorld())) { if (currentPoint < (pathPoints.Count - 1)) { currentPoint++; } else { currentPoint = 0; } } } //check for collisions with player if (collidesWith(Game1.getInstance().getPlayer().getBoundingSphere(), Game1.getInstance().getPlayer().getWorld())) { pos -= direction; } //each model has a world matrix for scale rotation and translation NB: Translation MUST BE LAST world = Matrix.CreateScale(1f, 0.2f, 5.5f) * Matrix.CreateRotationY(spinY) * Matrix.CreateTranslation(pos); }