-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.moon
More file actions
323 lines (239 loc) · 5.88 KB
/
player.moon
File metadata and controls
323 lines (239 loc) · 5.88 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{graphics: g} = love
import BigExplosionEmitter from require "emitters"
class Powerup extends Entity
mixin HasEffects
lazy sprite: => Spriter "images/sprites.png", 16, 16
w: 13
h: 13
speed: 10
new: (...) =>
super ...
@anim = @sprite\seq {3,5}, 0.2
@vel = Vec2d(-@speed, 0)
take_hit: (thing) =>
return if @dying
if thing == @world.player
AUDIO\play "powerup"
@dying = true
@world.player\collect_powerup powerup
@effects\add BlowOutEffect 0.2, -> @alive = false
update: (dt, @world) =>
@move unpack dt * @vel
@anim\update dt
@alive and @x > -@w
draw: =>
oy = 2 * math.sin(5 * love.timer.getTime!)
blend = g.getBlendMode!
g.setBlendMode "additive"
@anim\draw @x - 2, @y - 2 + oy
g.setBlendMode blend
if DEBUG
super {255, 100,100, 100}
class BulletHitParticle extends ImageParticle
w: 16
h: 16
quad: 2
lazy sprite: => Spriter "images/sprites.png", 16, 16
class BulletHitEmitter extends Emitter
new: (@dir=Vec2d(0, -1), ...) =>
super ...
make_particle: =>
with BulletHitParticle @x, @y
.vel = (@dir * 100)\random_heading(80)
.accel = -1.1 * .vel
class Bullet extends Entity
is_bullet: true
w: 5
h: 5
speed: 200
ox: 7
oy: 7
lazy sprite: => Spriter "images/sprites.png", 16, 16
new: (@life, ...) =>
super ...
@vel[1] = @speed
on_stuck: =>
@alive = false
color: { 0, 255, 100 }
take_hit: (thing, world) =>
dir = (Vec2d(@center!) - Vec2d(thing\center!))\normalized!
world.particles\add BulletHitEmitter dir, world, @center!
@alive = false
update: (dt, world) =>
super dt, world
if world.map and world.map\collides @
AUDIO\play "bullet_hit_wall"
@alive = false
@life -= dt
alive = @life > 0
@alive and alive
draw: =>
@sprite\draw 1, @x - @ox, @y - @oy
class Option extends Entity
@locations: {
Vec2d 0, -12
Vec2d 0, 12
Vec2d -5, -24
Vec2d -5, 24
}
radius: 4
drot: 1
rot: 0
new: (@ship, @position, ...) =>
super ...
@seqs = DrawList!
update: (dt, @world) =>
tx, ty = unpack Vec2d(@ship\center!) + @position
tx -= @w/2
ty -= @w/2
@x = smooth_approach(@x, tx, dt * 10)
@y = smooth_approach(@y, ty, dt * 10)
@seqs\update dt
true
shoot: =>
return if @shoot_timer
x = @x + @w / 2 - Bullet.w / 2
y = @y + @h / 2 - Bullet.h / 2
if @world
@world.bullets\add Bullet @ship\bullet_life!, x,y
@shoot_timer = @seqs\add Sequence ->
wait @ship\bullet_rate!
@shoot_timer = nil
draw: =>
g.push!
g.translate @center!
g.circle "line", 0, 0, @radius, 5
g.pop!
class Player extends Entity
mixin HasEffects
color: {255, 255, 255}
is_player: true
speed: 60
shielded: false
immune: false
w: 10
h: 5
max_upgrades: {
speed: 3
distance: 3
shot: 3
option: 4
shield: 1
boom: 1
}
new: (...) =>
super ...
@seqs = DrawList!
@options = DrawList!
@upgrades = {
speed: 0
distance: 0
shot: 0
option: 0
shield: 0
boom: 0
}
bullet_life: =>
0.4 + 0.4 * @upgrades.distance * @upgrades.distance
bullet_rate: =>
0.3 / math.pow(1.5, @upgrades.shot)
collect_powerup: (powerup) =>
@world.hud\add_point!
add_option: =>
i = #@options + 1
position = Option.locations[i]
return unless position
@options\add Option @, position, @center!
die: =>
return if @immune
if @shielded
@immune = true
@shielded = false
@downgrade "shield"
@seqs\add Sequence ->
AUDIO\play "enemy_hit"
wait 0.1
AUDIO\play "lose_shield"
wait 0.9
@immune = false
return
return if @dying
AUDIO\play "player_die"
@dying = true
@world.particles\add BigExplosionEmitter @world, @center!
@effects\add BlowOutEffect 0.8, ->
@world\end_anim!
@alive = false
update: (dt, @world) =>
unless @dying
dir = CONTROLLER\movement_vector!
move = dir * (dt * (@speed + @upgrades.speed * 25))
dx, dy = unpack move
@fit_move dx, dy, @world
if CONTROLLER\is_down "shoot"
@shoot!
@seqs\update dt
@options\update dt, @world
@alive
shoot: =>
for option in *@options
option\shoot!
return if @shoot_timer
AUDIO\play "shoot"
x = @x + @w / 2 - Bullet.w / 2
y = @y + @h / 2 - Bullet.h / 2
@world.bullets\add Bullet @bullet_life!, x,y
@shoot_timer = @seqs\add Sequence ->
wait @bullet_rate!
@shoot_timer = nil
draw_ship_origin: =>
x = -@w/2
y = -@h/2
g.polygon "line",
x, y - @h / 2,
x + @w * 1.5, y + @h / 2,
x, y + @h + @h / 2
draw: =>
if @immune and not duty_on 0.4
return
g.polygon "line",
@x, @y - @h / 2,
@x + @w * 1.5, @y + @h / 2,
@x, @y + @h + @h / 2
if @shielded
for i=0,1
t = love.timer.getTime! + i * 0.5
t = t - math.floor(t)
COLOR\push 99,254,244, (1 - t) * 255
g.push!
g.translate @center!
g.scale 1 + t* 1.5, 1 + t * 1.5
@draw_ship_origin!
g.pop!
COLOR\pop!
@options\draw!
if DEBUG
super {255,0,0,100}
downgrade: (what) =>
@upgrades[what] = math.max 0, @upgrades[what] - 1
button = @world.hud\find_button what
button\set_level @upgrades[what], false
upgrade: (what) =>
AUDIO\play "upgrade"
return true if what == "boom"
if @upgrades[what] + 1 > @max_upgrades[what]
return false
@upgrades[what] += 1
is_max = @upgrades[what] == @max_upgrades[what]
switch what
when "option"
@add_option!
when "shield"
@shielded = true
button = @world.hud\find_button what
button\set_level @upgrades[what], is_max
true
take_hit: (thing, world) =>
if thing.is_enemy_bullet
@die!
{ :Player, :Powerup }