-
Notifications
You must be signed in to change notification settings - Fork 127
/
Player.cs
2315 lines (1930 loc) · 60.6 KB
/
Player.cs
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
namespace Celeste64;
/// <summary>
/// Welcome to the monolithic player class! This time only 2300 lines ;)
/// </summary>
public class Player : Actor, IHaveModels, IHaveSprites, IRidePlatforms, ICastPointShadow
{
#region Constants
private const float Acceleration = 500;
private const float PastMaxDeccel = 60;
private const float AirAccelMultMin = .5f;
private const float AirAccelMultMax = 1f;
private const float MaxSpeed = 64;
private const float RotateThreshold = MaxSpeed * .2f;
private const float RotateSpeed = MathF.Tau * 1.5f;
private const float RotateSpeedAboveMax = MathF.Tau * .6f;
private const float Friction = 800;
private const float AirFrictionMult = .1f;
private const float Gravity = 600;
private const float MaxFall = -120;
private const float HalfGravThreshold = 100;
private const float JumpHoldTime = .1f;
private const float JumpSpeed = 90;
private const float JumpXYBoost = 10;
private const float CoyoteTime = .12f;
private const float WallJumpXYSpeed = MaxSpeed * 1.3f;
private const float DashSpeed = 140;
private const float DashEndSpeedMult = .75f;
private const float DashTime = .2f;
private const float DashResetCooldown = .2f;
private const float DashCooldown = .1f;
private const float DashRotateSpeed = MathF.Tau * .3f;
private const float DashJumpSpeed = 40;
private const float DashJumpHoldSpeed = 20;
private const float DashJumpHoldTime = .3f;
private const float DashJumpXYBoost = 16;
private const float SkidDotThreshold = -.7f;
private const float SkiddingStartAccel = 300;
private const float SkiddingAccel = 500;
private const float EndSkidSpeed = MaxSpeed * .8f;
private const float SkidJumpSpeed = 120;
private const float SkidJumpHoldTime = .16f;
private const float SkidJumpXYSpeed = MaxSpeed * 1.4f;
private const float WallPushoutDist = 3;
private const float ClimbCheckDist = 4;
private const float ClimbSpeed = 40;
private const float ClimbHopUpSpeed = 80;
private const float ClimbHopForwardSpeed = 40;
private const float ClimbHopNoMoveTime = .25f;
private const float SpringJumpSpeed = 160;
private const float SpringJumpHoldTime = .3f;
private const float FeatherStartTime = .4f;
private const float FeatherFlySpeed = 100;
private const float FeatherStartSpeed = 140;
private const float FeatherTurnSpeed = MathF.Tau * .75f;
private const float FeatherAccel = 60;
private const float FeatherDuration = 2.2f;
private const float FeatherExitXYMult = .5f;
private const float FeatherExitZSpeed = 60;
static private readonly Color CNormal = 0xdb2c00;
static private readonly Color CNoDash = 0x6ec0ff;
static private readonly Color CTwoDashes = 0xfa91ff;
static private readonly Color CRefillFlash = Color.White;
static private readonly Color CFeather = 0xf2d450;
#endregion
#region SubClasses
private class Trail
{
public readonly Hair Hair;
public readonly SkinnedModel Model;
public Matrix Transform;
public float Percent;
public Color Color;
public Trail()
{
Model = new(Assets.Models["player"]);
Model.Flags = ModelFlags.Transparent;
Model.MakeMaterialsUnique();
foreach (var mat in Model.Materials)
{
mat.Texture = Assets.Textures["white"];
mat.Effects = 0;
}
Hair = new();
foreach (var mat in Hair.Materials)
{
mat.Texture = Assets.Textures["white"];
mat.Effects = 0;
}
Hair.Flags = ModelFlags.Transparent;
}
}
#endregion
// used between respawns
private static Vec3 storedCameraForward;
private static float storedCameraDistance;
private enum States { Normal, Dashing, Skidding, Climbing, StrawbGet, FeatherStart, Feather, Respawn, Dead, StrawbReveal, Cutscene, Bubble, Cassette };
private enum Events { Land };
public bool Dead = false;
public Vec3 ModelScale = Vec3.One;
public SkinnedModel Model;
public readonly Hair Hair = new();
public float PointShadowAlpha { get; set; } = 1.0f;
public Vec3 Velocity => velocity;
public Vec3 PreviousVelocity => previousVelocity;
private Vec3 velocity;
private Vec3 previousVelocity;
private Vec3 groundNormal;
private Vec3 platformVelocity;
private float tPlatformVelocityStorage;
private float tGroundSnapCooldown;
private Actor? climbingWallActor;
private Vec3 climbingWallNormal;
private bool onGround;
private Vec2 targetFacing = Vec2.UnitY;
private Vec3 cameraTargetForward = new(0, 1, 0);
private float cameraTargetDistance = 0.50f;
private readonly StateMachine<States, Events> stateMachine;
private record struct CameraOverride(Vec3 Position, Vec3 LookAt);
private CameraOverride? cameraOverride = null;
private Vec3 cameraOriginPos;
private float tCoyote;
private float coyoteZ;
private bool drawModel = true;
private bool drawHair = true;
private bool drawOrbs = false;
private float drawOrbsEase = 0;
private readonly List<Trail> trails = [];
private readonly Func<SpikeBlock, bool> spikeBlockCheck;
private Color lastDashHairColor;
private Sound? sfxWallSlide;
private Sound? sfxFeather;
private Sound? sfxBubble;
private Vec3 SolidWaistTestPos
=> Position + Vec3.UnitZ * 3;
private Vec3 SolidHeadTestPos
=> Position + Vec3.UnitZ * 10;
private bool InFeatherState
=> stateMachine.State == States.FeatherStart
|| stateMachine.State == States.Feather;
private bool InBubble
=> stateMachine.State == States.Bubble;
public bool IsStrawberryCounterVisible
=> stateMachine.State == States.StrawbGet;
public bool IsAbleToPickup
=> stateMachine.State != States.StrawbGet
&& stateMachine.State != States.Bubble
&& stateMachine.State != States.Cassette
&& stateMachine.State != States.StrawbReveal
&& stateMachine.State != States.Respawn
&& stateMachine.State != States.Dead;
public bool IsAbleToPause
=> stateMachine.State != States.StrawbReveal
&& stateMachine.State != States.StrawbGet
&& stateMachine.State != States.Cassette
&& stateMachine.State != States.Dead;
public Player()
{
PointShadowAlpha = 1.0f;
LocalBounds = new BoundingBox(new Vec3(0, 0, 10), 10);
UpdateOffScreen = true;
// setup model
{
Model = new(Assets.Models["player"]);
Model.SetBlendDuration("Idle", "Dash", 0.05f);
Model.SetBlendDuration("Idle", "Run", 0.2f);
Model.SetBlendDuration("Run", "Skid", .125f);
Model.SetLooping("Dash", false);
Model.Flags |= ModelFlags.Silhouette;
Model.Play("Idle");
foreach (var mat in Model.Materials)
mat.Effects = 0.60f;
}
stateMachine = new();
stateMachine.InitState(States.Normal, StNormalUpdate, StNormalEnter, StNormalExit);
stateMachine.InitState(States.Dashing, StDashingUpdate, StDashingEnter, StDashingExit);
stateMachine.InitState(States.Skidding, StSkiddingUpdate, StSkiddingEnter, StSkiddingExit);
stateMachine.InitState(States.Climbing, StClimbingUpdate, StClimbingEnter, StClimbingExit);
stateMachine.InitState(States.StrawbGet, StStrawbGetUpdate, StStrawbGetEnter, StStrawbGetExit, StStrawbGetRoutine);
stateMachine.InitState(States.FeatherStart, StFeatherStartUpdate, StFeatherStartEnter, StFeatherStartExit);
stateMachine.InitState(States.Feather, StFeatherUpdate, StFeatherEnter, StFeatherExit);
stateMachine.InitState(States.Respawn, StRespawnUpdate, StRespawnEnter, StRespawnExit);
stateMachine.InitState(States.StrawbReveal, null, StStrawbRevealEnter, StStrawbRevealExit, StStrawbRevealRoutine);
stateMachine.InitState(States.Cutscene, StCutsceneUpdate, StCutsceneEnter);
stateMachine.InitState(States.Dead, StDeadUpdate, StDeadEnter);
stateMachine.InitState(States.Bubble, null, null, StBubbleExit, StBubbleRoutine);
stateMachine.InitState(States.Cassette, null, null, StCassetteExit, StCassetteRoutine);
spikeBlockCheck = (spike) =>
{
return Vec3.Dot(velocity.Normalized(), spike.Direction) < 0.5f;
};
SetHairColor(0xdb2c00);
}
#region Added / Update
public override void Added()
{
if (World.Entry.Reason == World.EntryReasons.Respawned)
{
cameraTargetForward = storedCameraForward;
cameraTargetDistance = storedCameraDistance;
stateMachine.State = States.Respawn;
}
else if (World.Entry.Submap && World.Entry.Reason == World.EntryReasons.Entered)
{
stateMachine.State = States.StrawbReveal;
}
else
{
stateMachine.State = States.Normal;
}
sfxWallSlide = World.Add(new Sound(this, Sfx.sfx_wall_slide));
sfxFeather = World.Add(new Sound(this, Sfx.sfx_feather_state_active_loop));
sfxBubble = World.Add(new Sound(this, Sfx.sfx_bubble_loop));
cameraOriginPos = Position;
GetCameraTarget(out var orig, out var target, out _);
World.Camera.LookAt = target;
World.Camera.Position = orig;
}
public override void Update()
{
// only update camera if not dead
if (stateMachine.State != States.Respawn && stateMachine.State != States.Dead &&
stateMachine.State != States.StrawbReveal && stateMachine.State != States.Cassette)
{
// Rotate Camera
{
var invertX = Save.Instance.InvertCamera == Save.InvertCameraOptions.X || Save.Instance.InvertCamera == Save.InvertCameraOptions.Both;
var rot = new Vec2(cameraTargetForward.X, cameraTargetForward.Y).Angle();
rot -= Controls.Camera.Value.X * Time.Delta * 4 * (invertX ? -1 : 1);
var angle = Calc.AngleToVector(rot);
cameraTargetForward = new(angle, 0);
}
// Move Camera in / out
if (Controls.Camera.Value.Y != 0)
{
var invertY = Save.Instance.InvertCamera == Save.InvertCameraOptions.Y || Save.Instance.InvertCamera == Save.InvertCameraOptions.Both;
cameraTargetDistance += Controls.Camera.Value.Y * Time.Delta * (invertY ? -1 : 1);
cameraTargetDistance = Calc.Clamp(cameraTargetDistance, 0, 1);
}
else
{
const float interval = 1f / 3;
const float threshold = .1f;
if (cameraTargetDistance % interval < threshold || cameraTargetDistance % interval > interval - threshold)
Calc.Approach(ref cameraTargetDistance, Calc.Snap(cameraTargetDistance, interval), Time.Delta / 2);
}
}
// don't do anything if dead
if (stateMachine.State == States.Respawn || stateMachine.State == States.Dead || stateMachine.State == States.Cutscene)
{
stateMachine.Update();
return;
}
// death plane
if (!InBubble)
{
if (Position.Z < World.DeathPlane ||
World.Overlaps<DeathBlock>(SolidWaistTestPos) ||
World.Overlaps<SpikeBlock>(SolidWaistTestPos, spikeBlockCheck))
{
Kill();
return;
}
}
// enter cutscene
if (World.All<Cutscene>().Count > 0)
stateMachine.State = States.Cutscene;
// run timers
{
if (tCoyote > 0)
tCoyote -= Time.Delta;
if (tHoldJump > 0)
tHoldJump -= Time.Delta;
if (tDashCooldown > 0)
tDashCooldown -= Time.Delta;
if (tDashResetCooldown > 0)
tDashResetCooldown -= Time.Delta;
if (tDashResetFlash > 0)
tDashResetFlash -= Time.Delta;
if (tNoMove > 0)
tNoMove -= Time.Delta;
if (tPlatformVelocityStorage > 0)
tPlatformVelocityStorage -= Time.Delta;
if (tGroundSnapCooldown > 0)
tGroundSnapCooldown -= Time.Delta;
if (tClimbCooldown > 0)
tClimbCooldown -= Time.Delta;
}
previousVelocity = velocity;
stateMachine.Update();
// move and pop out
if (!InBubble)
{
// push out of NPCs
foreach (var actor in World.All<IHavePushout>())
{
var it = (actor as IHavePushout)!;
if (it.PushoutRadius <= 0 || it.PushoutHeight <= 0)
continue;
if (Position.Z < actor.Position.Z - 12 || Position.Z > actor.Position.Z + it.PushoutHeight)
continue;
var diff = (Position.XY() - actor.Position.XY());
if (diff.LengthSquared() > it.PushoutRadius * it.PushoutRadius)
continue;
var normal = diff.Normalized();
var distance = diff.Length();
var pushout = (it.PushoutRadius - distance);
SweepTestMove(new Vec3(normal * pushout, 0), false);
}
// handle actual movement
{
var amount = velocity * Time.Delta;
SweepTestMove(amount, tNoMove <= 0);
}
// do an idle popout for good measure
Popout(false);
}
// pickups
if (IsAbleToPickup)
{
foreach (var actor in World.All<IPickup>())
{
if (actor is IPickup pickup)
{
if ((SolidWaistTestPos - actor.Position).LengthSquared() < pickup.PickupRadius * pickup.PickupRadius)
pickup.Pickup(this);
}
}
}
}
public override void LateUpdate()
{
// ground checks
{
bool prevOnGround = onGround;
onGround = GroundCheck(out var pushout, out var normal, out _);
if (onGround)
Position += pushout;
if (tGroundSnapCooldown <= 0 && prevOnGround && !onGround)
{
// try to ground snap?
if (World.SolidRayCast(Position, -Vec3.UnitZ, 5, out var hit) && FloorNormalCheck(hit.Normal))
{
Position = hit.Point;
onGround = GroundCheck(out _, out normal, out _);
}
}
if (onGround)
{
autoJump = false;
groundNormal = normal;
tCoyote = CoyoteTime;
coyoteZ = Position.Z;
if (tDashResetCooldown <= 0)
RefillDash();
}
else
groundNormal = Vec3.UnitZ;
if (!prevOnGround && onGround)
{
float t = Calc.ClampedMap(previousVelocity.Z, 0, MaxFall);
ModelScale = Vec3.Lerp(Vec3.One, new(1.4f, 1.4f, .6f), t);
stateMachine.CallEvent(Events.Land);
if (!Game.Instance.IsMidTransition && !InBubble)
{
Audio.Play(Sfx.sfx_land, Position);
for (int i = 0; i < 16; i++)
{
var angle = Calc.AngleToVector((i / 16.0f) * MathF.Tau);
var at = Position + new Vec3(angle, 0) * 4;
var vel = (tPlatformVelocityStorage > 0 ? platformVelocity : Vec3.Zero) + new Vec3(angle, 0) * 50;
World.Request<Dust>().Init(at, vel);
}
}
}
}
// update camera origin position
{
float ZPad = stateMachine.State == States.Climbing ? 0 : 8;
cameraOriginPos.X = Position.X;
cameraOriginPos.Y = Position.Y;
float targetZ;
if (onGround)
targetZ = Position.Z;
else if (Position.Z < cameraOriginPos.Z)
targetZ = Position.Z;
else if (Position.Z > cameraOriginPos.Z + ZPad)
targetZ = Position.Z - ZPad;
else
targetZ = cameraOriginPos.Z;
if (cameraOriginPos.Z != targetZ)
cameraOriginPos.Z += (targetZ - cameraOriginPos.Z) * (1 - MathF.Pow(.001f, Time.Delta));
}
// update camera position
{
Vec3 lookAt, cameraPos;
if (cameraOverride.HasValue)
{
lookAt = cameraOverride.Value.LookAt;
cameraPos = cameraOverride.Value.Position;
}
else
{
GetCameraTarget(out lookAt, out cameraPos, out _);
}
World.Camera.Position += (cameraPos - World.Camera.Position) * (1 - MathF.Pow(0.01f, Time.Delta));
World.Camera.LookAt = lookAt;
float targetFOV = Calc.ClampedMap(velocity.XY().Length(), MaxSpeed * 1.2f, 120, 1, 1.2f);
World.Camera.FOVMultiplier = Calc.Approach(World.Camera.FOVMultiplier, targetFOV, Time.Delta / 4);
}
// update model
{
Calc.Approach(ref ModelScale.X, 1, Time.Delta / .8f);
Calc.Approach(ref ModelScale.Y, 1, Time.Delta / .8f);
Calc.Approach(ref ModelScale.Z, 1, Time.Delta / .8f);
Facing = Calc.AngleToVector(Calc.AngleApproach(Facing.Angle(), targetFacing.Angle(), MathF.Tau * 2 * Time.Delta));
Model.Update();
Model.Transform = Matrix.CreateScale(ModelScale * 3);
if (stateMachine.State != States.Feather && stateMachine.State != States.FeatherStart)
{
Color color;
if (tDashResetFlash > 0)
color = CRefillFlash;
else if (dashes == 1)
color = CNormal;
else if (dashes == 0)
color = CNoDash;
else
color = CTwoDashes;
SetHairColor(color);
}
}
// hair
{
var hairMatrix = Matrix.Identity;
foreach (var it in Model.Instance.Armature.LogicalNodes)
{
if (it.Name == "Head")
{
hairMatrix = it.ModelMatrix * SkinnedModel.BaseTranslation * Model.Transform * Matrix;
}
}
Hair.Flags = Model.Flags;
Hair.Forward = -new Vec3(Facing, 0);
Hair.Squish = ModelScale;
Hair.Materials[0].Effects = 0;
Hair.Grounded = onGround;
Hair.Update(hairMatrix);
}
// trails
for (int i = trails.Count - 1; i >= 0; i--)
{
if (trails[i].Percent < 1)
trails[i].Percent += Time.Delta / 0.5f;
}
}
#endregion
#region Camera Calculation
public void GetCameraTarget(out Vec3 cameraLookAt, out Vec3 cameraPosition, out bool snapRequested)
{
snapRequested = false;
// get default values
cameraLookAt = cameraOriginPos;
cameraPosition = cameraLookAt
- cameraTargetForward * Utils.Lerp3(30, 60, 110, 110, cameraTargetDistance)
+ Vec3.UnitZ * Utils.Lerp3(1, 30, 80, 180, cameraTargetDistance);
cameraLookAt += Vec3.UnitZ * 12;
// inside a fixed camera zone
if (World.OverlapsFirst<FixedCamera>(SolidWaistTestPos) is {} fixedCamera
&& (cameraLookAt - fixedCamera.Position).Length() > 5)
{
cameraPosition = fixedCamera.Point;
cameraTargetForward = new Vec3((cameraLookAt.XY() - cameraPosition.XY()).Normalized(), 0);
snapRequested = true;
}
// try to push out of solids if we're in them
else
{
var from = cameraLookAt;// - Vec3.UnitZ * (onGround ? 0 : 6);
var to = cameraPosition;
var normal = (to - from).Normalized();
// reduce distance by a bit to account for near plane cutoff
var distance = (to - from).Length();
if (distance > World.Camera.NearPlane + 1)
distance -= World.Camera.NearPlane;
// inside a wall, push out
if (World.SolidRayCast(from, normal, distance, out var hit, false, true))
{
if ((hit.Intersections % 2) == 1)
{
snapRequested = true;
cameraPosition = hit.Point;
}
}
// push down from ceilings a bit
if (World.SolidRayCast(cameraPosition, Vec3.UnitZ, 5, out hit, true, true))
{
cameraPosition = hit.Point - Vec3.UnitZ * 5;
}
}
}
#endregion
#region Various Methods
private Vec2 RelativeMoveInput
{
get
{
if (Controls.Move.Value == Vec2.Zero)
return Vec2.Zero;
Vec2 forward, side;
var cameraForward = (World.Camera.LookAt - World.Camera.Position).Normalized().XY();
if (cameraForward.X == 0 && cameraForward.Y == 0)
forward = targetFacing;
else
forward = cameraForward.Normalized();
side = Vec2.Transform(forward, Matrix3x2.CreateRotation(MathF.PI / 2));
Vec2 input = -Controls.Move.Value.Normalized();
if (Vec2.Dot(input, Vec2.UnitY) >= .985f)
input = Vec2.UnitY;
return forward * input.Y + side * input.X;
}
}
public void SetTargetFacing(Vec2 facing)
{
targetFacing = facing;
}
public void SetHairColor(Color color)
{
foreach (var mat in Model.Materials)
{
if (mat.Name == "Hair")
{
mat.Color = color;
mat.Effects = 0;
}
mat.SilhouetteColor = color;
}
Hair.Color = color;
Hair.Nodes = (InFeatherState ? 18 : (dashes >= 2 ? 16 : 10));
}
public void SweepTestMove(Vec3 delta, bool resolveImpact)
{
if (delta.LengthSquared() <= 0)
return;
var remaining = delta.Length();
var stepSize = 2.0f;
var stepNormal = delta / remaining;
while (remaining > 0)
{
// perform step
var step = MathF.Min(remaining, stepSize);
remaining -= step;
Position += stepNormal * step;
if (Popout(resolveImpact))
{
// don't repeatedly resolve wall impacts
resolveImpact = false;
}
}
}
/// <summary>
/// Pops out of Solid Geometry. Returns true if popped out of a wall
/// </summary>
public bool Popout(bool resolveImpact)
{
// ground test
if (GroundCheck(out var pushout, out _, out _))
{
Position += pushout;
if (resolveImpact)
velocity.Z = MathF.Max(velocity.Z, 0);
}
// ceiling test
else if (CeilingCheck(out pushout))
{
Position += pushout;
if (resolveImpact)
velocity.Z = MathF.Min(velocity.Z, 0);
}
// wall test
if (World.SolidWallCheckNearest(SolidWaistTestPos, WallPushoutDist, out var hit) ||
World.SolidWallCheckNearest(SolidHeadTestPos, WallPushoutDist, out hit))
{
// feather state handling
if (resolveImpact && stateMachine.State == States.Feather && tFeatherWallBumpCooldown <= 0 && !(Controls.Climb.Down && TryClimb()))
{
Position += hit.Pushout;
velocity = velocity.WithXY(Vec2.Reflect(velocity.XY(), hit.Normal.XY().Normalized()));
tFeatherWallBumpCooldown = 0.50f;
Audio.Play(Sfx.sfx_feather_state_bump_wall, Position);
}
// is it a breakable thing?
else if (resolveImpact && hit.Actor is BreakBlock breakable && !breakable.Destroying && velocity.XY().Length() > 90)
{
BreakBlock(breakable, velocity.Normalized());
}
// normal wall
else
{
Position += hit.Pushout;
if (resolveImpact)
{
var dot = MathF.Min(0.0f, Vec3.Dot(velocity.Normalized(), hit.Normal));
velocity -= hit.Normal * velocity.Length() * dot;
}
}
return true;
}
return false;
}
public void CancelGroundSnap() =>
tGroundSnapCooldown = 0.1f;
private void Jump()
{
Position = Position with { Z = coyoteZ };
holdJumpSpeed = velocity.Z = JumpSpeed;
tHoldJump = JumpHoldTime;
tCoyote = 0;
autoJump = false;
var input = RelativeMoveInput;
if (input != Vec2.Zero)
{
input = input.Normalized();
targetFacing = input;
velocity += new Vec3(input * JumpXYBoost, 0);
}
AddPlatformVelocity(true);
CancelGroundSnap();
ModelScale = new(.6f, .6f, 1.4f);
Audio.Play(Sfx.sfx_jump, Position);
}
private void WallJump()
{
holdJumpSpeed = velocity.Z = JumpSpeed;
tHoldJump = JumpHoldTime;
autoJump = false;
var velXY = targetFacing * WallJumpXYSpeed;
velocity = velocity.WithXY(velXY);
AddPlatformVelocity(false);
CancelGroundSnap();
ModelScale = new(.6f, .6f, 1.4f);
Audio.Play(Sfx.sfx_jump_wall, Position);
}
private void SkidJump()
{
Position = Position with { Z = coyoteZ };
holdJumpSpeed = velocity.Z = SkidJumpSpeed;
tHoldJump = SkidJumpHoldTime;
tCoyote = 0;
var velXY = targetFacing * SkidJumpXYSpeed;
velocity = velocity.WithXY(velXY);
AddPlatformVelocity(false);
CancelGroundSnap();
for (int i = 0; i < 16; i ++)
{
var dir = new Vec3(Calc.AngleToVector((i / 16f) * MathF.Tau), 0);
World.Request<Dust>().Init(Position + dir * 8, new Vec3(velocity.XY() * 0.5f, 10) - dir * 50, 0x666666);
}
ModelScale = new(.6f, .6f, 1.4f);
Audio.Play(Sfx.sfx_jump, Position);
Audio.Play(Sfx.sfx_jump_skid, Position);
}
private void DashJump()
{
Position = Position with { Z = coyoteZ };
velocity.Z = DashJumpSpeed;
holdJumpSpeed = DashJumpHoldSpeed;
tHoldJump = DashJumpHoldTime;
tCoyote = 0;
autoJump = false;
dashes = 1;
if (DashJumpXYBoost != 0)
{
var input = RelativeMoveInput;
if (input != Vec2.Zero)
{
input = input.Normalized();
targetFacing = input;
velocity += new Vec3(input * DashJumpXYBoost, 0);
}
}
AddPlatformVelocity(false);
CancelGroundSnap();
ModelScale = new(.6f, .6f, 1.4f);
Audio.Play(Sfx.sfx_jump, Position);
Audio.Play(Sfx.sfx_jump_superslide, Position);
}
private void AddPlatformVelocity(bool playSound)
{
if (tPlatformVelocityStorage > 0)
{
Vec3 add = platformVelocity;
add.Z = Calc.Clamp(add.Z, 0, 180);
if (add.XY().LengthSquared() > 300 * 300)
add = add.WithXY(add.XY().Normalized() * 300);
velocity += add;
platformVelocity = Vec3.Zero;
tPlatformVelocityStorage = 0;
if (playSound && (add.Z >= 10 || add.XY().Length() > 10))
Audio.Play(Sfx.sfx_jump_assisted, Position);
}
}
public void Kill()
{
stateMachine.State = States.Dead;
storedCameraForward = cameraTargetForward;
storedCameraDistance = cameraTargetDistance;
Save.CurrentRecord.Deaths++;
Dead = true;
}
private bool ClimbCheckAt(Vec3 offset, out WallHit hit)
{
if (World.SolidWallCheckClosestToNormal(SolidWaistTestPos + offset, ClimbCheckDist, -new Vec3(targetFacing, 0), out hit)
&& (RelativeMoveInput == Vec2.Zero || Vec2.Dot(hit.Normal.XY().Normalized(), RelativeMoveInput) <= -0.5f)
&& ClimbNormalCheck(hit.Normal))
return true;
return false;
}
private bool TryClimb()
{
var result = ClimbCheckAt(Vec3.Zero, out var wall);
// let us snap up to walls if we're jumping for them
// note: if vel.z is allowed to be downwards then we awkwardly re-grab when sliding off
// the bottoms of walls, which is really bad feeling
if (!result && Velocity.Z > 0 && !onGround && stateMachine.State != States.Climbing)
result = ClimbCheckAt(Vec3.UnitZ * 4, out wall);
if (result)
{
climbingWallNormal = wall.Normal;
climbingWallActor = wall.Actor;
var moveTo = wall.Point + (Position - SolidWaistTestPos) + wall.Normal * WallPushoutDist;
SweepTestMove(moveTo - Position, false);
targetFacing = -climbingWallNormal.XY().Normalized();
return true;
}
else
{
climbingWallActor = default;
climbingWallNormal = default;
return false;
}
}
private bool ClimbNormalCheck(in Vec3 normal)
{
return MathF.Abs(normal.Z) < 0.35f;
}
private bool FloorNormalCheck(in Vec3 normal)
=> !ClimbNormalCheck(normal) && normal.Z > 0;
private bool WallJumpCheck()
{
if (Controls.Jump.Pressed
&& World.SolidWallCheckClosestToNormal(SolidWaistTestPos, ClimbCheckDist, -new Vec3(targetFacing, 0), out var hit))
{
Controls.Jump.ConsumePress();
Position += (hit.Pushout * (WallPushoutDist / ClimbCheckDist));
targetFacing = hit.Normal.XY().Normalized();
return true;
}
else
return false;
}
private void BreakBlock(BreakBlock block, Vec3 direction)
{
World.HitStun = 0.1f;
block.Break(direction);
if (block.BouncesPlayer)
{
velocity.X = -velocity.X * 0.80f;
velocity.Y = -velocity.Y * 0.80f;
velocity.Z = 100;
stateMachine.State = States.Normal;
CancelGroundSnap();
}
}
internal void Spring(Spring spring)
{
stateMachine.State = States.Normal;
Position = Position with { Z = Calc.Approach(Position.Z, spring.Position.Z + 3, 2) };
var posXY = Position.XY();
Calc.Approach(ref posXY, spring.Position.XY(), 4);
Position = Position.WithXY(posXY);
holdJumpSpeed = velocity.Z = SpringJumpSpeed;
tHoldJump = SpringJumpHoldTime;
tCoyote = 0;
autoJump = true;
var velXY = velocity.XY();
Calc.Approach(ref velXY, Vec2.Zero, 30);
velocity = velocity.WithXY(velXY);
dashes = Math.Max(dashes, 1);
CancelGroundSnap();
}
#endregion
#region Normal State
private const float FootstepInterval = .3f;
private float tHoldJump;
private float holdJumpSpeed;
private bool autoJump;
private float tNoMove;
private float tFootstep;
private void StNormalEnter()
{
tHoldJump = 0;
tFootstep = FootstepInterval;
}
private void StNormalExit()
{
tHoldJump = 0;
tNoMove = 0;
autoJump = false;
Model.Rate = 1;
}
private void StNormalUpdate()
{
// Check for NPC interaction
if (onGround)
{
foreach (var actor in World.All<NPC>())
if (actor is NPC npc && npc.InteractEnabled)
{
if ((Position - npc.Position).LengthSquared() < npc.InteractRadius * npc.InteractRadius &&
Vec2.Dot((npc.Position - Position).XY(), targetFacing) > 0 &&
MathF.Abs(npc.Position.Z - Position.Z) < 2)
{
npc.IsPlayerOver = true;
if (Controls.Dash.ConsumePress())
{
npc.Interact(this);
return;
}
break;
}
}
}
// movement
{
var velXY = velocity.XY();
if (Controls.Move.Value == Vec2.Zero || tNoMove > 0)
{
// if not moving, simply apply friction