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); }
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); }
/// <summary> /// Draw a box at the input location /// </summary> /// <param name="sb">The spritebatch</param> /// <param name="upperLeft">Upper left position</param> /// <param name="lowerRight">Lower right position</param> /// <param name="c">Color</param> public static void DrawBox(SpriteBatch sb, Vector2 upperLeft, Vector2 lowerRight, Color c, int linewidth) { Rectangle r = MathExtension.RectangleFromVectors(upperLeft, lowerRight); DrawLine(sb, r.Left, r.Top, r.Right, r.Top, c, linewidth); DrawLine(sb, r.Right, r.Y, r.Right, r.Bottom, c, linewidth); DrawLine(sb, r.Right, r.Bottom, r.Left, r.Bottom, c, linewidth); DrawLine(sb, r.Left, r.Bottom, r.Left, r.Top, c, linewidth); }
/// <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); } } } }
/// <summary> /// Draw a filled box at the input location /// </summary> /// <param name="sb">The spritebatch</param> /// <param name="upperLeft">Upper left position</param> /// <param name="lowerRight">Lower right position</param> /// <param name="c">Color</param> public static void DrawBoxFilled(SpriteBatch sb, Vector2 upperLeft, Vector2 lowerRight, Color c) { Rectangle r = MathExtension.RectangleFromVectors(upperLeft, lowerRight); sb.Draw(pixel, r, c); }
/// <summary> /// Draws this instance /// </summary> /// <param name="gameTime">The gametime</param> /// <param name="spriteBatch">The spriteBatch</param> public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { base.Draw(gameTime, spriteBatch); if (texture != null && Visible) // && (MeasureDimension().Intersects(SceneManager.ActiveCamera.BoundingBox) || displayMode == DisplayModes.Fill || displayMode == DisplayModes.Tile || displayMode == DisplayModes.PositionTile)) { Vector2 _orgx = Vector2.Zero; if (sourceRectangle != Rectangle.Empty) { _orgx = new Vector2(sourceRectangle.X - (sourceRectangle.Width / 2), sourceRectangle.Y - (sourceRectangle.Height / 2)); } else if (origin == Origins.Center) { _orgx = new Vector2(texture.Width / 2, texture.Height / 2); } if (displayMode == DisplayModes.Tile && MathExtension.IsPowerOfTwo(new Vector2(texture.Width, texture.Height))) { //Vector2 initialPosition = Vector2.Transform(Vector2.Zero, Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)); //Vector2 endPos = Vector2.Transform(new Vector2(SceneManager.GraphicsDevice.Viewport.Width, SceneManager.GraphicsDevice.Viewport.Height), Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)); //int sizeWidth = (int)(endPos.X - initialPosition.X); //int sizeHeight = (int)(endPos.Y - initialPosition.Y); spriteBatch.Begin(SpriteSortMode.Deferred, this.blendState, SamplerState.LinearWrap, null, null, null, SceneManager.ActiveCamera.TransformMatrix); //spriteBatch.Draw(texture, new Vector2(initialPosition.X, initialPosition.Y), new Rectangle((int)SceneManager.ActiveCamera.Position.X, // (int)SceneManager.ActiveCamera.Position.Y, sizeWidth, sizeHeight), Color, 0, Vector2.Zero, 1.0f, // SpriteEffects.None, 1); float tBorder = Vector2.Transform(new Vector2(0, 0), Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)).Y; float bBorder = Vector2.Transform(new Vector2(0, SceneManager.GraphicsDevice.Viewport.Height), Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)).Y; float lBorder = Vector2.Transform(new Vector2(0, 0), Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)).X; float rBorder = Vector2.Transform(new Vector2(SceneManager.GraphicsDevice.Viewport.Width, 0), Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)).X; Rectangle dispRect = new Rectangle() { X = (int)lBorder, Y = (int)tBorder, Width = (int)rBorder - (int)lBorder, Height = (int)bBorder - (int)tBorder }; //spriteBatch.Draw(texture, dispRect, null, Color, 0, Vector2.Zero, spriteEffect, 1); spriteBatch.Draw(texture, new Vector2(lBorder, tBorder), new Rectangle((int)SceneManager.ActiveCamera.Position.X, (int)SceneManager.ActiveCamera.Position.Y, dispRect.Width, dispRect.Height), Color, 0, Vector2.Zero, Transform.Scale, spriteEffect, 1); } else if (displayMode == DisplayModes.PositionTile) { //Vector2 initialPosition = Vector2.Transform(Vector2.Zero, Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)); //Vector2 endPos = Vector2.Transform(new Vector2(SceneManager.GraphicsDevice.Viewport.Width, SceneManager.GraphicsDevice.Viewport.Height), Matrix.Invert(SceneManager.ActiveCamera.TransformMatrix)); //int sizeWidth = (int)(endPos.X - initialPosition.X); //int sizeHeight = (int)(endPos.Y - initialPosition.Y); spriteBatch.Begin(SpriteSortMode.Deferred, this.blendState, SamplerState.LinearWrap, null, null, null, SceneManager.ActiveCamera.TransformMatrix); //spriteBatch.Draw(texture, new Vector2(initialPosition.X, initialPosition.Y), new Rectangle((int)Transform.Position.X, // (int)Transform.Position.Y, sizeWidth, sizeHeight), Color, 0, Vector2.Zero, 1.0f, // SpriteEffects.None, 1); spriteBatch.Draw(texture, new Vector2((int)SceneManager.ActiveCamera.Position.X - SceneManager.GraphicsDevice.Viewport.Width, (int)SceneManager.ActiveCamera.Position.Y - SceneManager.GraphicsDevice.Viewport.Height), new Rectangle((int)Transform.Position.X, (int)Transform.Position.Y, SceneManager.GraphicsDevice.Viewport.Width * 2, SceneManager.GraphicsDevice.Viewport.Height * 2), Color, 0, Vector2.Zero, Transform.Scale, spriteEffect, 1); } else if (displayMode == DisplayModes.Fill) { Rectangle fill = new Rectangle( (int)SceneManager.ActiveScene.Camera.Position.X, (int)SceneManager.ActiveScene.Camera.Position.Y - 1, (int)SceneManager.GameProject.Settings.ScreenWidth, (int)SceneManager.GameProject.Settings.ScreenHeight); spriteBatch.Begin(SpriteSortMode.Deferred, this.blendState, SamplerState.LinearClamp, null, null, null, SceneManager.ActiveCamera.TransformMatrix); if (sourceRectangle == Rectangle.Empty) { spriteBatch.Draw(texture, fill, null, Color, 0, _orgx, spriteEffect, 0); } else { spriteBatch.Draw(texture, fill, sourceRectangle, Color, 0, _orgx, spriteEffect, 0); } } else { spriteBatch.Begin(SpriteSortMode.Deferred, this.blendState, SamplerState.LinearClamp, null, RasterizerState.CullNone, null, SceneManager.ActiveCamera.TransformMatrix); //Console.WriteLine("rr: " + Transform.Rotation); if (sourceRectangle == Rectangle.Empty) { spriteBatch.Draw(texture, Transform.Position, null, color, Transform.Rotation, _orgx, Transform.Scale, spriteEffect, 1); } else { spriteBatch.Draw(texture, Transform.Position, sourceRectangle, color, Transform.Rotation, _orgx, Transform.Scale, spriteEffect, 1); } } spriteBatch.End(); } }