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); }
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 = 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 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); }
// 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); }