-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
player.lisp
1522 lines (1442 loc) · 69.2 KB
/
player.lisp
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(in-package #:org.shirakumo.fraf.kandria)
(define-event player-died (event))
(define-shader-entity stamina-wheel (vertex-entity standalone-shader-entity)
((vertex-array :initform (// 'kandria '16x))
(visibility :initform 0.0 :accessor visibility)))
(defmethod render :before ((stamina-wheel stamina-wheel) (program shader-program))
(let ((player (node 'player +world+)))
(translate-by (vx (location player)) (vy (location player)) -1000)
(scale-by -0.8 0.8 1)
(translate-by (* -20 (direction player)) 20 0)
(setf (uniform program "stamina") (max 0.0 (float (/ (climb-strength player) (p! climb-strength)) 0f0)))
(setf (uniform program "visibility") (clamp 0.0 (visibility stamina-wheel) 1.0))
(setf (uniform program "dash") (if (dash-exhausted player) 0.0 1.0))))
(define-class-shader (stamina-wheel :vertex-shader)
"layout (location = TRIAL_V_UV) in vec2 vertex_uv;
out vec2 uv;
void main(){
maybe_call_next_method();
uv = vertex_uv;
}")
(define-class-shader (stamina-wheel :fragment-shader)
"
#define PI 3.141592653589793
in vec2 uv;
out vec4 color;
uniform float stamina;
uniform float visibility;
uniform float dash;
vec2 rotate(vec2 p, float angle){
float sine = sin(angle);
float cosine = cos(angle);
return vec2(cosine * p.x + sine * p.y, cosine * p.y - sine * p.x);
}
float sdPie(in vec2 p, in float gap, in float rot, in float r){
p = rotate(p, rot);
vec2 c = vec2(sin(gap), cos(gap));
p.x = abs(p.x);
float l = length(p) - r;
float m = length(p-c*clamp(dot(p,c),0.0,r));
return max(l,m*sign(c.y*p.x-c.x*p.y));
}
float sdCirc(in vec2 p, in float r){
return length(p)-r;
}
vec4 evalSDF(in float sdf, in vec4 color){
float dsdf = fwidth(sdf) * 0.5;
return color * smoothstep(dsdf, -dsdf, sdf);
}
vec4 add(vec4 a, vec4 b){
return mix(a, b, b.a);
}
void main(){
maybe_call_next_method();
float b = stamina*PI;
vec2 p = uv-0.5;
float inner = -sdCirc(p, 0.225);
color = evalSDF(sdCirc(p, 0.4), vec4(0,0,0,0.5));
vec4 c = mix(vec4(1.0, 0, 0, 1), vec4(0.0,0.6,1.0,1), clamp(stamina*2-0.1, 0, 1));
if(stamina >= 0.9) c = vec4(1);
color = add(color, evalSDF(max(sdPie(p, b, b, 0.40), inner), c));
c = mix(vec4(1), vec4(0, 0, 0, 1), (sin(stamina*50))*0.5);
color = add(color, evalSDF(max(sdPie(p, 0.1, b*2, 0.45), inner), c));
color = add(color, evalSDF(sdCirc(p-0.4, 0.1), vec4(1-dash,dash,0,1)));
color *= visibility;
}")
(defstruct (map-marker
(:constructor make-map-marker (location &optional (label " ") (color colors:white)))
(:copier NIL)
(:predicate NIL))
(location NIL :type vec2)
(color NIL :type colored:rgb)
(label " " :type T))
(define-shader-entity player (alloy:observable stats-entity paletted-entity animatable profile ephemeral inventory)
((name :initform 'player)
(palette :initform (// 'kandria 'player-palette))
(bsize :initform (vec 7.0 15.0))
(spawn-location :initform (vec2 0 0) :accessor spawn-location)
(interactable :initform NIL :accessor interactable)
(jump-time :initform 1.0 :accessor jump-time)
(dash-time :initform 1.0 :accessor dash-time)
(run-time :initform 1.0 :accessor run-time)
(limp-time :initform 0.0 :accessor limp-time)
(look-time :initform 0.0 :accessor look-time)
(slide-time :initform 0.0 :accessor slide-time)
(climb-strength :initform 5.0 :accessor climb-strength)
(combat-time :initform 10000.0 :accessor combat-time)
(attack-held :initform NIL :accessor attack-held)
(used-aerial :initform NIL :accessor used-aerial)
(inertia-time :initform 0.0 :accessor inertia-time)
(inertia :initform (vec2 0 0) :accessor inertia)
(buffer :initform (cons NIL 0))
(prompt :initform (make-instance 'prompt) :reader prompt)
(prompt-b :initform (make-instance 'prompt) :reader prompt-b)
(profile-sprite-data :initform (asset 'kandria 'player-profile))
(nametag :initform (@ player-nametag))
(invincible :initform (setting :gameplay :god-mode))
(dash-exhausted :initform NIL :accessor dash-exhausted)
(trace :initform (make-array (* 12 60 60 2) :element-type 'single-float :adjustable T :fill-pointer 0)
:accessor movement-trace)
(fishing-line :initform (make-instance 'fishing-line) :accessor fishing-line)
(stamina-wheel :initform (make-instance 'stamina-wheel) :accessor stamina-wheel)
(color :initform (vec 1 1 1 0) :accessor color)
(sword-level :initform 0 :initarg :sword-level :accessor sword-level)
(map-markers :initform () :initarg :map-maprkers :accessor map-markers))
(:default-initargs
:sprite-data (asset 'kandria 'player)))
(defmethod initialize-instance :after ((player player) &key)
(setf (active-p (action-set 'in-game)) T)
(setf (spawn-location player) (vcopy (location player))))
(defmethod register :after ((player player) (world scene))
(show-panel 'hud :player player)
(when (setting :gameplay :show-splits)
(show-panel 'splits)))
(defmethod deregister :after ((player player) (world scene))
(hide-panel 'hud)
(hide-panel 'splits))
(defmethod minimum-idle-time ((player player)) 30)
(defmethod base-health ((player player)) 1000)
(defmethod resize ((player player) w h))
(defmethod capable-p ((player player) (edge jump-node)) T)
(defmethod capable-p ((player player) (edge crawl-node)) T)
(defmethod capable-p ((player player) (edge climb-node)) T)
(defmethod movement-speed ((player player))
(case (state player)
(:crawling (p! crawl))
(:climbing (p! climb-up))
(T (p! walk-limit))))
(defmethod reset ((player player))
(reset (stats player))
(setf (fill-pointer (movement-trace player)) 0))
(defmethod stage :after ((player player) (area staging-area))
(dolist (sound '(player-dash player-jump player-evade player-die player-die-platforming
player-low-health player-awaken player-damage enter-water
player-red-flashing zombie-die ; Used for explosion
player-hard-land player-roll-land player-counter
player-pick-up player-enter-passage player-soft-land player-wall-slide
step-dirt-1 step-dirt-2 step-dirt-3 step-dirt-4
step-rocks-1 step-rocks-2 step-rocks-3 step-rocks-4
step-sand-1 step-sand-2 step-sand-3 step-sand-4
step-metal-1 step-metal-2 step-metal-3 step-metal-4
step-concrete-1 step-concrete-2 step-concrete-3 step-concrete-4
step-tall-grass-1 step-tall-grass-2 step-tall-grass-3 step-tall-grass-4
sword-small-slash-1 sword-small-slash-2 sword-small-slash-3 sword-big-slash
sword-hit-ground-soft sword-hit-ground-hard sword-jab
sword-rotating-swing-1 sword-rotating-swing-2 bomb-plant
sword-air-swing-1 sword-air-swing-2 sword-air-swing-3-jab
sword-hammer-swing-1 sword-hammer-swing-2
sword-overhead-swing-1 sword-overhead-swing-2 sword-overhead-swing-3-tornado))
(stage (// 'sound sound) area))
(stage (fishing-line player) area)
(stage (stamina-wheel player) area)
(stage (fishing-line player) (node 'render +world+))
(stage (// 'kandria 'line-part) area)
(stage (// 'kandria 'sting) area)
(stage (// 'kandria 'lights) area))
(defun interrupt-movement-trace (player &key death)
(let ((trace (movement-trace player)))
(declare (type (array single-float (*))))
(vector-push-extend (vx (location player)) trace)
(vector-push-extend (vy (location player)) trace)
(vector-push-extend (float-features:bits-single-float #b01111111110000000000000000000000) trace)
(if death
(vector-push-extend float-features:single-float-negative-infinity trace)
(vector-push-extend (float-features:bits-single-float #b01111111110000000000000000000000) trace))))
(defmethod hurt :after (thing (player player))
(let ((dir (nv- (vxy (hurtbox player)) (location thing))))
(trigger (make-instance 'sting-effect :angle (if (v/= 0 dir) (point-angle dir) 0)) player)))
(defmethod (setf medium) :before ((medium medium) (player player))
(when (or (and (typep (medium player) 'water)
(not (typep medium 'water)))
(and (not (typep (medium player) 'water))
(typep medium 'water)))
(harmony:play (// 'sound 'enter-water))))
(defmethod handle ((ev interact) (player player))
(let ((interactable (interactable player)))
(when (and interactable (null (path player)))
(setf (buffer player) NIL)
(interact interactable player))))
(defmethod interact :before ((thing dialog-entity) (player player))
(setf (state player) :normal)
(hide (prompt player)))
(defmethod interact :after ((thing item) (player player))
(award-experience player (experience-reward thing))
(unless (eql :crawling (state player))
(start-animation 'pickup player)))
(defmethod interact ((door door) (player player))
(unless (find (name (animation player)) '(enter enter-forward exit))
(setf (animation door) 'open)
(let ((location (location (target door))))
(labels ((teleport ()
(start-animation 'exit player)
(setf (animation (target door)) 'open)
(setf (air-time player) 0.0)
(setf (buffer player) NIL)
(interrupt-movement-trace player)
(setf (location player) (vec (vx location) (- (vy location) 5))))
(enter (player)
(start-animation (if (facing-towards-screen-p door) 'enter 'enter-forward) player)
(if (eq (chunk player) (find-chunk (target door)))
(teleport)
(transition
(teleport)
(issue +world+ 'force-lighting)
(setf (zoom (camera +world+)) 1.0)
(setf (intended-zoom (camera +world+)) 1.0)
(snap-to-target (camera +world+) player)))))
(cond ((move-to (v- (location door) (vec 0 (vy (bsize door)))) player)
(setf (pending-action player) #'enter))
(T
(enter player)))))))
(defmethod interact ((trigger teleport-trigger) (player player))
(when (primary trigger)
(let ((location (location (target trigger))))
(vsetf (velocity player) 0 0)
(transition
(interrupt-movement-trace player)
(setf (location player) location)
(issue +world+ 'force-lighting)
(clear-retained)
(snap-to-target (camera +world+) player)))))
(defmethod interact ((save-point save-point) (player player))
(setf (vx (location player))
(- (vx (location save-point)) 11))
(setf (direction player) +1)
(start-animation 'phone player)
(setf (animation save-point) 'call)
(save-state +main+ T :show T)
(status (@ game-save-complete)))
(defmethod enter :after ((player player) (water water))
(setf (dash-exhausted player) NIL))
(defmethod handle :after ((ev quickmenu) (player player))
(unless (or (path player)
(<= (health player) 0))
(setf (combat-time player) 0f0)
(toggle-panel 'quick-menu)))
(defmethod handle :after ((ev open-map) (player player))
(toggle-panel 'map-panel))
(defun handle-evasion (player)
(let ((endangering (endangering player)))
(when endangering
(setf (combat-time player) 0.0)
(let ((dir (float-sign (- (vx (location endangering)) (vx (location player))))))
(setf (direction player) dir)
(setf (iframes player) 1.0)
(start-animation 'evade-left player))
T)))
(defmethod handle ((ev dash) (player player))
(setf (limp-time player) (min (limp-time player) 1.0))
(unless (path player)
(case (state player)
(:crawling
(handle (make-instance 'crawl) player))
((:normal :climbing :sliding)
(cond ((handle-evasion player))
((null (dash-exhausted player))
(setf (dash-time player) 0.0)
(when (not (or (setting :gameplay :infinite-dash)
(typep (medium player) 'water)))
(setf (dash-exhausted player) T))
(when (and (eql :climbing (state player))
(v= 0 (velocity player)))
(setf (direction player) (- (direction player))))
(rumble :duration 0.2 :intensity 0.5)
(setf (state player) :dashing)
(setf (animation player) 'dash))
(T
(setf (buffer player) 'dash))))
(:dashing
(setf (buffer player) 'dash))
(:animated
;; Queue dash //except// for when we're being hit, as it's
;; unlikely the player will want to dash right after getting
;; hit.
(let ((name (name (animation player))))
(unless (or (eq name 'light-hit)
(eq name 'hard-hit))
(setf (buffer player) 'dash)))))))
(defmethod handle ((ev jump) (player player))
(cond ((path player))
((eql :animated (state player))
(setf (buffer player) 'jump))
((and (typep (svref (collisions player) 2) 'platform)
(retained 'down))
(decf (vy (location player)) 2))
((eql :crawling (state player))
(handle (make-instance 'crawl) player))
;; Prevent jumps on elevators that are too fast, as it just leads to stutter.
;; This stutter is unavoidable as air friction slows the player down instantly
;; causing the elevator to slam into them again.
((and (typep (svref (collisions player) 2) 'moving-platform)
(< 5.0 (vy (velocity (svref (collisions player) 2))))))
(T
(when (< 0 (inertia-time player))
(setf (inertia-time player) -1.0))
(setf (jump-time player) (- (p! coyote-time))))))
(defmethod handle ((ev crawl) (player player))
(unless (path player)
(case (state player)
(:normal
(when (svref (collisions player) 2)
(setf (state player) :crawling))))))
(defmethod handle ((ev light-attack) (player player))
(cond ((path player))
((eql :dying (state player)))
((eql :respawning (state player)))
((or (eql :animated (state player))
(eql :dashing (state player)))
(setf (buffer player) 'light-attack))
((eql :crawling (state player))
(handle (make-instance 'crawl) player))
((and (null (svref (collisions player) 2))
(used-aerial player)))
(T
(setf (attack-held player) T)
(setf (vw (color player)) 0.0)
(setf (buffer player) 'light-attack)
(setf (animation player) 'stand)
(setf (state player) :animated))))
(defmethod handle ((ev heavy-attack) (player player))
(cond ((path player))
((eql :dying (state player)))
((eql :respawning (state player)))
((or (eql :animated (state player))
(eql :dashing (state player)))
(setf (buffer player) 'heavy-attack))
((eql :crawling (state player))
(handle (make-instance 'crawl) player))
((and (null (svref (collisions player) 2))
(used-aerial player)))
(T
(setf (attack-held player) T)
(setf (vw (color player)) 0.0)
(setf (buffer player) 'heavy-attack)
(setf (animation player) 'stand)
(setf (state player) :animated))))
#-kandria-release
(let ((type (copy-seq '(wolf drone zelah mech dummy zombie rogue wraw))))
(defmethod handle ((ev mouse-scroll) (player player))
(setf type (cycle-list type))
(status :note "Switched to spawning ~a" (first type)))
(defmethod handle ((ev mouse-release) (player player))
(when (eql :middle (button ev))
(spawn (mouse-world-pos (pos ev)) (first type)))))
(flet ((handle-solid (player hit)
(when (< 0 (vy (hit-normal hit)))
(let ((vel (velocity player)))
(cond ((and (< (vy vel) -6)
(< 1.0 (air-time player))
(not (eql :animated (state player))))
(trigger 'land player :location (nv+ (v* vel (hit-time hit))
(location player)))
(cond ((or (retained 'left) (retained 'right))
(start-animation 'roll player)
(harmony:play (// 'sound 'player-roll-land) :reset T))
(T
(start-animation 'land player)
(harmony:play (// 'sound 'player-hard-land) :reset T)))
(duck-camera (vx vel) (vy vel))
(shake-camera :intensity (* 3 (/ (abs (vy vel)) (vy (p! velocity-limit))))))
((and (< (vy vel) -0.5)
(< 0.2 (air-time player)))
(harmony:play (// 'sound 'player-soft-land) :reset T))))
(unless (eql :dashing (state player))
(setf (dash-exhausted player) NIL))
(setf (used-aerial player) NIL))))
(defmethod collide :before ((player player) (block block) hit)
(unless (typep block 'spike)
(handle-solid player hit)))
(defmethod collide :before ((player player) (solid solid) hit)
(handle-solid player hit)))
(defmethod collide ((player player) (trigger trigger) hit)
(when (active-p trigger)
(interact trigger player)))
(defmethod collides-p ((player player) (block platform) hit)
(and (call-next-method)
(not (typep (interactable player) 'elevator))
(or (not (typep (interactable player) 'rope))
(not (eq :climbing (state player))))
(not (and (retained 'down)
(retained 'jump)
(< 0.01 (air-time player))))))
(defmethod idleable-p ((player player))
(and (call-next-method)
(not (or (retained 'up)
(retained 'down)))))
(defmethod (setf state) :before (state (player player))
(unless (eq state (state player))
(let ((bottom (- (vy (location player)) (vy (bsize player)))))
(case (state player)
(:crawling
(setf (vy (bsize player)) 15)
(setf (vy (location player)) (+ bottom (vy (bsize player)))))
(:dashing
(setf (vy (bsize player)) 15)
;; Need to shift to ensure that we don't glitch through platforms.
(cond ((svref (collisions player) 0)
(decf (vy (location player)) 4))
((svref (collisions player) 2)
(incf (vy (location player)) 4))))
(:fishing
(setf (slot-value player 'state) state)
(handle (make-instance 'stop-fishing) player)))
(case state
(:normal
(setf (vw (color player)) 0.0))
(:crawling
(setf (vy (bsize player)) 7)
(setf (vy (location player)) (+ bottom (vy (bsize player)))))
(:dashing
(setf (vy (bsize player)) 8))))))
(defmethod (setf buffer) (thing (player player))
(let ((buffer (slot-value player 'buffer)))
(setf (cdr buffer) (clock +world+))
(setf (car buffer) thing)))
(defmethod buffer ((player player))
(let ((buffer (slot-value player 'buffer)))
(when (< (clock +world+) (+ (cdr buffer) (p! buffer-expiration-time)))
(car buffer))))
(defun attack-chain-p (input player)
(case input
(light-attack
(case (name (animation player))
((light-ground-1 light-ground-2 light-aerial-1 light-aerial-2) T)
(T NIL)))
(heavy-attack
(case (name (animation player))
((heavy-ground-1 heavy-ground-2 heavy-aerial-1 heavy-aerial-2) T)
(T NIL)))))
(defmethod collides-p :around ((player player) thing hit)
(unless (eql :noclip (state player))
(call-next-method)))
(defmethod apply-effect :after (effect (player player) dt)
(setf (combat-time player) 0f0))
(defmethod handle :before ((ev tick) (player player))
(when (path player)
(execute-path player ev)
(nv+ (frame-velocity player) (velocity player))
(return-from handle))
;; FIXME: Very bad! We cannot track time passage by frame count!
;; Need to do proper test to check whether a second has passed.
(when (and (= (mod (fc ev) 100) 0)
(chunk player)
(visible-on-map-p (chunk player)))
(let ((trace (movement-trace player)))
(declare (type (array single-float (*))))
(vector-push-extend (vx (location player)) trace)
(vector-push-extend (vy (location player)) trace)))
(when (<= (p! climb-strength) (climb-strength player))
(decf (visibility (stamina-wheel player)) (dt ev)))
(let* ((collisions (collisions player))
(dt (dt ev))
(loc (location player))
(vel (velocity player))
(size (bsize player))
(chunk (chunk player))
(ground (svref collisions 2))
(input-source +input-source+)
(ground-limit (cond
((and (typep input-source 'gamepad:device)
;; FIXME: This is really bad and terrible lmao.
(< (abs (gamepad:axis :l-h input-source)) 0.75)
(< (abs (gamepad:axis :dpad-h input-source)) 0.75)
(not (gamepad:button :dpad-l input-source))
(not (gamepad:button :dpad-r input-source)))
(p! slowwalk-limit))
((< (run-time player) (p! run-time))
(p! walk-limit))
(T
(p! run-limit))))
(ground-acc (if (< (p! run-time) (run-time player))
(p! run-acc)
(p! walk-acc))))
;; Advance clocks
(when (< (abs (vx vel)) (/ (p! walk-limit) 2))
(setf (run-time player) 0.0))
(incf (run-time player) dt)
(incf (combat-time player) dt)
;; HUD
(cond ((< (combat-time player) 5)
(when (= 1.0 (intended-zoom (camera +world+)))
(setf (intended-zoom (camera +world+))
(if (eql 'evade-left (name (animation player)))
1.6 1.2)))
(setf (timeout (health (find-panel 'hud))) 5.0))
((and (< 5 (combat-time player) 6)
(< 1 (intended-zoom (camera +world+))))
(setf (intended-zoom (camera +world+)) 1.0)))
;; Interaction checks
(setf (interactable player) NIL)
(when (and ground (interactable-p ground))
(setf (interactable player) ground))
(let ((interactable (tvec (vx2 loc) (vy2 loc) 16 16))
(trigger (tvec (vx2 loc) (vy2 loc) 16 8))
(closest NIL))
(do-fitting (entity (bvh (region +world+)) (or (chunk player) player))
(typecase entity
(rope
(when (and (contained-p interactable entity)
(< (+ 2 (- (vy loc) (vy size)))
(+ (vy (location entity)) (vy (bsize entity)))))
(setf (interactable player) entity)))
(interactable
(when (and (contained-p interactable entity)
(not (eql :climbing (state player)))
(interactable-p entity)
(or (null closest)
(< (interactable-priority closest)
(interactable-priority entity))
(<= (vsqrdistance (location entity) loc)
(vsqrdistance (location closest) loc))))
(setf closest entity)))
(trigger
(when (contained-p trigger entity)
(interact entity player)))))
(when closest
(setf (interactable player) closest)))
;; Handle fishing
(when (and (v= 0 vel)
ground
chunk
(null (interactable player))
(eql :normal (state player))
(typep (medium player) 'air))
(do-fitting (entity (bvh (region +world+)) (tvec (max (- (vx loc) (* +tile-size+ 13))
(- (vx (location chunk))
(vx (bsize chunk))))
(- (vy loc) (* +tile-size+ 10))
(min (+ (vx loc) (* +tile-size+ 13))
(+ (vx (location chunk))
(vx (bsize chunk))))
(vy loc)))
(when (and (typep entity 'water) (< (vy (location entity)) (vy (location player))))
(let ((dx (/ (- (vx (location entity)) (vx loc)) 2.0)))
(when (and (< (abs dx) 250)
(null (scan-collision-for player +world+ (tvec (+ (vx loc) dx) (+ (vy loc) 16) (abs dx) (vy size))))
(interactable-p entity))
(setf (interactable player) entity)
(return))))))
(if (and (interactable player)
(interactable-p (interactable player))
(eql :normal (state player))
(setting :gameplay :display-hud))
(let ((loc (vec (typecase (interactable player)
(water (vx loc))
(T (vx (location (interactable player)))))
(+ (vy loc) (vy (bsize player))))))
(when (and (typep (interactable player) 'npc)
(nametag-element (interactable player)))
(incf (vy loc) (+ 5 (offset (nametag-element (interactable player))))))
(show (prompt player)
:button (action (interactable player))
:location loc
:description (description (interactable player))))
(when (slot-boundp (prompt player) 'alloy:layout-parent)
(hide (prompt player))))
;; Handle states.
(ecase (state player)
(:noclip
(setf (animation player) 't-pose)
(vsetf vel 0 0)
(when (retained 'left) (setf (vx vel) (- (vx (p! velocity-limit)))))
(when (retained 'right) (setf (vx vel) (+ (vx (p! velocity-limit)))))
(when (retained 'up) (setf (vy vel) (+ (vx (p! velocity-limit)))))
(when (retained 'down) (setf (vy vel) (- (vx (p! velocity-limit))))))
(:oob
(vsetf vel 0 0))
(:stunned
(handle-animation-states player ev)
(when (and (cancelable-p (frame player))
(or (retained 'left)
(retained 'right)))
(setf (state player) :normal)))
((:dying :respawning)
(handle-animation-states player ev))
(:fishing
(let* ((line (fishing-line player))
(visible (container line)))
(when visible
(!vload (location line) (hurtbox player) 'xy)
;; FIXME: If camera is near borders, duck small amount. Otherwise, duck full.
(duck-camera (* 0.2 (- (vx (location (buoy line))) (vx loc)))
(+ 32 (- (vy (location (buoy line))) (vy loc)))))
(case (name (animation player))
(fishing-start
(if (= (frame-idx player) 543)
(unless visible
(!vload (location line) (hurtbox player) 'xy)
(enter line (region +world+))))))
(case (name (animation player))
(fishing-loop
(let* ((loc (vcopy (location (buoy line))))
(bloc (tvec (+ (vx loc) (* -10 (setting :display :ui-scale)))
(+ (vy loc) (* 16 (setting :display :ui-scale)))))
(tt (* 1.3 (tt ev)))
(t2 (+ tt (/ PI 3))))
(nv+ loc (vec (* 5 (cos tt)) (* 3 (sin tt) (cos tt))))
(nv+ bloc (vec (* 5 (cos t2)) (* 3 (sin t2) (cos t2))))
(when (setting :gameplay :display-hud)
(show (prompt player) :button 'reel-in
:description (language-string 'reel-in)
:location loc)
(show (prompt-b player) :button 'stop-fishing
:description (language-string 'stop-fishing)
:location bloc))))
((show stand)
(let* ((loc (tv+ #.(vec 0 16) (location player)))
(bloc (tvec (+ (vx loc) -10)
(+ (vy loc) 16)))
(tt (* 1.3 (tt ev)))
(t2 (+ tt (/ PI 3))))
(nv+ loc (vec (* 5 (cos tt)) (* 3 (sin tt) (cos tt))))
(nv+ bloc (vec (* 5 (cos t2)) (* 3 (sin t2) (cos t2))))
(when (setting :gameplay :display-hud)
(show (prompt player) :button 'cast-line
:description (language-string 'cast-line)
:location loc)
(show (prompt-b player) :button 'stop-fishing
:description (language-string 'stop-fishing)
:location bloc))))
(T
(hide (prompt player))
(hide (prompt-b player))))))
(:sitting
(handle-animation-states player ev)
(if (eql :keyboard +input-source+)
(duck-camera
(cond ((retained 'left) -200)
((retained 'right) +200)
(T 0))
(cond ((retained 'down) -200)
((retained 'up) +200)
(T 0)))
(duck-camera
(* 200 (gamepad:axis :l-h +input-source+))
(* 200 (gamepad:axis :l-v +input-source+))))
(when (retained 'jump)
(let ((segment (harmony:segment :lowpass T)))
(setf (mixed:frequency segment) (1- (mixed:samplerate segment))))
(setf (intended-zoom (camera +world+)) 1.0)
(start-animation 'stand-up player)))
(:animated
(when (and ground (eql 'heavy-aerial-3 (name (animation player))))
(start-animation 'heavy-aerial-3-release player))
(let ((buffer (buffer player))
(animation (animation player)))
(cond ((and buffer
(cancelable-p (frame player))
(or (<= (cooldown-time player) 0.0)
(eql 'dash buffer)
(and (< 3 (- (frame-idx player) (start animation)))
(attack-chain-p buffer player))))
(setf (buffer player) NIL)
(cond ((retained 'left) (setf (direction player) -1))
((retained 'right) (setf (direction player) +1)))
(when (or (eql buffer 'light-attack)
(eql buffer 'heavy-attack))
(setf (combat-time player) 0f0))
(case buffer
(light-attack
(case (name animation)
(light-ground-1 (start-animation 'light-ground-2 player))
(light-ground-2 (start-animation 'light-ground-3 player))
(light-aerial-1 (start-animation 'light-aerial-2 player))
(light-aerial-2 (start-animation 'light-aerial-3 player))
((evade-left evade-right)
(harmony:play (// 'sound 'player-counter) :reset T)
(start-animation 'light-counter player))
(T
(cond ((not (svref (collisions player) 2))
(unless (used-aerial player)
(setf (used-aerial player) T)
(if (retained 'down)
(start-animation 'light-aerial-down player)
(start-animation 'light-aerial-1 player))))
((retained 'up)
(start-animation 'light-up player))
((retained 'down)
(start-animation 'light-down player))
(T
(start-animation 'light-ground-1 player))))))
(heavy-attack
(case (name animation)
(heavy-ground-1 (start-animation 'heavy-ground-2 player))
(heavy-ground-2 (start-animation 'heavy-ground-3 player))
(heavy-aerial-1 (start-animation 'heavy-aerial-2 player))
(heavy-aerial-2 (start-animation 'heavy-aerial-3 player))
((evade-left evade-right)
(harmony:play (// 'sound 'player-counter) :reset T)
(start-animation 'heavy-counter player))
(T
(cond ((not (svref (collisions player) 2))
(unless (used-aerial player)
(setf (used-aerial player) T)
(if (retained 'down)
(start-animation 'heavy-aerial-3 player)
(start-animation 'heavy-aerial-1 player))))
((retained 'up)
(start-animation 'heavy-up player))
((retained 'down)
(start-animation 'heavy-down player))
(T
(start-animation 'heavy-ground-1 player))))))
(dash
(setf (state player) :normal)
(handle (make-instance 'dash) player))
(jump
(setf (state player) :normal)
(handle (make-instance 'jump) player))))
((and (retained 'jump)
(eql 'climb-ledge (name animation)))
(setf (state player) :normal))
((eql 'stand (name animation))
(setf (state player) :normal))
((not (retained (case (name animation)
((light-ground-1 light-charge-2) 'light-attack)
((heavy-ground-1 heavy-charge-2) 'heavy-attack))))
(setf (attack-held player) NIL))
((and (attack-held player)
(<= 2 (- (frame-idx player) (start animation)))
(<= (duration (frame player)) (+ dt (clock player))))
(case (name animation)
(light-ground-1
(start-animation 'light-charge-2 player))
(heavy-ground-1
(start-animation 'heavy-charge-2 player))
(light-charge-2
(unless (interruptable-p (frame player))
(setf (clock player) 0.0)))
(heavy-charge-2
(unless (interruptable-p (frame player))
(setf (clock player) 0.0)))))))
(handle-animation-states player ev))
(:dashing
(incf (dash-time player) dt)
(setf (jump-time player) 100.0)
(when (< (decf (vw (color player)) (* 4 dt)) 0)
(setf (vw (color player)) 0.0))
(or (and (< (dash-time player) (p! dash-evade-grace-time))
(handle-evasion player)
(setf (vw (color player)) 0.0))
(cond ((< (p! dash-min-time) (dash-time player))
(setf (state player) :normal)
(when (eql 'dash (buffer player))
(handle (load-time-value (make-instance 'dash)) player)))
((< (p! dash-dcc-end) (dash-time player)))
((< (p! dash-dcc-start) (dash-time player))
(nv* vel (damp* (p! dash-dcc) (* 100 dt))))
((< (p! dash-acc-start) (dash-time player))
(nv* vel (damp* (p! dash-acc) (* 100 dt))))
((< (p! dash-acc-start) (+ (dash-time player) (dt ev)))
(vsetf (color player) 1 1 1 1)
(vsetf vel
(cond ((retained 'left) -0.5)
((retained 'right) +0.5)
(T 0))
(cond ((retained 'up) +0.5)
((retained 'down) -0.5)
(T 0)))
(if (= (vx vel) 0.0)
(if (= (vy vel) 0.0)
(setf (vx vel) (direction player)))
(setf (direction player) (float-sign (vx vel))))
(nvunit vel)
(when (and (retained 'jump)
(<= (air-time player) 0.1))
(incf (vx vel) (* (direction player) (vx (p! hyperdash-bonus))))
(incf (vy vel) (vy (p! hyperdash-bonus)))
(vsetf (color player) 0.5 0.5 1 1))
(if (svref (collisions player) 2)
(trigger 'dash player)
(trigger 'air-dash player)))
(T
(vsetf (color player) 1 1 1 1)
(vsetf vel 0 0))))
(when (typep (interactable player) 'rope)
(nudge (interactable player) loc (* (direction player) 20)))
;; Adapt velocity if we are on sloped terrain
;; I'm not sure why this is necessary, but it is.
(typecase ground
(slope
(when (v/= 0 vel)
(let* ((normal (nvunit (vec2 (- (vy2 (slope-l ground)) (vy2 (slope-r ground)))
(- (vx2 (slope-r ground)) (vx2 (slope-l ground))))))
(slope (vec (- (vy normal)) (vx normal)))
(proj (v* slope (v. slope vel)))
(angle (vangle slope (vunit vel))))
(when (or (< angle (* PI 1/4)) (< (* PI 3/4) angle))
(vsetf vel (vx proj) (vy proj))))))
(null
(nv* vel (damp* (p! dash-air-dcc) (* 100 dt)))))
;; Handle crawlspaces
(when (/= 0 (vx vel))
(let* ((off (tvec (+ (vx loc) (* (float-sign (vx vel)) (+ (vx size) 8)))
(+ (vy loc) (vy size) 8)))
(ymin (- (vy loc) (vy size) 8))
(pprev NIL)
(prev NIL))
(loop (let ((cur (scan +world+ off (lambda (hit)
(not (and (typep (hit-object hit) '(and (or solid ground) (not death)))
(is-collider-for player (hit-object hit))))))))
(when (and pprev (null prev)
(if (< (vy off) ymin)
(or cur ground)
cur))
(setf (vw (color player)) 0.0)
(setf (vx vel) (* (float-sign (vx vel)) (vx (p! velocity-limit))))
(if cur
(setf (vy loc) (+ (vy (hit-location cur)) (vy size) 8))
(setf (vy loc) (- (vy pprev) (vy size) 8)))
(setf (air-time player) 0.0)
(setf (state player) :crawling)
(setf (dash-exhausted player) NIL)
(return))
(shiftf pprev prev (if cur (vcopy (hit-location cur)))))
(when (< (vy off) ymin)
(return))
(decf (vy off) 16)))))
(:sliding
(let ((dir (cond ((retained 'left) -1)
((retained 'right) +1)
(T 0))))
;; Allow the player to influence movement.
(if (= dir (float-sign (vx vel)))
(when (<= (abs (vx vel)) (p! slide-acclimit))
(incf (vx vel) (* dir (p! slide-acc))))
(when (<= (p! slide-dcc) (abs (vx vel)))
(incf (vx vel) (* dir (p! slide-dcc)))))
;; Slope sticky
(when (typep ground 'slope)
(let ((slope-steepness (* (- (vy (slope-l ground)) (vy (slope-r ground))))))
(cond ((= (direction player) (float-sign slope-steepness))
(incf (vy vel) -1)
(incf (vx vel) (* slope-steepness (p! slide-slope-acc))))
(T
(incf (vx vel) (* slope-steepness (p! slide-slope-dcc)))
(let* ((normal (nvunit (vec2 (- (vy2 (slope-l ground)) (vy2 (slope-r ground)))
(- (vx2 (slope-r ground)) (vx2 (slope-l ground))))))
(slope (vec (- (vy normal)) (vx normal)))
(proj (v* slope (v. slope vel))))
(vsetf vel (vx proj) (vy proj))))))))
;; Handle jumps
(when (and (< (jump-time player) 0.0)
(< (air-time player) (p! coyote-time)))
(trigger 'jump player)
(setf (vy vel) (+ (p! jump-acc)
(if ground
(max 0 (vy (velocity ground)))
0)))
(setf ground NIL)
(setf (jump-time player) 0.0))
;; Friction
(if ground
(setf (vx vel) (* (vx vel) (damp* (p! slide-friction) (* 100 dt))))
(setf (vx vel) (* (vx vel) (damp* (p! air-dcc) (* 100 dt)))))
(cond (ground
(setf (air-time player) 0.0)
(when (or (<= (abs (vx vel)) 1.0))
(start-animation 'stumble player)))
((and (< 0.0 (vy vel))
(< 0.1 (jump-time player))
(< (air-time player) 0.02))
(nv* vel (damp* 1.17 (* 100 dt)))))
(when (and (retained 'jump)
(<= 0.05 (jump-time player) 0.15)
(< 0 (vy vel)))
(incf (vy vel) (* dt (p! jump-hold-acc))))
(nv+ vel (v* (gravity (medium player)) dt)))
(:climbing
(setf (visibility (stamina-wheel player)) 1.0)
(setf (air-time player) 0.0)
(when (< 0 (inertia-time player))
(decf (inertia-time player) dt))
;; Movement
(let* ((offset (tvec (+ (vx loc) (* (direction player) (+ 8 (vx size)))) (- (vy loc) (vy size) -2)))
(top (scan-collision-for player +world+ offset))
(ledge (scan-collision-for player +world+ (tv+ offset #.(vec 0 16))))
(attached (or (if (typep (svref collisions (if (< 0 (direction player)) 1 3)) '(or ground solid))
(svref collisions (if (< 0 (direction player)) 1 3)))
(interactable player)
top)))
(setf (vx vel) 0f0)
(when (or (not (retained 'climb))
(not (typep attached '(or (and ground (not slipblock)) (and solid (not half-solid)) rope)))
(<= (climb-strength player) 0))
(setf (state player) :normal))
(typecase attached
(rope
(setf (climb-strength player) (p! climb-strength))
(vsetf (color player) 0 0 0 0)
(nudge attached loc (* (direction player) -8))
(cond ((retained 'left)
(let ((target-x (- (vx (location (interactable player))) (vx size))))
(unless (scan-collision-for player +world+ (vec target-x (vy loc) (vx size) (vy size)))
(setf (direction player) +1)
(setf (vx loc) target-x))))
((retained 'right)
(let ((target-x (+ (vx (location (interactable player))) (vx size))))
(unless (scan-collision-for player +world+ (vec target-x (vy loc) (vx size) (vy size)))
(setf (direction player) -1)
(setf (vx loc) target-x))))))
(moving-platform
(trigger attached player)
#++(handle ev attached)
#++(incf (vy (frame-velocity player)) (vy (velocity attached)))
(setf (vx loc) (+ (vx (location attached))
(* (float-sign (- (vx loc) (vx (location attached))))
(+ (vx (bsize attached)) (vx size)))))
(incf (vy loc) (vy (velocity attached)))
(when (and (v/= 0 (velocity attached))
(not (retained 'jump)))
(setf (inertia-time player) (p! inertia-coyote))
(v<- (inertia player) (velocity attached)))))
(if (retained 'down)
(if (typep attached 'rope)
(harmony:play (// 'sound 'rope-slide-down)))
(harmony:stop (// 'sound 'rope-slide-down)))
(cond ((retained 'jump)
(when (typep attached 'rope)
(nudge attached (location player) (* (direction player) 16)))
(harmony:stop (// 'sound 'rope-slide-down))
(setf (state player) :normal))
((retained 'up)
(cond ((and (or top
(and (typep attached 'rope)
(< (+ (vy (location attached)) (vy (bsize attached))) (vy loc))))
(null (scan-collision-for player +world+ (tv+ offset #.(vec 0 38))))
(null ledge)
(null (retained 'jump)))
(start-animation 'climb-ledge player))
((typep ledge 'spike)
(setf (vy vel) 0.0))
(T
(unless (typep attached 'rope)
(decf (climb-strength player) (* (p! climb-up-cost) dt)))
(if (< (vy vel) (p! climb-up))
(setf (vy vel) (p! climb-up))
(decf (vy vel) 0.1)))))
((retained 'down)
(setf (vy vel) (* (p! climb-down) -1)))
(T
(unless (typep attached 'rope)
(decf (climb-strength player) (* (p! climb-hold-cost) dt)))
(setf (vy vel) 0.0)))))
(:crawling