spriteBatch.Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix);
spriteBatch.Draw(Texture2D texture, Vector2D position, Rectangle sourceDest, Color color, float angle, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using System.Diagnostics; namespace ParticleSystem { public class Particle { public Texture2D Texture { get; set; } // public Vector2 Position { get; set; } // public Vector2 Velocity { get; set; } // public float Angle { get; set; } // public float AngularVelocity { get; set; } // public Vector4 Color { get; set; } // public float Size { get; set; } // public float SizeVel { get; set; } // public float AlphaVel { get; set; } // public int TTL { get; set; } // public Particle(Texture2D texture, Vector2 position, Vector2 velocity, float angle, float angularVelocity, Vector4 color, float size, int ttl, float sizeVel, float alphaVel) // { Texture = texture; Position = position; Velocity = velocity; Angle = angle; Color = color; AngularVelocity = angularVelocity; Size = size; SizeVel = sizeVel; AlphaVel = alphaVel; TTL = ttl; } public void Update() // { TTL--; // // Position += Velocity; Angle += AngularVelocity; Size += SizeVel; Color = new Vector4(Color.X, Color.Y, Color.Z, Color.W - AlphaVel); // . , Vector4, Color, : Color.R/G/B Byte ( 0x00 0xFF), , float Vector4 } public void Draw(SpriteBatch spriteBatch) { Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height); // : Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2); // spriteBatch.Draw(Texture, Position, sourceRectangle, new Color(Color), Angle, origin, Size, SpriteEffects.None, 0); // } } }
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using System.Diagnostics; namespace ParticleSystem { class ParticleController { public List<Particle> particles; private Texture2D dot; // private Texture2D smoke; // private Random random; public ParticleController() { this.particles = new List<Particle>(); random = new Random(); } public void LoadContent(ContentManager Manager) { dot = Manager.Load<Texture2D>("spark"); smoke = Manager.Load<Texture2D>("smoke"); } public void EngineRocket(Vector2 position) // , { for (int a = 0; a < 2; a++) // 2 { Vector2 velocity = AngleToV2((float)(Math.PI * 2d * random.NextDouble()), 0.6f); float angle = 0; float angleVel = 0; Vector4 color = new Vector4(1f, 1f, 1f, 1f); float size = 1f; int ttl = 40; float sizeVel = 0; float alphaVel = 0; GenerateNewParticle(smoke, position, velocity, angle, angleVel, color, size, ttl, sizeVel, alphaVel); } for (int a = 0; a < 1; a++) // 1 { Vector2 velocity = AngleToV2((float)(Math.PI * 2d * random.NextDouble()), .2f); float angle = 0; float angleVel = 0; Vector4 color = new Vector4(1.0f, 0.5f, 0.5f, 0.5f); float size = 1f; int ttl = 80; float sizeVel = 0; float alphaVel = .01f; GenerateNewParticle(dot, position, velocity, angle, angleVel, color, size, ttl, sizeVel, alphaVel); } for (int a = 0; a < 10; a++) // 10 , — { Vector2 velocity = Vector2.Zero; float angle = 0; float angleVel = 0; Vector4 color = new Vector4(1.0f, 0.5f, 0.5f, 1f); float size = 0.1f + 1.8f * (float)random.NextDouble(); int ttl = 10; float sizeVel = -.05f; float alphaVel = .01f; GenerateNewParticle(smoke, position, velocity, angle, angleVel, color, size, ttl, sizeVel, alphaVel); } } private Particle GenerateNewParticle(Texture2D texture, Vector2 position, Vector2 velocity, float angle, float angularVelocity, Vector4 color, float size, int ttl, float sizeVel, float alphaVel) // { Particle particle = new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl, sizeVel, alphaVel); particles.Add(particle); return particle; } public void Update(GameTime gameTime) { for (int particle = 0; particle < particles.Count; particle++) { particles[particle].Update(); if (particles[particle].Size <= 0 || particles[particle].TTL <= 0) // , { particles.RemoveAt(particle); particle--; } } } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive); // Addictive for (int index = 0; index < particles.Count; index++) // { particles[index].Draw(spriteBatch); } spriteBatch.End(); } public Vector2 AngleToV2(float angle, float length) { Vector2 direction = Vector2.Zero; direction.X = (float)Math.Cos(angle) * length; direction.Y = -(float)Math.Sin(angle) * length; return direction; } } }
particleController.EngineRocket(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
Source: https://habr.com/ru/post/128286/
All Articles