/
particles.moon
103 lines (72 loc) · 1.66 KB
/
particles.moon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{ graphics: g } = love
{ :cos, :sin, :pi } = math
export *
lazy_value Particle, "sprite", ->
Spriter "img/sprite.png", 32, 32
class Spark extends Particle
life: 2.0
speed: 300
gravity: 300
cell: 0
min_mod: -1.0
max_mod: 1.0
new: (...) =>
super ...
@time = 0
@mod = rand @min_mod, @max_mod
@accel[2] = @gravity
draw: =>
g.push!
g.translate @x, @y
g.rotate @time * @mod
s = @mod + 1.5
g.scale s,s
g.setColor @r, @g, @b, 255 * (1 - @p!)
@sprite\draw @cell, -16, -16
g.pop!
update: (dt, ...) =>
@time += dt
super dt, ...
class Blood extends Spark
gravity: 700
life: 1.0
min_mod: 0
cell: 1
class Shrapnel extends Spark
min_mod: 0
max_mod: 2
cell: 2
class Smoke extends Spark
speed: 200
gravity: -400
life: 2.0
min_mod: 1
max_mod: 3
cell: 3
class Flare extends Spark
speed: 200
gravity: -400
life: 0.8
min_mod: 1
max_mod: 3
cell: 4
class DirectionalEmitter extends Emitter
type: nil
width: 1
new: (@dir, ...) =>
super ...
make_particle: (x,y) =>
particle_cls = @type
particle_cls x,y, @dir\rotate(rand -@width, @width) * particle_cls.speed
class Sparks extends DirectionalEmitter
type: Spark
class BloodSquirt extends DirectionalEmitter
type: Blood
class Explosion extends Emitter
make_particle: (x,y) =>
with @world.particles
\add Smoke x,y, Vec2d(0,1)\rotate(rand 0, math.pi*2) * Smoke.speed
\add Spark x,y, Vec2d(0,1)\rotate(rand 0, math.pi*2) * Spark.speed
\add Flare x,y, Vec2d(0,1)\rotate(rand 0, math.pi*2) * Flare.speed
Shrapnel x,y, Vec2d(0,1)\rotate(rand 0, math.pi*2) * Shrapnel.speed
nil