public Player(GameState state, Texture2D texture, Vector2 position, int width, int height, bool isControllable = true) : base(state, texture, position, width, height, true) { playerHealth = playerMaxHealth; FacingDirection = 1; Energy = MaxEnergy; Size = new Vector2(16, 32); Body.Velocity.X = 0; Body.Velocity.Y = -2f; FloatingUpSpeed = 0.8f; FloatingDownSpeed = FloatingUpSpeed * 2; Body.Acceleration.X = 1f; Body.MaxVelocity = 3f; Body.Drag.X = 0.6f; Body.Drag.Y = 0.6f; Body.Enabled = true; Body.Tag = "player"; /* Create a few bullets */ Bullets = new List <Bullet>(); for (int i = 0; i < 50; i++) { Bullet b = new Bullet(state, texture, Vector2.Zero, this); b.Animations.CurrentFrame = new Frame(48, 96, 16, 16); b.Body.SetSize(6, 6, 5, 5); b.Body.Drag.Y *= 1.1f; Bullets.Add(b); } movementParticleEmitter = new ParticleEmitter(State, 0, 0, 128); movementParticleEmitter.EmitterBox.Resize(1, 4); movementParticleEmitter.MakeParticles(texture, 16, 16); movementParticleEmitter.ParticleVelocity = new Vector2(0, 0.01f); movementParticleEmitter.SetAcceleration(0, -0.005f); movementParticleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f); movementParticleEmitter.YVelocityVariationRange = new Vector2(-40f, 40f); movementParticleEmitter.SetTextureCropRectangle(new Rectangle(3 * 16, 6 * 16, 16, 16)); movementParticleEmitter.SpawnRate = 40f; movementParticleEmitter.ParticleLifespanMilliseconds = 750f; movementParticleEmitter.ParticleLifespanVariationMilliseconds = 50f; movementParticleEmitter.InitialScale = 0.5f; movementParticleEmitter.FinalScale = 1.1f; anchorParticleEmitter = new ParticleEmitter(State, 0, 0, 10); //anchorParticleEmitter.EmitterBox.Resize(1, 4); anchorParticleEmitter.EmitterBox.Resize(Body.Bounds.Width, Body.Bounds.Height); anchorParticleEmitter.MakeRandomParticles(texture, new Rectangle[] { new Rectangle(0, 80, 16, 16), new Rectangle(16, 80, 16, 16) }); float dispersion = 200f; anchorParticleEmitter.XVelocityVariationRange = new Vector2(-dispersion, dispersion); anchorParticleEmitter.YVelocityVariationRange = new Vector2(-dispersion, dispersion); anchorParticleEmitter.SpawnRate = 150f; anchorParticleEmitter.ParticleLifespanMilliseconds = 750f; anchorParticleEmitter.ParticleLifespanVariationMilliseconds = 100f; anchorParticleEmitter.InitialScale = 0.5f; anchorParticleEmitter.FinalScale = 0.1f; anchorParticleEmitter.Burst = true; anchorParticleEmitter.ParticlesPerBurst = 5; anchorParticleEmitter.Activated = false; Floating = true; }
public void UpdateProjectiles(GameTime gameTime, KeyboardState keyboardState) { if (Alive) { this.movementParticleEmitter.Update(gameTime); this.movementParticleEmitter.ForEachParticle(KillOutOfBoundsParticle); this.movementParticleEmitter.EmitterBox.X = Body.X + 8; this.movementParticleEmitter.EmitterBox.Y = Body.Y + 16; this.movementParticleEmitter.Activated = false; this.anchorParticleEmitter.Update(gameTime); this.anchorParticleEmitter.EmitterBox.X = Body.X + 8; this.anchorParticleEmitter.EmitterBox.Y = Body.Y + 16; this.anchorParticleEmitter.Activated = false; } if (keyboardState.IsKeyDown(Keys.RightControl) && Energy >= BulletCost) { if (this.LastShot < gameTime.TotalGameTime.TotalMilliseconds) { this.LastShot = (float)gameTime.TotalGameTime.TotalMilliseconds + this.ShootRate; // get the first dead bullet Bullet b = null; for (int i = 0; i < Bullets.Count; i++) { if (!Bullets[i].Alive) { b = Bullets[i]; FireKnockBack(this); break; } } if (b != null) { Random rnd = new Random(); int YVariation = 4; b.Reset(); b.Revive(); b.ShotAtMilliseconds = gameTime.TotalGameTime.TotalMilliseconds; b.Body.X = Body.X + (FacingDirection > 0 ? 24 : -2); b.Body.Y = this.Body.Y + rnd.Next(-YVariation, YVariation) + 10; //TODO: fix 16 offset with final sprites b.Body.Velocity.X = (ShootingVelocity + (rnd.Next(-2, 2) * 0.1f)) * FacingDirection; // some variation to the speed b.Body.Velocity.Y = (rnd.Next(-3, -1) * 0.01f); // make it float a bit // subtract bullet cost to energy Energy -= BulletCost; Karma.playerShotsFired = Karma.ShotFired(Karma.playerShotsFired); float pitch = rnd.Next(-100, 10) * 0.01f; SoundEffect sfx; State.SFX.TryGetValue("bubble", out sfx); sfx?.Play(1f, pitch, 0f); } } } foreach (Bullet b in Bullets) { b.Update(gameTime); } }