/// <summary> /// Constructor consisting of a given model /// </summary> /// <param name="model">Model for use</param> public StaticModel(Model newModel, Vector3 position) { this.model = newModel; game = ScreenManager.Game; SetupModel(position); CollisionMesh = new CollisionMesh(model, 4, position); SetupCamera(); }
public override void Update(TimeSpan elapsedTime, CollisionMesh collisionMesh, GamePadState gamepadState, KeyboardState keyboardState) { if (collisionMesh == null) { throw new ArgumentNullException("collisionMesh"); } float timeSeconds = (float)elapsedTime.TotalSeconds; float speedBoost = 0.0f; if (gamepadState.Buttons.LeftStick == ButtonState.Pressed) speedBoost = 1.0f; if (keyboardState != null && keyboardState.IsKeyDown(Keys.LeftShift)) speedBoost = 1.0f; float rotSpeed = 2.0f * timeSeconds; float moveSpeed = (400.0f + 600.0f * speedBoost) * timeSeconds; Vector3 position = world.Translation; Vector3 axisX = new Vector3(world.M11, world.M12, world.M13); Vector3 axisY = new Vector3(world.M21, world.M22, world.M23); Vector3 axisZ = new Vector3(world.M31, world.M32, world.M33); Vector3 translate, rotate; GetInputVectors(gamepadState, keyboardState, out translate, out rotate); if (gamepadState.Buttons.RightStick == ButtonState.Pressed) rotate.X = rotate.Y = 0; Vector3 newPosition = position; newPosition += axisX * (moveSpeed * translate.X); newPosition += axisY * (moveSpeed * translate.Y); newPosition -= axisZ * (moveSpeed * translate.Z); collisionMesh.BoxMove(box, position, newPosition, 1, 0, 3, out newPosition); Matrix rotX = Matrix.CreateFromAxisAngle(axisX, -rotSpeed * rotate.X); Matrix rotY = Matrix.CreateFromAxisAngle(axisY, -rotSpeed * rotate.Y); Matrix rotZ = Matrix.CreateFromAxisAngle(axisZ, rotSpeed * rotate.Z); world.Translation = new Vector3(0, 0, 0); world = world * (rotX * rotY * rotZ); world.Translation = newPosition; Orthonormalize(ref world); view = Matrix.Invert(world); frustum = new BoundingFrustum(view * projection); }
public override void Update(TimeSpan elapsed_time, ref CollisionMesh collision_mesh, GamePadState gamepad_state, KeyboardState keyboard_state) { float time_seconds = (float)elapsed_time.TotalSeconds; float speed_boost = 0.0f; if (gamepad_state.Buttons.LeftStick == ButtonState.Pressed) speed_boost = 1.0f; if (keyboard_state != null && keyboard_state.IsKeyDown(Keys.LeftShift)) speed_boost = 1.0f; float rot_speed = 2.0f * time_seconds; float move_speed = (400.0f + 600.0f * speed_boost) * time_seconds; Vector3 position = world.Translation; Vector3 axis_x = new Vector3(world.M11, world.M12, world.M13); Vector3 axis_y = new Vector3(world.M21, world.M22, world.M23); Vector3 axis_z = new Vector3(world.M31, world.M32, world.M33); Vector3 translate, rotate; GetInputVectors(gamepad_state, keyboard_state, out translate, out rotate); if (gamepad_state.Buttons.RightStick == ButtonState.Pressed) rotate.X = rotate.Y = 0; Vector3 new_position = position; new_position += axis_x * (move_speed * translate.X); new_position += axis_y * (move_speed * translate.Y); new_position -= axis_z * (move_speed * translate.Z); collision_mesh.BoxMove(box, position, new_position, 1.0f, 0.0f, 3, out new_position); Matrix rot_x = Matrix.CreateFromAxisAngle(axis_x, -rot_speed * rotate.X); Matrix rot_y = Matrix.CreateFromAxisAngle(axis_y, -rot_speed * rotate.Y); Matrix rot_z = Matrix.CreateFromAxisAngle(axis_z, rot_speed * rotate.Z); world.Translation = new Vector3(0, 0, 0); world = world * (rot_x * rot_y * rot_z); world.Translation = new_position; Orthonormalize(ref world); view = Matrix.Invert(world); frustum = new BoundingFrustum(view * projection); }
/// <summary> /// Animates the camera from its current position towards the desired offset /// behind the chased object. The camera's animation is controlled by a simple /// physical spring attached to the camera and anchored to the desired position. /// </summary> public void Update(float elapsedTime, CollisionMesh collision) { UpdateWorldPositions(); // Calculate spring force Vector3 stretch = position - desiredPosition; Vector3 force = -stiffness * stretch - damping * velocity; // Apply acceleration Vector3 acceleration = force / mass; velocity += acceleration * elapsedTime; // Apply velocity position += velocity * elapsedTime; collisionPosition = position; // test camera for collision with world if (collision != null) { float collisionDistance; Vector3 collisionPoint, collisionNormal; if (collision.PointIntersect(lookAt, position, out collisionDistance, out collisionPoint, out collisionNormal)) { Vector3 dir = Vector3.Normalize(collisionPoint - lookAt); collisionPosition = collisionPoint - 10 * dir; } } UpdateMatrices(); }
public abstract void Update( TimeSpan elapsedTime, CollisionMesh collisionMesh, GamePadState gamepadState, KeyboardState keyboardState);
public override void Update(TimeSpan elapsed_time, ref CollisionMesh collision_mesh, GamePadState gamepad_state, KeyboardState keyboard_state) { float time_seconds = (float)elapsed_time.TotalSeconds; float speed_boost = gamepad_state.Triggers.Left; if (keyboard_state.IsKeyDown(Keys.LeftShift)) speed_boost = 1.0f; float rot_speed = 2.0f * time_seconds; float move_speed = (300.0f + 400.0f * speed_boost) * time_seconds; if (on_ground == false) velocity.Y -= gravity * time_seconds; else { if (gamepad_state.Buttons.A == ButtonState.Pressed || keyboard_state.IsKeyDown(Keys.Space)) { velocity.Y = (float)Math.Sqrt(gravity * 2.0f * jump_height); on_ground = false; } else velocity.Y = 0.0f; } Vector3 position = transform.Translation; Vector3 axis_x = new Vector3(transform.M11, transform.M12, transform.M13); Vector3 axis_y = new Vector3(0, 1, 0); Vector3 axis_z = new Vector3(transform.M31, transform.M32, transform.M33); Vector3 translate, rotate; GetInputVectors(gamepad_state, keyboard_state, out translate, out rotate); Vector3 new_position = position; new_position += axis_x * (move_speed * translate.X); new_position -= axis_z * (move_speed * translate.Z); new_position += velocity * time_seconds; float move_y = 12.5f * step_height * time_seconds; if (auto_move_y >= 0) { if (move_y > auto_move_y) move_y = auto_move_y; } else { move_y = -move_y; if (move_y < auto_move_y) move_y = auto_move_y; } new_position.Y += move_y; auto_move_y = 0; collision_mesh.BoxMove(box, position, new_position, 1.0f, 0.0f, 3, out new_position); if (Math.Abs(new_position.Y - position.Y) < 0.0001f && velocity.Y > 0.0f) velocity.Y = 0.0f; float dist; Vector3 pos, norm; if (velocity.Y <= 0) if (true == collision_mesh.BoxIntersect(box, new_position, new_position + new Vector3(0, -2 * step_height, 0), out dist, out pos, out norm)) { if (norm.Y > 0.70710678f) { on_ground = true; auto_move_y = step_height - dist; } else on_ground = false; } else on_ground = false; up_down_rot -= rot_speed * rotate.X; if (up_down_rot > 1) up_down_rot = 1; else if (up_down_rot < -1) up_down_rot = -1; Matrix rot_x = Matrix.CreateFromAxisAngle(axis_x, up_down_rot); Matrix rot_y = Matrix.CreateFromAxisAngle(axis_y, -rot_speed * rotate.Y); transform.Translation = Vector3.Zero; transform = transform * rot_y; Orthonormalize(ref transform); world.Translation = Vector3.Zero; world = transform * rot_x; transform.Translation = new_position; new_position.Y += head_height; world.Translation = new_position; view = Matrix.Invert(world); frustum = new BoundingFrustum(view * projection); }
public override void Update( TimeSpan elapsedTime, CollisionMesh collisionMesh, GamePadState gamepadState, KeyboardState keyboardState) { if (collisionMesh == null) { throw new ArgumentNullException("collisionMesh"); } float timeSeconds = (float)elapsedTime.TotalSeconds; float speedBoost = gamepadState.Triggers.Left; if (keyboardState.IsKeyDown(Keys.LeftShift)) { speedBoost = 1.0f; } float rotSpeed = 2.0f * timeSeconds; float moveSpeed = (300.0f + 400.0f * speedBoost) * timeSeconds; if (onGround == false) { velocity.Y -= gravity * timeSeconds; } else { if (gamepadState.Buttons.A == ButtonState.Pressed || keyboardState.IsKeyDown(Keys.Space)) { velocity.Y = (float)Math.Sqrt(gravity * 2.0f * jumpHeight); onGround = false; } else { velocity.Y = 0.0f; } } Vector3 position = transform.Translation; Vector3 axisX = new Vector3(transform.M11, transform.M12, transform.M13); Vector3 axisY = new Vector3(0, 1, 0); Vector3 axisZ = new Vector3(transform.M31, transform.M32, transform.M33); Vector3 translate, rotate; GetInputVectors(gamepadState, keyboardState, out translate, out rotate); Vector3 newPosition = position; newPosition += axisX * (moveSpeed * translate.X); newPosition -= axisZ * (moveSpeed * translate.Z); newPosition += velocity * timeSeconds; float moveY = 12.5f * stepHeight * timeSeconds; if (autoMoveY >= 0) { if (moveY > autoMoveY) { moveY = autoMoveY; } } else { moveY = -moveY; if (moveY < autoMoveY) { moveY = autoMoveY; } } newPosition.Y += moveY; autoMoveY = 0; collisionMesh.BoxMove(box, position, newPosition, 1, 0, 3, out newPosition); if (Math.Abs(newPosition.Y - position.Y) < 0.0001f && velocity.Y > 0.0f) { velocity.Y = 0.0f; } float dist; Vector3 pos, norm; if (velocity.Y <= 0) { if (true == collisionMesh.BoxIntersect(box, newPosition, newPosition + new Vector3(0, -2 * stepHeight, 0), out dist, out pos, out norm)) { if (norm.Y > 0.70710678f) { onGround = true; autoMoveY = stepHeight - dist; } else { onGround = false; } } else { onGround = false; } } upDownRot -= rotSpeed * rotate.X; if (upDownRot > 1) { upDownRot = 1; } else if (upDownRot < -1) { upDownRot = -1; } Matrix rotX = Matrix.CreateFromAxisAngle(axisX, upDownRot); Matrix rotY = Matrix.CreateFromAxisAngle(axisY, -rotSpeed * rotate.Y); transform.Translation = Vector3.Zero; transform = transform * rotY; Orthonormalize(ref transform); world.Translation = Vector3.Zero; world = transform * rotX; transform.Translation = newPosition; newPosition.Y += headHeight; world.Translation = newPosition; view = Matrix.Invert(world); frustum = new BoundingFrustum(view * projection); }
public override void Initialize() { float aspectRatio = this.device.Viewport.Width / this.device.Viewport.Height; this.collisionMesh = new CollisionMesh(sceneModel, 1); this.sceneObjects = new GameEntityList(); XmlLoaderHelper.LoadPlayerAttributes(xmlDoc, aspectRatio, ref this.camera, ref this.player); XmlLoaderHelper.LoadGunsAttributes(xmlDoc, ref this.sceneObjects); XmlLoaderHelper.LoadAmmoPacksAttributes(xmlDoc, ref this.sceneObjects); XmlLoaderHelper.LoadMedPacksAttributes(xmlDoc, ref this.sceneObjects); XmlLoaderHelper.LoadEnemiesAttributes(this.Game, xmlDoc, ref this.enemies); //Create collisionbox for each dynamic entity if (this.sceneObjects != null) { foreach (String meshName in this.sceneObjects.Keys) { ModelMesh mesh = null; this.sceneModel.Meshes.TryGetValue(meshName, out mesh); if (mesh == null) continue; GameEntity geOut = null; this.sceneObjects.TryGetValue(meshName, out geOut); if (geOut != null) { geOut.ModelMesh = mesh; mesh.Tag = new object(); //only marks it as a dynamic object } } foreach (String meshName in this.enemies.Keys) { ModelMesh mesh = null; this.sceneModel.Meshes.TryGetValue(meshName, out mesh); if (mesh == null) continue; Enemy eOut = null; this.enemies.TryGetValue(meshName, out eOut); if (eOut != null) { eOut.ModelMesh = mesh; Vector3 pos = mesh.BoundingSphere.Center; eOut.Position = pos; mesh.Tag = null; //don't render mesh marker } } } base.Initialize(); }
public void Update(GameTime gameTime, Enemy e, Vector3 cameraPos, ref Matrix view, ref Matrix projection, ref CollisionMesh collision) { /*Vector3 direction = Vector3.Subtract(e.Position, cameraPos); Matrix rotation = Matrix.CreateRotationY(e.YawAngle); Vector3 initialFacing = Vector3.Cross(Vector3.UnitZ, Vector3.Up); initialFacing = Vector3.Transform(initialFacing, rotation); initialFacing.Normalize(); direction.Normalize(); float res = Vector3.Dot(initialFacing, direction); float x = direction.X - e.Position.X; float y = direction.Y - e.Position.Y; float desiredAngle = (float)Math.Atan2(y, x); if (desiredAngle >= MathHelper.ToRadians(30.0f)) { float speed = 0.25f; float difference = Enemy.WrapAngle(desiredAngle - e.YawAngle); difference = MathHelper.Clamp(difference, -speed, speed); e.YawAngle = Enemy.WrapAngle(e.YawAngle + difference); e.ActualAnimationState = GameEntityAnimationState.Idle; } else { e.ActualAnimationState = GameEntityAnimationState.Walk; Vector3 pos01 = e.Position; pos01.X -= e.WalkSpeed * direction.X; pos01.Z -= e.WalkSpeed * direction.Z; e.Position = pos01; } float time_seconds = (float)gameTime.ElapsedGameTime.TotalSeconds; float rot_speed = 2.0f * time_seconds; float move_speed = (300.0f + 400.0f) * time_seconds; if (e.OnGround == false) { Vector3 vector = e.Velocity; vector.Y -= 2000.0f * time_seconds; } Vector3 position = e.Position; Vector3 axis_x = new Vector3(e.Matrix.M11, e.Matrix.M12, e.Matrix.M13); Vector3 axis_y = new Vector3(0, 1, 0); Vector3 axis_z = new Vector3(e.Matrix.M31, e.Matrix.M32, e.Matrix.M33); Vector3 translate = Vector3.Zero; Vector3 rotate = Vector3.Zero; Vector3 new_position = position; new_position += axis_x * (move_speed * translate.X); new_position -= axis_z * (move_speed * translate.Z); new_position += e.Velocity * time_seconds; float move_y = 12.5f * time_seconds; if (e.AutoMoveY >= 0) { if (move_y > e.AutoMoveY) move_y = e.AutoMoveY; } else { move_y = -move_y; if (move_y < e.AutoMoveY) move_y = e.AutoMoveY; } collision.BoxMove(e.Box, position, new_position, 1.0f, 0.0f, 3, out new_position); if (Math.Abs(new_position.Y - position.Y) < 0.0001f && e.Velocity.Y > 0.0f) { Vector3 v = e.Velocity; v.Y = 0.0f; e.Velocity = v; } float dist; Vector3 pos, norm; if (e.Velocity.Y <= 0) if (true == collision.BoxIntersect(e.Box, new_position, new_position + new Vector3(0, -2, 0), out dist, out pos, out norm)) { if (norm.Y > 0.70710678f) { e.AutoMoveY = dist; } else e.OnGround = false; } else e.OnGround = false; Matrix rot_y = Matrix.CreateFromAxisAngle(axis_y, -rot_speed * rotate.Y); Matrix m = e.Matrix; m.Translation = Vector3.Zero; m = m * rot_y; CollisionCamera.Orthonormalize(ref m); m.Translation = Vector3.Zero; m.Translation = new_position;*/ Vector3 direction = Vector3.Subtract(e.Position, cameraPos); Matrix rotation = Matrix.CreateRotationY(e.YawAngle); Vector3 initialFacing = Vector3.Cross(Vector3.UnitZ, Vector3.Up); initialFacing = Vector3.Transform(initialFacing, rotation); initialFacing.Normalize(); direction.Normalize(); float res = Vector3.Dot(initialFacing, direction); float x = direction.X - e.Position.X; float y = direction.Y - e.Position.Y; float desiredAngle = (float)Math.Atan2(y, x); if (desiredAngle >= MathHelper.ToRadians(30.0f)) { float speed = 0.25f; float difference = Enemy.WrapAngle(desiredAngle - e.YawAngle); difference = MathHelper.Clamp(difference, -speed, speed); e.YawAngle = Enemy.WrapAngle(e.YawAngle + difference); e.ActualAnimationState = GameEntityAnimationState.Idle; } else { e.ActualAnimationState = GameEntityAnimationState.Walk; Vector3 pos01 = e.Position; pos01.X -= e.WalkSpeed * direction.X; pos01.Z -= e.WalkSpeed * direction.Z; e.Box.min.X -= e.WalkSpeed * direction.X; e.Box.min.Z -= e.WalkSpeed * direction.Z; e.Box.max.X -= e.WalkSpeed * direction.X; e.Box.max.Z -= e.WalkSpeed * direction.Z; e.Position = pos01; } /* float time_seconds = (float)gameTime.ElapsedGameTime.TotalSeconds; float rot_speed = 2.0f * time_seconds; float move_speed = (300.0f + 400.0f) * time_seconds; if (e.OnGround == false) { Vector3 vector = e.Velocity; vector.Y -= 2000.0f * time_seconds; } Vector3 position = e.Position; Vector3 axis_x = new Vector3(e.Matrix.M11, e.Matrix.M12, e.Matrix.M13); Vector3 axis_y = new Vector3(0, 1, 0); Vector3 axis_z = new Vector3(e.Matrix.M31, e.Matrix.M32, e.Matrix.M33); Vector3 translate = Vector3.Zero; Vector3 rotate = Vector3.Zero; Vector3 new_position = position; new_position += axis_x * (move_speed * translate.X); new_position -= axis_z * (move_speed * translate.Z); new_position += e.Velocity * time_seconds; float move_y = 12.5f * time_seconds; if (e.AutoMoveY >= 0) { if (move_y > e.AutoMoveY) move_y = e.AutoMoveY; } else { move_y = -move_y; if (move_y < e.AutoMoveY) move_y = e.AutoMoveY; } collision.BoxMove(e.Box, position, new_position, 1.0f, 0.0f, 3, out new_position); if (Math.Abs(new_position.Y - position.Y) < 0.0001f && e.Velocity.Y > 0.0f) { Vector3 v = e.Velocity; v.Y = 0.0f; e.Velocity = v; } float dist; Vector3 pos, norm; if (e.Velocity.Y <= 0) if (true == collision.BoxIntersect(e.Box, new_position, new_position + new Vector3(0, -2, 0), out dist, out pos, out norm)) { if (norm.Y > 0.70710678f) { e.AutoMoveY = dist; } else e.OnGround = false; } else e.OnGround = false; Matrix rot_y = Matrix.CreateFromAxisAngle(axis_y, -rot_speed * rotate.Y); Matrix m = e.Matrix; m.Translation = Vector3.Zero; m = m * rot_y; CollisionCamera.Orthonormalize(ref m); m.Translation = Vector3.Zero; m.Translation = new_position;*/ }
public abstract void Update(TimeSpan elapsed_time, ref CollisionMesh collision_mesh, GamePadState gamepad_state, KeyboardState keyboard_state);
public override void Update(GameTime gameTime, CollisionMesh collision_mesh) { if (this.isCatched == false) { if (this.isRotatable) { this.Rotate(gameTime, this.angleOffset); } } else { /* BulletList deads = new BulletList(); foreach (Bullet b in this.bullets) { b.Update(gameTime); if (b.Dead) deads.Add(b); } foreach (Bullet b in deads) this.bullets.Remove(b); deads.Clear(); */ } base.Update(gameTime, collision_mesh); }
/// <summary> /// Unload game files /// </summary> public void UnloadFiles() { // unload level levelColor = null; levelCollision = null; levelSpawns = null; levelLights = null; // unload poasticles particleTextures = null; // unload animated sprites animatedSpriteTextures = null; // unload projectiles projectileModels = null; // unload powerups powerupModels = null; // unload players for (int i = 0; i < GameOptions.MaxPlayers; i++) { // must displose player so that it releases its effects if (players[i] != null) players[i].Dispose(); players[i] = null; } // unload hud hudCrosshair = null; hudEnergy = null; hudMissile = null; hudScore = null; hudBars = null; // unload damage texture damageTexture = null; // unload powerups powerup.Clear(); }
/// <summary> /// Load the game files (level, ships, wepons, etc...) /// </summary> public void LoadFiles(ContentManager content) { String level = levelFile + "/" + levelFile; // load level model levelColor = content.Load<Model>("levels/" + level); // load collision model Model collisionModel = content.Load<Model>( "levels/" + level + "_collision"); levelCollision = new CollisionMesh(collisionModel, GameOptions.CollisionMeshSubdivisions); collisionModel = null; // load spawns and lights levelSpawns = EntityList.Load("content/levels/" + level + "_spawns.xml"); levelLights = LightList.Load("content/levels/" + level + "_lights.xml"); // load particle textures if (particleTextures == null) { int i, j = particleFiles.GetLength(0); particleTextures = new Texture2D[j]; for (i = 0; i < j; i++) particleTextures[i] = content.Load<Texture2D>( "particles/" + particleFiles[i]); } // load animated sprite textures if (animatedSpriteTextures == null) { int i, j = animatedSpriteFiles.GetLength(0); animatedSpriteTextures = new Texture2D[j]; for (i = 0; i < j; i++) animatedSpriteTextures[i] = content.Load<Texture2D>( "explosions/" + animatedSpriteFiles[i]); } // load projectile models if (projectileModels == null) { int i, j = projectileFiles.GetLength(0); projectileModels = new Model[j]; for (i = 0; i < j; i++) projectileModels[i] = content.Load<Model>( "projectiles/" + projectileFiles[i]); } // load powerup models if (powerupModels == null) { int i, j = powerupFiles.GetLength(0); powerupModels = new Model[j]; for (i = 0; i < j; i++) powerupModels[i] = content.Load<Model>( "powerups/" + powerupFiles[i]); } // cerate players for (int i = 0; i < GameOptions.MaxPlayers; i++) if (shipFile[i] != null) { Model ShipModel = content.Load<Model>( "ships/" + shipFile[i]); EntityList ShipEnities = EntityList.Load( "content/ships/" + shipFile[i] + ".xml"); players[i] = new PlayerShip(this, i, ShipModel, ShipEnities, GameOptions.CollisionBoxRadius); } else players[i] = null; // create powerups EntityList powerups = EntityList.Load( "content/levels/" + level + "_powerups.xml"); foreach (Entity entity in powerups.Entities) { switch (entity.name) { case "energy": AddPowerup(PowerupType.Energy, entity.transform); break; case "missile": AddPowerup(PowerupType.Missile, entity.transform); break; } } // load hud textures if (gameMode == GameMode.SinglePlayer) { hudCrosshair = content.Load<Texture2D>( "screens/hud_sp_crosshair"); hudEnergy = content.Load<Texture2D>( "screens/hud_sp_energy"); hudMissile = content.Load<Texture2D>( "screens/hud_sp_missile"); hudScore = content.Load<Texture2D>( "screens/hud_sp_score"); hudBars = content.Load<Texture2D>( "screens/hud_sp_bars"); } else { hudCrosshair = content.Load<Texture2D>( "screens/hud_mp_crosshair"); hudEnergy = content.Load<Texture2D>( "screens/hud_mp_energy"); hudMissile = content.Load<Texture2D>( "screens/hud_mp_missile"); hudScore = content.Load<Texture2D>( "screens/hud_mp_score"); hudBars = content.Load<Texture2D>( "screens/hud_mp_bars"); } // load damage indicator texture damageTexture = content.Load<Texture2D>("screens/damage"); }
// level spawn points /// <summary> /// Updates the player ship for given elapsed time /// </summary> public void Update( float elapsedTime, // elapsed time on this frame CollisionMesh collision, // level collision mesh EntityList entities) { if (collision == null) { throw new ArgumentNullException("collision"); } if (entities == null) { throw new ArgumentNullException("entities"); } // updates damage screen time (zero for no damage indication) damageTime = Math.Max(0.0f, damageTime - elapsedTime); // if player dead if (IsAlive == false) { // disable engine particle system particleBoost.Enabled = false; // updates dead time (if zero, player is alive) deadTime = Math.Max(0.0f, deadTime - elapsedTime); // if player dead time expires, respawn if (IsAlive == true) { // reset player to a random spawn point Reset(entities.GetTransformRandom(random)); // add spawn animated sprite in front of player Vector3 Pos = movement.position + 10 * movement.rotation.Forward; gameManager.AddAnimSprite(AnimSpriteType.Spawn, Pos, 140, 80, 30, DrawMode.Additive, playerIndex); // play spawn sound gameManager.PlaySound("ship_spawn"); // reset energy, shield and boost energy = 1.0f; shield = 1.0f; boost = 1.0f; missileCount = 3; } return; } // hold position before movement Vector3 lastPostion = movement.position; // update movement movement.Update(elapsedTime); // test for collision with level Vector3 collisionPosition; if (collision.BoxMove(box, lastPostion, movement.position, 1.0f, 0.0f, 3, out collisionPosition)) { // update to valid position after collision movement.position = collisionPosition; // compute new velocity after collision Vector3 newVelocity = (collisionPosition - lastPostion) * (1.0f / elapsedTime); // if collision sound enabled if (collisionSound) { // test collision angle to play collision sound Vector3 WorldVel = movement.WorldVelocity; float dot = Vector3.Dot( Vector3.Normalize(WorldVel), Vector3.Normalize(newVelocity)); if (dot < 0.7071f) { // play collision sound gameManager.PlaySound("ship_collide"); // set rumble intensity dot = 1 - 0.5f * (dot + 1); gameManager.SetVibration(playerIndex, dot * 0.5f); // disable collision sounds until ship stops colliding collisionSound = false; } } // set new velocity after collision movement.WorldVelocity = newVelocity; } else // clear of collisions, re-enable collision sounds collisionSound = true; // update player transform transform = movement.rotation; transform.Translation = movement.position; // compute inverse transform transformInverse = Matrix.Invert(transform); // get normalized player velocity float velocityFactor = movement.VelocityFactor; // update bobbing bobbingTime += elapsedTime; float bobbingFactor = 1.0f - velocityFactor; float time = GameOptions.ShipBobbingSpeed * bobbingTime % (2 * MathHelper.TwoPi); float distance = bobbingFactor * GameOptions.ShipBobbingRange; bobbing.M41 = distance * (float)Math.Sin(time * 0.5f); bobbing.M42 = distance * (float)Math.Sin(time); bobbingInverse.M41 = -bobbing.M41; bobbingInverse.M42 = -bobbing.M42; // compute transform with bobbing Matrix bobbingTransform = bobbing * transform; // update particle system position particleBoost.Enabled = true; particleBoost.SetTransform(boostTransform * bobbingTransform); // if shield active if (shieldUse) { // update shield position animatedSpriteShield.Position = bobbingTransform.Translation + 10f * bobbingTransform.Forward; // update shiled charge shield -= elapsedTime / GameOptions.ShieldUse; // if shield charge depleted if (shield < 0) { // disable shield shieldUse = false; shield = 0; // kill shield animated sprite animatedSpriteShield.SetTotalTime(0); animatedSpriteShield = null; } } else // change shield shield = Math.Min(1.0f, shield + elapsedTime / GameOptions.ShieldRecharge); // if boost active if (boostUse) { // increase ship maximum velocity movement.maxVelocity = GameOptions.MovementVelocityBoost; // apply impulse force forward AddImpulseForce(transform.Forward * GameOptions.BoostForce); // set particle system velocity scale particleBoost.VelocityScale = Math.Min(1.0f, particleBoost.VelocityScale + 4.0f * elapsedTime); // update shield charge boost -= elapsedTime / GameOptions.BoostUse; // if boost depleated if (boost < 0) { // disable boost boostUse = false; boost = 0; } } else { // slowly returns ship maximum velocity to normal levels if (movement.maxVelocity > GameOptions.MovementVelocity) movement.maxVelocity -= GameOptions.BoostSlowdown * elapsedTime; // slowly returns particle system velocity scale to normal levels particleBoost.VelocityScale = Math.Max(0.1f, particleBoost.VelocityScale - 2.0f * elapsedTime); // charge boost boost = Math.Min(1.0f, boost + elapsedTime / GameOptions.BoostRecharge); } // charge blaster blaster = Math.Min(1.0f, blaster + elapsedTime / GameOptions.BlasterChargeTime); // charge missile missile = Math.Min(1.0f, missile + elapsedTime / GameOptions.MissileChargeTime); // update chase camera chaseCamera.ChasePosition = transform.Translation; chaseCamera.ChaseDirection = transform.Forward; chaseCamera.Up = transform.Up; chaseCamera.Update(elapsedTime, collision); }
public override void Update(TimeSpan elapsedTime, CollisionMesh collisionMesh, GamePadState gamepadState, KeyboardState keyboardState) { if (collisionMesh == null) { throw new ArgumentNullException("collisionMesh"); } float timeSeconds = (float)elapsedTime.TotalSeconds; float speedBoost = 0.0f; if (gamepadState.Buttons.LeftStick == ButtonState.Pressed) { speedBoost = 1.0f; } if (keyboardState != null && keyboardState.IsKeyDown(Keys.LeftShift)) { speedBoost = 1.0f; } float rotSpeed = 2.0f * timeSeconds; float moveSpeed = (400.0f + 600.0f * speedBoost) * timeSeconds; Vector3 position = world.Translation; Vector3 axisX = new Vector3(world.M11, world.M12, world.M13); Vector3 axisY = new Vector3(world.M21, world.M22, world.M23); Vector3 axisZ = new Vector3(world.M31, world.M32, world.M33); Vector3 translate, rotate; GetInputVectors(gamepadState, keyboardState, out translate, out rotate); if (gamepadState.Buttons.RightStick == ButtonState.Pressed) { rotate.X = rotate.Y = 0; } Vector3 newPosition = position; newPosition += axisX * (moveSpeed * translate.X); newPosition += axisY * (moveSpeed * translate.Y); newPosition -= axisZ * (moveSpeed * translate.Z); collisionMesh.BoxMove(box, position, newPosition, 1, 0, 3, out newPosition); Matrix rotX = Matrix.CreateFromAxisAngle(axisX, -rotSpeed * rotate.X); Matrix rotY = Matrix.CreateFromAxisAngle(axisY, -rotSpeed * rotate.Y); Matrix rotZ = Matrix.CreateFromAxisAngle(axisZ, rotSpeed * rotate.Z); world.Translation = new Vector3(0, 0, 0); world = world * (rotX * rotY * rotZ); world.Translation = newPosition; Orthonormalize(ref world); view = Matrix.Invert(world); frustum = new BoundingFrustum(view * projection); }
private void SetupLevel() { stage = new StaticModel(ScreenManager.Game.Content.Load<Model>(@"Levels\uhEnginePlayground"), Vector3.Zero); stage.Scale = 100; collideStage = new StaticModel(ScreenManager.Game.Content.Load<Model>(@"Levels\uhEnginePlayground"), Vector3.Zero); levelCollision = new CollisionMesh(stage.model, 1); collisionModels.Add(collideStage); }
public override void Update(GameTime gameTime, CollisionMesh collision_mesh) { this.Position = this.camera.transform.Translation; if (this.actualGun != null) { this.ActualGun.Update(gameTime, collision_mesh); } }