-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparticles.moon
More file actions
154 lines (112 loc) · 3 KB
/
particles.moon
File metadata and controls
154 lines (112 loc) · 3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{graphics: g} = love
import random from math
export *
linear_step = (p, left, right, start=0, stop=1) ->
return left if p <= start
return right if p >= stop
p = (p - start) / (stop - start)
right * p + left * (1 - p)
spread_dir = (dir, spread) ->
dir\rotate (random! - 0.5) * spread
class NumberParticle extends Particle
life: 0.8
speed: 100
spread: math.pi / 2
new: (x, y, @str) =>
super x,y
rad = math.pi/2 + (random! - 0.5) * @spread
@vel = Vec2d.from_radians(rad) * -@speed
@accel = Vec2d 0, 400
@dr = (random! - 0.5) * @spread
draw: =>
p = @p!
a = linear_step p, 255, 0, 0.5
g.setColor @r, @g, @b, a
g.push!
g.translate @x, @y
g.print @str, 0,0, p * @dr, nil, nil, 4,4
g.pop!
class SparkEmitter extends Emitter
spread: math.pi / 8
speed: 60
P = class extends PixelParticle
size: 1
life: 0.3
make_particle: (x,y) =>
P x,y, spread_dir @dir * @speed, @spread
new: (world, x,y, @dir) =>
super world, x,y
class EnergyEmitter extends Emitter
P = class extends Particle
life: 0.5
ox: 3
oy: 3
sprite: "117,20,7,7"
new: (...) =>
@rot = random! * math.pi / 2
super ...
update: (dt, ...) =>
dampen_vector @vel, dt * 200
super dt, ...
draw: =>
p = @p!
a = (1 - p) * 255
g.setColor @r, @g, @b, a
sprite\draw @sprite, @x, @y, p * @rot, nil, nil, @ox, @oy
new: (world, x, y) =>
super world, x, y, 0.1, 10
make_particle: (x,y) =>
rads = random! * math.pi * 2
P x,y, Vec2d.from_radians(rads) * 100
class Explosion extends Sequence
alive: true
Smoke: class extends Particle
sprite: "64,16,16,16"
new: (...) =>
@rot = random! * math.pi / 2
super ...
@vel = Vec2d.random! * (random! * 20 + 20)
@accel = @vel * -1
draw: =>
p = @p!
a = (1 - p) * 255
g.setColor @r, @g, @b, a
scale = 1 + p * 0.5
sprite\draw @sprite, @x, @y, p * @rot, scale, scale, 8, 8
Flare: class extends Particle
sprite: "48,32,32,32"
draw: =>
p = @p!
a = if p < 0.5
p * 2 * 255
else
(1 - p) * 2 * 255
scale = @p! * 2 + 0.3
g.setColor @r, @g, @b, a
sprite\draw @sprite, @x, @y, nil, scale, scale, 16, 16
Fire: class extends Particle
sequence: { 3,4,5,6,7,8,9,10,11 }
new: (x,y) =>
super x,y
@anim = Animator sprite, @sequence, 0.05
@anim.once = true
update: (dt) =>
@anim\update dt
super dt
draw: =>
a = linear_step @p!, 255,0, 0.5
g.setColor @r, @g, @b, a
@anim\draw @x - 8, @y - 8
new: (@world, x, y) =>
super ->
sfx\play "boom"
@world.particles\add @.Fire x, y
@world.particles\add @.Flare x, y
for i=1,3
@world.particles\add @.Smoke x, y
for i=1,4
wait 0.1
rads = random! * math.pi * 2
offset = Vec2d.from_radians(rads) * (random! * 5 + 5)
@world.particles\add @.Fire x + offset.x, y + offset.y
draw: =>