1 module dgt.particle; 2 import dgt.geom, dgt.texture, dgt.util; 3 4 /** 5 Controls the behavior of particles when they collide with a tilemap 6 7 Ignore means the particles will continue as normal, Die will mean the particle disappears, and Bounce will mean the particle rebounds from the wall 8 */ 9 enum ParticleBehavior 10 { 11 Ignore, Die, Bounce 12 } 13 14 /** 15 An individual instance of a particle 16 17 If you want to spawn particles use Window.addParticleBurst 18 */ 19 struct Particle 20 { 21 Texture region; 22 Vector position, velocity, acceleration, scale, scale_velocity; 23 float rotation = 0, rotational_velocity = 0; 24 int lifetime = 0; 25 ParticleBehavior behavior = ParticleBehavior.Ignore; 26 27 ///Step a particle forward a frame 28 @safe @nogc nothrow pure public void update() 29 { 30 velocity = velocity + acceleration; 31 position = position + velocity; 32 scale = scale + scale_velocity; 33 rotation += rotational_velocity; 34 lifetime--; 35 } 36 } 37 38 /** 39 A structure that allows particle spawn settings to be tweaked 40 */ 41 struct ParticleEmitter 42 { 43 const(Texture[]) regions; 44 Vector top_left, bottom_right, velocity_min, velocity_max, 45 acceleration_min, acceleration_max, scale_min, scale_max, 46 scale_velocity_min, scale_velocity_max; 47 float rotation_min = 0, rotation_max = 0, rotational_velocity_min = 0, 48 rotational_velocity_max = 0; 49 int lifetime_min, lifetime_max; 50 int particle_min, particle_max; 51 ParticleBehavior behavior = ParticleBehavior.Ignore; 52 53 @disable this(); 54 55 @nogc nothrow public: 56 /** 57 A list of all possible texture regions that the particles can source from 58 */ 59 this(in Texture[] regions) 60 { 61 this.regions = regions; 62 } 63 64 ///Create a single particle 65 Particle emit() const 66 { 67 return Particle(regions[randomRange(0, cast(int)regions.length)], 68 randomRange(top_left, bottom_right), 69 randomRange(velocity_min, velocity_max), 70 randomRange(acceleration_min, acceleration_max), 71 randomRange(scale_min, scale_max), 72 randomRange(scale_velocity_min, scale_velocity_max), 73 randomRange(rotation_min, rotation_max), 74 randomRange(rotational_velocity_min, rotational_velocity_max), 75 randomRange(lifetime_min, lifetime_max), 76 behavior); 77 } 78 }