Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using SFML.System;
- using SFML.Graphics;
- using SFML.Window;
- namespace Example
- {
- class MainClass
- {
- private static RenderWindow _window;
- public static void Main(string[] args)
- {
- Random R = new Random();
- _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);
- particles.SetEmitter(new Vector2f(mouse.X, mouse.Y));
- particles.Update(Time.FromMilliseconds(50), R);
- _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(uint count)
- {
- m_particles = new List<Particle>((int)count);
- m_emitter = new Vector2f(0, 0);
- m_vertices = new VertexArray(PrimitiveType.Points);
- for (int i = 0; i != count; i++)
- {
- m_vertices.Append(new Vertex(m_emitter, Color.White));
- }
- m_lifetime = Time.FromSeconds(3);
- for (int i = 0; i < count; i++)
- {
- m_particles.Add(new Particle());
- }
- }
- public void Draw(RenderTarget target, RenderStates states)
- {
- states.Texture = null;
- states.Transform *= Transform;
- target.Draw(m_vertices, states);
- }
- public void Update(Time Elapsed, Random R)
- {
- for(int i = 0; i != m_particles.Count(); i++)
- {
- var temp = m_vertices[(uint)i];
- m_particles[i].lifetime -= Elapsed / 2;
- if (m_particles[i].lifetime <= Time.Zero)
- {
- float Angle = ((R.Next() % 360) * 3.14f) / 180;
- float Speed = ((R.Next() % 50) + 50) / 3f;
- m_particles[i].velocity = new Vector2f(
- (float)Math.Cos(Angle) * Speed,
- (float)Math.Sin(Angle) * Speed);
- m_particles[i].lifetime = Time.FromMilliseconds((R.Next()%2000)+1000);
- temp.Color = Color.White;
- temp.Position = m_emitter;
- }
- temp.Position += m_particles[i].velocity * .125f;
- float ratio =
- m_particles[i].lifetime.AsSeconds() / m_lifetime.AsSeconds();
- temp.Color.A = (byte)(ratio * 255);
- m_vertices[(uint)i] = temp;
- }
- }
- public void SetEmitter(Vector2f newEmitter)
- {
- m_emitter = newEmitter;
- }
- public class Particle
- {
- public Vector2f velocity;
- public Time lifetime;
- }
- }
- }
Add Comment
Please, Sign In to add comment