-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.moon
More file actions
734 lines (562 loc) · 15 KB
/
player.moon
File metadata and controls
734 lines (562 loc) · 15 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
{graphics: g} = love
import
BloodEmitter
DirtEmitter
DustEmitter
GibEmitter
from require "particles"
S = (name, fn) ->
with Sequence fn
.name = name
export show_properties = (t) ->
require("moon").p { k,v for k,v in pairs t when type(v) != "table" }
-- on_ground: is on ground
-- wall_running: is wall running
-- can_wall_jump: allowed to wall jump, set to true when jump key released
-- ledge_grabbing: currently attached to ledge
class Player extends Entity
is_player: true
speed: 100
run_speed: 160
jump_power: 200
on_ground: false
movement_locked: false
dampen_movement: 1
run_scale: 1
step_time: 0
step_rate: 0.2
alpha: 255
w: 10
h: 20
lazy sprite: -> Spriter "images/protagonist.png"
new: (@controller, x,y) =>
super x, y
@was_down = {}
@seqs = DrawList!
@seqs.annotate = true
@effects = EffectList!
@velocity = Vec2d 0,0
@facing = "left"
rate = 0.08
with @sprite
@anim = StateAnim "stand_left", {
stand_left: \seq {
"6,9,21,26"
ox: 6
oy: 3
flip_x: true
}
stand_right: \seq {
ox: 5
oy: 3
"6,9,21,26"
}
jump_left: \seq {
"69,10,21,26"
ox: 6
oy: 3
flip_x: true
}
jump_right: \seq {
ox: 5
oy: 3
"69,10,21,26"
}
run_left: \seq {
"6,72,21,26"
"38,72,21,26"
"70,72,21,26"
"102,72,21,26"
ox: 5
oy: 4
flip_x: true
:rate
}
run_right: \seq {
"6,72,21,26"
"38,72,21,26"
"70,72,21,26"
"102,72,21,26"
ox: 4
oy: 4
:rate
}
wall_run_left: \seq {
"14,97,21,26"
"45,97,21,26"
"77,97,21,26"
"109,97,21,26"
ox: 3
oy: 4
flip_x: true
:rate
}
wall_run_right: \seq {
"14,97,21,26"
"45,97,21,26"
"77,97,21,26"
"109,97,21,26"
ox: 9
oy: 4
:rate
}
attack_left: \seq {
"7,171,34,22"
"71,171,34,22"
"135,171,34,22"
"198,171,34,22"
ox: 19
oy: 1
flip_x: true
:rate
}
attack_right: \seq {
"7,171,34,22"
"71,171,34,22"
"135,171,34,22"
"198,171,34,22"
ox: 5
oy: 1
:rate
}
grab_left: \seq {
"135,9,21,26"
ox: 5
oy: 7
}
grab_right: \seq {
"135,9,21,26"
flip_x: true
ox: 6
oy: 7
}
down_stab_right: \seq {
"105,233,18,26"
"73,233,18,26"
"39,233,18,26"
"9,233,18,26"
once: true
:rate
ox: 3
oy: 3
}
down_stab_left: \seq {
"105,233,18,26"
"73,233,18,26"
"39,233,18,26"
"9,233,18,26"
once: true
:rate
flip_x: true
ox: 5
oy: 3
}
}
set_color: (r,g,b) =>
import ColorShader from require "shaders"
@shader = ColorShader {
240/255,0,0
}, {
r/255,g/255,b/255
}
draw: (...) =>
if DEBUG
if @wall_running
COLOR\push 100,255,100, 50
else
COLOR\pusha 50
super ...
COLOR\pop!
COLOR\pusha @alpha if @alpha != 255
@effects\before!
if @shader
@shader\render ->
@anim\draw @x, @y
else
@anim\draw @x, @y
@effects\after!
COLOR\pop! if @alpha != 255
if DEBUG and @attack_box
Box.outline @attack_box
update: (dt, @world) =>
@effects\update dt
if @on_ground and not @attacking
@anim\update dt * @run_scale
else
@anim\update dt
@seqs\update dt, @world
if @ledge_grabbing
@update_for_ledge_grab dt
elseif @wall_running
@update_for_wall_run dt
else
@update_for_gravity dt
if not @can_attack
@can_attack = not @controller\is_down "attack"
@position_attack_box!
if @step_time > @step_rate
AUDIO\play "step"
while @step_time > @step_rate
@step_time -= @step_rate
true
update_for_ledge_grab: (dt) =>
dx, dy = unpack @controller\movement_vector! * dt * @speed
@on_ground = false
dir = @ledge_grabbing.is_left and "left" or "right"
@anim\set_state "grab_#{dir}"
@world.map
@ledge_grabbing.tid
@y = @ledge_grabbing.tile.y
if not @can_ledge_jump
@can_ledge_jump = not @controller\is_down "jump"
if @can_ledge_jump and @controller\is_down "jump"
@ledge_jump dx, dy
update_for_wall_run: (dt) =>
dx, dy = unpack @controller\movement_vector! * dt * @speed
@on_ground = false
@step_time += dt
@anim\set_state "wall_run_#{@wall_run_up_key}"
unless @feet_emitter
@feet_emitter = with DirtEmitter @world, @x, @y, @wall_run_up_key
.update = (...) ->
unless @wall_running
@feet_emitter = false
return false
DirtEmitter.update ...
@world.particles\add @feet_emitter
@feet_emitter.x, @feet_emitter.y = @feet_position!
if @controller\is_down @wall_run_up_key
@velocity[2] = -math.abs(dx) * @speed
else
if @velocity[2] < 0
@velocity[2] = 0
@velocity += @world.gravity * dt
if not @can_wall_jump
@can_wall_jump = not @controller\is_down "jump"
if @can_wall_jump and @controller\is_down "jump"
@jump @world
cx, cy = @fit_move @velocity[1] * dt, @velocity[2] * dt, @world
-- see if hit ledge zone
for zone in *@world.ledge_zones\get_touching @
@ledge_grab zone
break
moving_away = if @wall_run_up_key == "left"
dx > 0
else
dx < 0
-- stop if moving away, not against wall anymore or moving opposite direction
if not @against_wall(@wall_run_up_key) or cy or moving_away
@seqs\remove @wall_running
@end_wall_run!
-- slow the dx based on how long they've been holding direciton
movement_vector: (dt) =>
dx, dy = unpack @controller\movement_vector!
left_down = @controller\is_down "left"
right_down = @controller\is_down "right"
if left_down and not @was_down.left
@step_time = @step_rate
if right_down and not @was_down.right
@step_time = @step_rate
@was_down.left = left_down
@was_down.right = right_down
if not left_down and not right_down
@step_time = 0
if left_down
if not @left_down_time
@left_down_time = 0
else
@left_down_time = nil
if right_down
unless @right_down_time
@right_down_time = 0
else
@right_down_time = nil
if @left_down_time and @on_ground
@left_down_time += dt
@step_time += dt
if @right_down_time and @on_ground
@right_down_time += dt
@step_time += dt
accel_time = 0.2
elapsed = if dx > 0
@right_down_time
elseif dx < 0
@left_down_time
if elapsed
@run_scale = math.min(accel_time, elapsed) / accel_time * 0.4 + 0.6
dx, dy
update_for_gravity: (dt) =>
dx, dy = if @taking_hit or @dying or @stunned
0,0
else
@movement_vector dt
if dx != 0
@facing = if dx < 0 then "left" else "right"
unless @taking_hit or @dying or @stunned
if not @jumping and @controller\is_down "jump"
@jump!
elseif not @attacking and @controller\is_down "attack"
@attack not @on_ground and @controller\direction_is_down "down"
motion = if @attacking
"attack"
elseif not @on_ground
"jump"
elseif dx != 0
"run"
else
"stand"
if @stab_attacking
@anim\set_state "down_stab_#{@stab_attacking}"
else
@anim\set_state "#{motion}_#{@facing}"
@velocity += @world.gravity * dt
-- air resistance
if @velocity[1] != 0
@velocity[1] = dampen @velocity[1], dt * 200
vx, vy = unpack @velocity
vx += dx * @run_scale * @run_speed * @dampen_movement
cx, cy = @fit_move vx * dt, vy * dt, @world
if cx
against, wx, wy = @against_wall @facing
if against
root_tile = @world.map\get_wall_root wx, wy
-- try to initiate wall run
@wall_run root_tile
if cy
if @velocity[2] > 0
if not @on_ground
if @stab_attacking or @velocity[2] > 300
@world.particles\add DustEmitter @world, @x + @w/2 , @y + @h
-- change shake intensity based on hit velocity
p = math.min 1, math.max 0, (math.abs(@velocity[2]) - 300) / 200
p /= 2 unless @stab_attackin
AUDIO\play "land"
@world.viewport\shake 0.2 + (.3 * p), 5, 1.5 + (3.5 * p)
@on_ground = true
@velocity[2] = 0
else
if math.floor(@velocity[2] * dt) != 0
@on_ground = false
if @on_ground == true
@last_wall_tile = nil
feet_position: =>
return unless @wall_running
if @wall_run_up_key == "left"
@x, @y + @h - 5
else
@x + @w, @y + @h - 5
wall_test_coords: (dir) =>
ep = 0.1
wy = @y + @h * 4 / 5 -- around the feet?
wx = switch dir
when "left"
@x - ep
when "right"
@x + @w + ep
else
error "unknown dir #{dir}"
wx, wy
against_wall: (dir) =>
return false if @stab_attacking
wx, wy = @wall_test_coords dir
return false unless @world.map\collides_pt wx, wy
{:map} = @world
solid = map.layers[map.solid_layer]
idx = map\pt_to_idx wx, wy
switch dir
when "left" -- moving left
moved = map\move_idx idx, 1, 1
return false if solid[moved]
when "right" -- moving right
moved = map\move_idx idx, -1, 1
return false if solid[moved]
else
error "unknown direction #{dir}"
true, wx, wy
ledge_grab: (zone) =>
@end_wall_run!
AUDIO\play "grab"
@ledge_grabbing = zone
@can_ledge_jump = false
wall_run: (wall_tile) =>
return if @on_ground
return if @wall_running
return if wall_tile == @last_wall_tile
@last_wall_tile = wall_tile
-- cancel the jump
if @jumping
@seqs\remove @jumping
@jumping = false
-- cancel attack
@end_attack!
@step_time = @step_rate
@wall_running = @seqs\add S "wall run", ->
@wall_run_up_key = @facing
wait 0.5
@end_wall_run!
end_wall_run: =>
if @wall_running
@seqs\remove @wall_running
@wall_running = false
@velocity[2] = math.max 0, @velocity[2]
position_attack_box: =>
return unless @attack_box
if @stab_attacking
@attack_box.x = @x - 1
@attack_box.y = @y + 10
else
dist = 5
@attack_box.y = @y + (@h - @attack_box.h) / 2
if @facing == "left"
@attack_box.x = @x - @attack_box.w - 5
else
@attack_box.x = @x + @w + 5
attack: (downward) =>
return if @attacking
return unless @can_attack
AUDIO\play "player_attack"
if downward
@down_attack!
else
@horizontal_attack!
down_attack: =>
@stab_attacking = @facing
@attacking = @seqs\add S "stab attack", ->
wait 0.08 * 2
@velocity[2] += 100
@attack_box = Box 0, 0, @w + 2, 15
@position_attack_box!
@can_attack = false
wait_until -> @on_ground or @wall_running
@stunned = true
@attack_box = nil
if @on_ground
wait 0.2
@stunned = false
@end_attack!
horizontal_attack: =>
@attacking = @seqs\add S "attack", ->
wait 0.08 * 1
@attack_box = Box 0, 0, 15, 10
@position_attack_box!
wait 0.08 * 1
@attack_box = nil
wait 0.08 * 2
@end_attack!
end_attack: =>
return unless @attacking
@seqs\remove @attacking
@attack_box = nil
@attacking = false
@stab_attacking = false
ledge_jump: (dx, dy) =>
return if @jumping
return unless @ledge_grabbing
is_left = @ledge_grabbing.is_left
AUDIO\play "jump"
@ledge_grabbing = false
@jumping = @seqs\add S "ledge jump", ->
-- pressing down or nothing, just drop to the ground
vx, vy = if dx == 0 and dy > 0
0,0
elseif dx < 0 and is_left
vx = -@jump_power / 2
vy = -@jump_power
elseif dx > 1 and not is_left
vx = @jump_power / 2
vy = -@jump_power
else
vx = 0
vy = -@jump_power
@can_wall_jump = false
@velocity[1] = vx
@velocity[2] = vy
wait 0.1
@jumping = false
jump: =>
return if @jumping
return unless @on_ground or @wall_running
AUDIO\play "jump"
@jumping = @seqs\add S "ground jump", ->
vx, vy = if @wall_running
@end_wall_run!
@slow_movement_for 0.3
if @wall_run_up_key == "left"
100, -@jump_power
else
-100, -@jump_power
else
0, -@jump_power
@can_wall_jump = false
@velocity[1] = vx
@velocity[2] = vy
wait 0.1
@jumping = false
slow_movement_for: (duration) =>
if @_dampen_seq
@seqs\remove @_dampen_seq
@_dampen_seq = @seqs\add S "dampen", ->
@dampen_movement = 0
tween @, duration, dampen_movement: 1
looking_at: (viewport) =>
if @lost_dagger
return @lost_dagger\center!
cx, cy = @center!
if @facing == "left"
cx - 20, cy
else
cx + 20, cy
take_hit: (world, thing) =>
return if @taking_hit or @dying
@end_wall_run!
@end_attack!
hit_power = 150
if thing.is_bullet
thing\take_hit world, @
@taking_hit = @seqs\add S "take hit", ->
@effects\add ShakeEffect 0.2
@world.viewport\shake!
@world.particles\add BloodEmitter @world, @center!
-- get center to center vec
vx, vy = unpack (Vec2d(@center!) - Vec2d(thing\center!))\normalized! * hit_power
@velocity[1] = vx
@velocity[2] = vy / 2
@die! -- if should_die
wait 0.2
@taking_hit = false
after_hit: (world, thing) =>
if @stab_attacking
@world.viewport\shake!
@end_attack!
@velocity[2] = -100
die: =>
return if @dying
import Dagger from require "dagger"
@seqs\add S "gibs", ->
cx, cy = @center!
y = @y + @h
for i=1,6
@world.particles\add GibEmitter @world, cx , y
y -= 10
wait 0.1
@seqs\add S "bloodshow", ->
cx, cy = @center!
@world.particles\add DustEmitter @world, cx , @y + @h
for i=1,4 do
@world.particles\add BloodEmitter @world,
cx + rand(-5, 5),
cy + rand(-5, 5)
wait rand 0.1, 0.2
@dying = @seqs\add S "dying", ->
AUDIO\play "player_die"
@world\stop_audio!
await (done) ->
speed = 200 + 300 * random_normal!
dir = Vec2d(0, -1)\random_heading(140) * speed
@lost_dagger = Dagger @x, @y, dir, done
@lost_dagger.shader = @shader
@world.particles\add @lost_dagger
tween @, 0.5, alpha: 0
wait 1.0
@world.game\game_over!
{ :Player }