protected void SpawnProjectile(string projectileName, ProjectileSource source) { var slop = 20; //spawn the projectile just outside the players bounding box in the direction the player is facing. var offset = GameUtils.RotationToVec2((float)(RigidBody.GetAngle() * 180 / System.Math.PI)); var offsetLength = GameUtils.PhysicsVec(new Vector2(0, (Texture.Height + slop) / 2)); offset = offset * offsetLength.Length(); WeaponFactory.Instance.CreateProjectile(projectileName, RigidBody.GetPosition() + offset, RigidBody.GetAngle(), source); }
/// <summary> /// update override /// </summary> /// <param name="gameTime"></param> public override void Update(GameTime gameTime) { if (Active) { var rotationDegrees = RigidBody.GetAngle() * 180 / System.Math.PI; if (Keyboard.GetState().IsKeyDown(Keys.OemOpenBrackets)) { if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed) { //apply impulse to push the player to left var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees - 90); RigidBody.ApplyImpulse(impulseVec * GameData.PlayerLateralImpulse , RigidBody.GetPosition()); } } if (Keyboard.GetState().IsKeyDown(Keys.OemCloseBrackets)) { if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed) { //apply impulse to push the player to right var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees + 90); RigidBody.ApplyImpulse(impulseVec * GameData.PlayerLateralImpulse , RigidBody.GetPosition()); } } if (FilteredInputListener.WasKeyPressed(Keys.Left)) { WeaponInventory.SelectPreviousWeapon(); FilteredInputListener.ResetKey(Keys.Left); } if (FilteredInputListener.WasKeyPressed(Keys.Right)) { WeaponInventory.SelectNextWeapon(); FilteredInputListener.ResetKey(Keys.Right); } if (Keyboard.GetState().IsKeyDown(Keys.W)) { if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed) { var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees); if (Vec2.Dot(impulseVec, RigidBody.GetLinearVelocity()) == 0) { RigidBody.SetLinearVelocity(Vec2.Zero); } RigidBody.ApplyImpulse(impulseVec * GameData.PlayerImpulse , RigidBody.GetPosition()); } } if (Keyboard.GetState().IsKeyDown(Keys.S)) { if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed) { var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees); RigidBody.ApplyImpulse(impulseVec * -GameData.PlayerImpulse , RigidBody.GetPosition()); } } if (Keyboard.GetState().IsKeyDown(Keys.D)) { //DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 1); RigidBody.ApplyTorque(GameData.PlayerTurnTorque); } if (Keyboard.GetState().IsKeyDown(Keys.A)) { //DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 1); RigidBody.ApplyTorque(-GameData.PlayerTurnTorque); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { var weapon = WeaponInventory.GetSelectedWeapon(); if (weapon != null && weapon.RemainingAmmo > 0) { if (DateTime.Now - _lastProjectileTime > TimeSpan.FromMilliseconds(100)) { ShootingEffect.Play(); _lastProjectileTime = DateTime.Now; WeaponInventory.DecreaseAmmo(1); SpawnProjectile(weapon.ProjectileName, ProjectileSource.Player); } } } if (Keyboard.GetState().IsKeyUp(Keys.W) && Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.S) && Keyboard.GetState().IsKeyUp(Keys.D)) { DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 0); } if (Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.D)) { RigidBody.SetAngularVelocity(0); } } }