public void Draw(SpriteBatch spriteBatch) { float currScale = MathExtension.LinearInterpolate(ScaleEnd, ScaleBegin, lifePhase); Color currCol = MathExtension.LinearInterpolate(EndColor, StartColor, lifePhase); spriteBatch.Draw(Parent.texture, new Vector2((int)(Position.X), (int)(Position.Y)), null, currCol, rotation, origin, currScale, SpriteEffects.None, 0); }
public bool Update(float dt) { LifeLeft -= dt; if (LifeLeft <= 0) { return(false); } lifePhase = LifeLeft / StartingLife; // 1 means newly created 0 means dead. Position += MathExtension.LinearInterpolate(EndDirection, StartDirection, lifePhase) * dt; rotation += (Parent.RotationStrength * dt); //if (Parent.RestrictToBoundries) //{ // // //if (this.Position.X > Parent.Transform.Position.X + Parent.CollisionModel.Width / 2 // // || this.Position.X < Parent.Transform.Position.X - Parent.CollisionModel.Width / 2) // //{ // // EndDirection.X *= -1; // // StartDirection.X *= -1; // //} // //if (this.Position.Y > Parent.Transform.Position.Y + Parent.CollisionModel.Height / 2 // // || this.Position.Y < Parent.Transform.Position.Y - Parent.CollisionModel.Height / 2) // //{ // // EndDirection.Y *= -1; // // StartDirection.Y *= -1; // //} //} return(true); }
/// <summary> /// Initializes this instance /// </summary> public override void Initialize() { base.Initialize(); random = new Random(); if (texture == null && texturePath != null && texturePath.Trim() != "") { LoadTexture(); } LoadState(); nextSpawn = MathExtension.LinearInterpolate(SecondsPerSpawnMin, SecondsPerSpawnMax, random.NextDouble()); secElapsed = 0.0f; particles = new LinkedList <Particle>(); }
/// <summary> /// Updates this instance /// </summary> /// <param name="gameTime">The gametime</param> public override void Update(GameTime gameTime) { base.Update(gameTime); if (texture != null) { secElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds; while (secElapsed > nextSpawn) { if (particles.Count < MaxParticles && enabled) { // Spawn a particle Vector2 StartDirection = Vector2.Transform(SpawnDirection, Matrix.CreateRotationZ(MathExtension.LinearInterpolate(spawnAngleNoise.X, spawnAngleNoise.Y, random.NextDouble()))); StartDirection.Normalize(); Vector2 EndDirection = StartDirection * MathExtension.LinearInterpolate(FinalSpeedMin, FinalSpeedMax, random.NextDouble()); StartDirection *= MathExtension.LinearInterpolate(InitialSpeedMin, InitialSpeedMax, random.NextDouble()); particles.AddLast(new Particle( Transform.Position, StartDirection, EndDirection, MathExtension.LinearInterpolate(LifespanMin, LifespanMax, random.NextDouble()), MathExtension.LinearInterpolate(InitialScaleMin, InitialScaleMax, random.NextDouble()), MathExtension.LinearInterpolate(FinalScaleMin, FinalScaleMax, random.NextDouble()), MathExtension.LinearInterpolate(InitialColor1, InitialColor2, random.NextDouble()), MathExtension.LinearInterpolate(FinalColor1, FinalColor2, random.NextDouble()), this) ); particles.Last.Value.Update(secElapsed); } secElapsed -= nextSpawn; nextSpawn = MathExtension.LinearInterpolate(SecondsPerSpawnMin, SecondsPerSpawnMax, random.NextDouble()); if (burst) { particleBurstCount++; } if (burst && particleBurstCount >= maxParticles) { this.enabled = false; this.particleBurstCount = 0; } } } LinkedListNode <Particle> node = particles.First; while (node != null) { bool isAlive = node.Value.Update((float)gameTime.ElapsedGameTime.TotalSeconds); node = node.Next; if (!isAlive) { if (node == null) { particles.RemoveLast(); } else { particles.Remove(node.Previous); } } } }