Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML;
- using SFML.System;
- using SFML.Graphics;
- using SFML.Window;
- namespace ConsoleApp1
- {
- class MainClass
- {
- private static RenderWindow _window;
- public static void Main(string[] args)
- {
- _window = new RenderWindow(new VideoMode(800, 600), "SFML window");
- ParticleSystem particles = new ParticleSystem(1000);
- Clock clock = new Clock();
- _window.SetVisible(true);
- _window.SetFramerateLimit(60);
- _window.Closed += new EventHandler(OnClosed);
- while (_window.IsOpen)
- {
- Vector2i mouse = Mouse.GetPosition(_window);
- //Console.WriteLine(mouse.X + " " + mouse.Y);
- particles.SetEmitter(new Vector2f(mouse.X, mouse.Y));
- particles.update(clock.Restart());
- _window.DispatchEvents();
- _window.Clear(Color.Black);
- _window.Draw(particles);
- _window.Display();
- }
- }
- private static void OnClosed(object sender, EventArgs e)
- {
- _window.Close();
- }
- }
- class ParticleSystem : Transformable, Drawable
- {
- public VertexArray m_vertices;
- public Time m_lifetime;
- public List<Particle> m_particles;
- public Vector2f m_emitter;
- public ParticleSystem(int count)
- {
- m_particles = new List<Particle>(count);
- m_vertices = new VertexArray(PrimitiveType.Points, (uint)count);
- m_lifetime = Time.FromSeconds(count);
- m_emitter = new Vector2f(0, 0);
- for(int i = 0; i<count; i++)
- {
- m_particles.Add(new Particle());
- resetParticle(i);
- }
- }
- public void Draw(RenderTarget target, RenderStates states)
- {
- states.Texture = null;
- states.Transform *= Transform;
- target.Draw(m_vertices, states);
- }
- public void SetEmitter(Vector2f newEmitter)
- {
- m_emitter = newEmitter;
- Console.WriteLine(newEmitter.X + " " + newEmitter.Y);
- }
- public void update(Time elapsed)
- {
- for (int i = 0; i < m_particles.Count; ++i)
- {
- // update the particle lifetime
- Particle p = m_particles[i];
- p.lifetime -= elapsed;
- m_particles[i] = p;
- // if the particle is dead, respawn it
- if (p.lifetime <= Time.Zero)
- resetParticle(i);
- // update the position of the corresponding vertex
- var a = m_vertices[(uint)i];
- a.Position += p.velocity * elapsed.AsSeconds();
- // update the alpha (transparency) of the particle according to its lifetime
- float ratio = p.lifetime.AsSeconds() / m_lifetime.AsSeconds();
- a.Color = Color.White;
- a.Color.A = (byte)(ratio * 255);
- m_vertices[(uint)i] = a;
- }
- }
- void resetParticle(int index)
- {
- // give a random velocity and lifetime to the particle
- float angle = (new Random().Next() % 360) * 3.14f / 180;
- float speed = (new Random().Next() % 50) + 50;
- var temp = m_particles[index];
- temp.velocity = new SFML.System.Vector2f((float)(Math.Cos(angle) * speed), (float)(Math.Sin(angle) * speed));
- temp.lifetime = Time.FromMilliseconds((new Random().Next() % 2000) + 1000);
- m_particles[index] = temp;
- // reset the position of the corresponding vertex
- var temp2 = m_vertices[(uint)index];
- temp2.Position = m_emitter;
- m_vertices[(uint)index] = temp2;
- }
- public class Particle
- {
- public Vector2f velocity;
- public Time lifetime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement