This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cs
2761 lines (2305 loc) · 141 KB
/
MainWindow.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
using Celeste_WinForms.Properties;
using SharpDX.DirectInput;
using System.Diagnostics;
using XInputDotNetPure;
using ButtonState = XInputDotNetPure.ButtonState;
namespace Celeste_WinForms;
public partial class MainWindow : Form {
// Global variables
/// Player
bool inputEnabled = false;
bool left, right;
bool leftInput, rightInput;
bool upInput, downInput;
bool jump, jumpInput, spaceReleased = true;
bool slide;
bool grab, grabInput;
bool grabFirstTimeSupport; // Pomoc pøi trackování pøichycení
string facing = "", lastStraightFacing = "Right", lastStraightFacingBefore;
bool midAir;
bool jumpCooldown = false;
bool grabAfterJumpCooldown = false;
public static int movementSpeed;
int movementSpeedTarget = 0;
int movementSpeedMax = 6;
public static int force;
bool closeToBlockLeft, closeToBlockRight;
int closeToBlockLeftDist, closeToBlockRightDist;
bool onBlockLeft, onBlockRight;
bool closeToBlockDown, closeToBlockUp;
int closeToBlockDownDist, closeToBlockUpDist;
bool onBlockDown, onBlockUp;
int playerBlockHeightDiff;
bool climbed;
string lastGrabbedOn = "";
bool dashInput, ctrlReleased = true;
bool dashed, dashedBefore;
bool dashedNonVertical;
int playerLeftOffset, playerRightOffset;
enum PlayerAnim { Climb, Dangling, Dash, Fall, Idle, Jump, Walk }
PlayerAnim playerAnimNow;
PlayerAnim playerAnimBefore;
PlayerAnim playerAnimQueue;
/// Falling block
bool rodeOnFallingBlock;
/// Interface
string playingOn;
Point cursor;
Point previousCursor;
string settingsOpenedFrom = "mainmenu";
string controlsOpenedFrom = "mainmenu";
int langIndex = 0;
int textScaleIndex = 0;
/// Level
int currentLevel = 1;
int spawnLocation = 0; // For level5 (multiple spawn locations)
// For level6 - end
Stopwatch swEnd = new Stopwatch(); // Stopwatch for end animation
bool endScreen = false;
bool grabbedOn = false;
// For level 9 - Ben
int benClickI = 0;
int benAnimI = 0;
bool benCanClick = true;
int benSoundLast;
static Terrain[] terrainArray;
static Strawberry[] strawberryArray;
public static List<string> strawberryCollected = new List<string> { };
/// Sound
public enum SoundTypes {
Dash, Death, FallingblockShake, Grab, GrabLetgo, Jump, JumpWall, Land, Spring, StrawberryGet, StrawberryTouch, ZipmoverTouch, ZipmoverImpact, ZipmoverReturn, ZipmoverReset,
BenBen, BenHaha, BenHm, BenKYS, BenMaybe, BenNo, BenPleading, BenYes
}
public static SoundTypes? soundQueue;
public int soundOutputType = 0;
bool grabbedBefore; // If was player grabbed last tick - for playing sound only once
bool touchedGround = true;
int activeElevatorsCount = 0;
/// Camera
int cameraMovementSpeed, cameraMovementSpeedTarget;
int playerCenterY;
// Gaming controllers
/// Unified inputs
bool cA, cB, cX, cY;
bool cLeft, cRight, cUp, cDown;
bool cTrigger;
bool cOptions;
bool clickedController;
/// Xbox controller
private GamePadState previousXboxState;
GamePadState xboxState;
bool xboxA, xboxB, xboxX, xboxY;
bool xboxLeft, xboxRight, xboxUp, xboxDown;
bool xboxTrigger;
bool xboxOptions;
/// PlayStation controller
Joystick joystick;
bool psControllerConnected;
bool[]? previousButtons;
bool previousButtonsCleared = true;
bool psA, psB, psX, psY;
bool psLeft, psRight, psUp, psDown;
bool psTrigger;
bool psOptions;
bool developerKeys; // NumPad0 pressed
// Protect timers from pause (if paused, timers will reset on continue)
bool tTimerJumpCooldown;
bool tTimerJumpHeadBumpCooldown;
bool tTimerGrabAfterJumpCooldown;
public MainWindow() {
InitializeComponent();
ConfigFileUpdate(0, "load");
SettingsUpdate();
Level1();
FindControllers();
menuMainContainer.Enabled = true; menuMainContainer.Visible = true;
PlayingOnInfo("keyboard");
}
#region Gaming controllers
private void timerControllers_Tick(object sender, EventArgs e) {
// Input reset
xboxA = false; xboxB = false; xboxX = false; xboxY = false;
xboxLeft = false; xboxRight = false; xboxUp = false; xboxDown = false;
xboxTrigger = false; xboxOptions = false;
psA = false; psB = false; psX = false; psY = false;
psLeft = false; psRight = false; psUp = false; psDown = false;
psTrigger = false; psOptions = false;
// Xbox
GamePadState xboxState = GamePad.GetState(PlayerIndex.One);
if (xboxState.IsConnected) {
if (inputEnabled) {
xboxA = xboxState.Buttons.A == ButtonState.Pressed;
xboxB = xboxState.Buttons.B == ButtonState.Pressed;
xboxX = xboxState.Buttons.X == ButtonState.Pressed;
xboxY = xboxState.Buttons.Y == ButtonState.Pressed;
xboxLeft =
xboxState.ThumbSticks.Left.X < -0.4 ||
xboxState.DPad.Left == ButtonState.Pressed;
xboxRight =
xboxState.ThumbSticks.Left.X > 0.4 ||
xboxState.DPad.Right == ButtonState.Pressed;
xboxUp =
xboxState.ThumbSticks.Left.Y > 0.4 ||
xboxState.DPad.Up == ButtonState.Pressed;
xboxDown =
xboxState.ThumbSticks.Left.Y < -0.4 ||
xboxState.DPad.Down == ButtonState.Pressed;
xboxTrigger =
xboxState.Triggers.Left > 0 ||
xboxState.Triggers.Right > 0 ||
xboxState.Buttons.LeftShoulder == ButtonState.Pressed ||
xboxState.Buttons.RightShoulder == ButtonState.Pressed;
}
xboxOptions = xboxState.Buttons.Start == ButtonState.Pressed &&
previousXboxState.Buttons.Start == ButtonState.Released;
#region Input is "xbox" if controller is touched
if (xboxState.Buttons.A != previousXboxState.Buttons.A ||
xboxState.Buttons.B != previousXboxState.Buttons.B ||
xboxState.Buttons.X != previousXboxState.Buttons.X ||
xboxState.Buttons.Y != previousXboxState.Buttons.Y ||
xboxState.Buttons.Start != previousXboxState.Buttons.Start ||
xboxState.Buttons.Back != previousXboxState.Buttons.Back ||
xboxState.Buttons.Guide != previousXboxState.Buttons.Guide ||
xboxState.Buttons.RightStick != previousXboxState.Buttons.RightStick ||
xboxState.Buttons.LeftShoulder != previousXboxState.Buttons.LeftShoulder ||
xboxState.Buttons.RightShoulder != previousXboxState.Buttons.RightShoulder ||
xboxState.DPad.Left != previousXboxState.DPad.Left ||
xboxState.DPad.Right != previousXboxState.DPad.Right ||
xboxState.DPad.Up != previousXboxState.DPad.Up ||
xboxState.DPad.Down != previousXboxState.DPad.Down) {
PlayingOnInfo("xbox");
clickedController = true;
}
if (xboxState.ThumbSticks.Left.X >= 0.1 ||
xboxState.ThumbSticks.Left.X <= -0.1 ||
xboxState.ThumbSticks.Left.Y >= 0.1 ||
xboxState.ThumbSticks.Left.Y <= -0.1 ||
xboxState.ThumbSticks.Right.X >= 0.1 ||
xboxState.ThumbSticks.Right.X <= -0.1 ||
xboxState.ThumbSticks.Right.Y >= 0.1 ||
xboxState.ThumbSticks.Right.Y <= -0.1 ||
xboxState.Triggers.Left != 0 ||
xboxState.Triggers.Right != 0) {
PlayingOnInfo("xbox");
}
#endregion
previousXboxState = xboxState;
} else {
previousXboxState = GamePad.GetState(PlayerIndex.One);
}
// PlayStation
try {
if (psControllerConnected) {
joystick.Poll();
JoystickState state = joystick.GetCurrentState();
bool[] buttons = state.Buttons;
if (previousButtonsCleared) {
previousButtons = buttons;
previousButtonsCleared = false;
}
if (inputEnabled) {
psA = buttons[1];
psB = buttons[2];
psX = buttons[0];
psY = buttons[3];
psLeft =
(state.X / 32768.0f) - 1 < -0.4 ||
state.PointOfViewControllers[0] == 22500 || // Down-Left
state.PointOfViewControllers[0] == 27000 || // Left
state.PointOfViewControllers[0] == 31500; // Up-Left
psRight =
(state.X / 32768.0f) - 1 > 0.4 ||
state.PointOfViewControllers[0] == 4500 || // Up-Right
state.PointOfViewControllers[0] == 9000 || // Right
state.PointOfViewControllers[0] == 13500; // Down-Right
psUp =
(state.Y / 32768.0f) - 1 < -0.4 ||
state.PointOfViewControllers[0] == 0 || // Up
state.PointOfViewControllers[0] == 4500 || // Up-Right
state.PointOfViewControllers[0] == 31500; // Up-Left
psDown =
(state.Y / 32768.0f) - 1 > 0.4 ||
state.PointOfViewControllers[0] == 13500 || // Down-Right
state.PointOfViewControllers[0] == 18000 || // Down
state.PointOfViewControllers[0] == 22500; // Down-Left
psTrigger =
buttons[4] ||
buttons[5];
}
psOptions = buttons[9] && !previousButtons[9];
#region Input is "playstation" if controller is touched
for (int i = 0; i <= 13; i++) {
if (buttons[i] != previousButtons[i]) {
PlayingOnInfo("playstation");
clickedController = true;
}
}
if ((state.X / 32768.0f) - 1 >= 0.1 ||
(state.X / 32768.0f) - 1 <= -0.1 ||
(state.Y / 32768.0f) - 1 >= 0.1 ||
(state.Y / 32768.0f) - 1 <= -0.1) {
PlayingOnInfo("playstation");
}
#endregion
previousButtons = buttons;
}
} catch (Exception) {
previousButtons = new bool[0];
previousButtonsCleared = true;
psControllerConnected = false;
return;
}
// Unifying Xbox and PlayStation inputs
cA = xboxA || psA;
cB = xboxB || psB;
cX = xboxX || psX;
cY = xboxY || psY;
cLeft = xboxLeft || psLeft;
cRight = xboxRight || psRight;
cUp = xboxUp || psUp;
cDown = xboxDown || psDown;
cTrigger = xboxTrigger || psTrigger;
cOptions = xboxOptions || psOptions;
if (cOptions) {
Escape("controller");
}
// Mouse cursor
previousCursor = cursor;
cursor = PointToClient(Cursor.Position);
if (cursor != previousCursor) {
// Input is "keyboard" if mouse is moved
PlayingOnInfo("keyboard");
}
}
#endregion Herní ovladaèe
private void timer1_Tick(object sender, EventArgs e) {
// Default values
playerLeftOffset = player.Left + 0;
playerRightOffset = player.Right - 0;
playerCenterY = player.Top + player.Height / 2;
facing = "";
movementSpeedTarget = 0;
midAir = true;
closeToBlockLeft = false; closeToBlockRight = false;
closeToBlockLeftDist = movementSpeed; closeToBlockRightDist = movementSpeed;
onBlockLeft = false; onBlockRight = false;
closeToBlockLeftDist = 0;
playerBlockHeightDiff = 0;
slide = false;
grab = false;
playerAnimBefore = playerAnimNow;
dashedBefore = dashed;
lastStraightFacingBefore = lastStraightFacing;
UpdateCamera();
#region Movement
foreach (Terrain item in terrainArray) {
item.onBlockLeftExclusive = false; item.onBlockRightExclusive = false;
item.onBlockDown = false;
}
// Movement
/// Inputs
if (leftInput || cLeft) {
left = true;
facing = "Left";
lastStraightFacing = "Left";
} else
left = false;
if (rightInput || cRight) {
right = true;
facing = "Right";
lastStraightFacing = "Right";
} else
right = false;
if (upInput || cUp)
facing += "Up";
else if (downInput || cDown)
facing += "Down";
if (facing == "")
facing = lastStraightFacing;
// Interacton with blocks
foreach (Terrain block in terrainArray.Where(block => block.pb.Tag != null)) {
if (!block.pb.Tag.ToString().Contains("jump-through")) {
// Side collisions
if (block.pb.Tag.ToString().Contains("collision") && player.Bounds.IntersectsWith(block.pb.Bounds)) {
if (playerLeftOffset < block.pb.Right && player.Right > block.pb.Left + player.Width / 2 &&
player.Bottom > block.pb.Top + 1 && player.Top < block.pb.Bottom) {
left = false;
movementSpeed = 0;
}
if (playerRightOffset > block.pb.Left && player.Left < block.pb.Right - player.Width / 2 &&
player.Bottom > block.pb.Top + 1 && player.Top < block.pb.Bottom) {
right = false;
movementSpeed = 0;
}
}
// If the player is close to the block, it will only get closer by the difference between the edge of the player and the block (against bugs)
if (block.pb.Tag.ToString().Contains("collision")) {
if (playerLeftOffset - block.pb.Right < (Math.Abs(movementSpeed) < movementSpeedMax ? movementSpeedMax : Math.Abs(movementSpeed)) && playerLeftOffset - block.pb.Right >= 0 &&
player.Bottom > block.pb.Top + 1 && player.Top < block.pb.Bottom) {
closeToBlockLeft = true;
closeToBlockLeftDist = playerLeftOffset - block.pb.Right;
if (playerLeftOffset - block.pb.Right == 0 &&
player.Top + player.Height / 2 < block.pb.Bottom) {
onBlockLeft = true;
block.onBlockLeftExclusive = true;
midAir = false;
playerBlockHeightDiff = player.Bottom - block.pb.Top;
}
}
if (block.pb.Left - playerRightOffset < (movementSpeed < movementSpeedMax ? movementSpeedMax : movementSpeed) && block.pb.Left - playerRightOffset >= 0 &&
player.Bottom > block.pb.Top + 1 && player.Top < block.pb.Bottom) {
closeToBlockRight = true;
closeToBlockRightDist = block.pb.Left - playerRightOffset;
if (block.pb.Left - playerRightOffset == 0 &&
player.Top + player.Height / 2 < block.pb.Bottom) {
onBlockRight = true;
block.onBlockRightExclusive = true;
midAir = false;
playerBlockHeightDiff = player.Bottom - block.pb.Top;
}
}
}
// Slide activation
if (((onBlockLeft && (leftInput || cLeft)) || (onBlockRight && (rightInput || cRight))) && force < 0) {
slide = true;
midAir = false;
}
// Grab activation
if ((grabInput || cTrigger) && (onBlockLeft || onBlockRight)) {
grab = true;
soundMaterial = block.pb.Tag.ToString();
midAir = false;
// If grabbed, move 1 pixel up (against bugs)
if (!grabFirstTimeSupport) {
player.Top -= 1;
grabFirstTimeSupport = true;
}
lastGrabbedOn = onBlockLeft ? "Left" : onBlockRight ? "Right" : "";
}
}
// midAir
if (block.pb.Top - player.Bottom == -1 &&
playerLeftOffset < block.pb.Right && playerRightOffset > block.pb.Left) {
midAir = false;
}
}
// User holds Space/Jump
if ((jumpInput || (cA || cY)) && !midAir && !jumpCooldown && !grabAfterJumpCooldown) {
jump = true;
force = 15;
PlayerAnimationChanged(PlayerAnim.Jump);
if (((onBlockLeft || onBlockRight) && !onBlockDown) || // Touching block mid-air
slide) // Sliding on block
{
movementSpeed = onBlockLeft ? movementSpeedMax * 2 : onBlockRight ? -movementSpeedMax * 2 : 0;
force = 11;
PlaySound(SoundTypes.JumpWall);
} else if (!grab)
PlaySound(SoundTypes.Jump);
// Grabbed on block
if (grab) {
if (onBlockLeft && facing.Contains("Right") || onBlockRight && facing.Contains("Left"))
force = 13;
else
force = 11;
int grabJumpPower = 3;
movementSpeed += facing.Contains("Left") ? -movementSpeedMax * (grabJumpPower / 2) : facing.Contains("Right") ? movementSpeedMax * (grabJumpPower / 2) : 0;
grab = false;
grabAfterJumpCooldown = true;
timerGrabAfterJumpCooldown.Enabled = true;
PlaySound(SoundTypes.JumpWall);
}
jumpCooldown = true;
timerJumpCooldown.Enabled = true;
}
// Grab - Jump on top of block (<25px: bottom of player - top of block)
if (grab && !grabAfterJumpCooldown) {
if (playerBlockHeightDiff < 25) {
force = Math.Abs(playerBlockHeightDiff / 2);
climbed = true;
grab = false;
grabAfterJumpCooldown = true;
timerGrabAfterJumpCooldown.Enabled = true;
}
}
if (climbed && !(onBlockLeft || onBlockRight)) {
movementSpeed = lastGrabbedOn == "Left" ? -12 : lastGrabbedOn == "Right" ? 12 : 0;
climbed = false;
lastGrabbedOn = "";
}
// Dash ability
if ((dashInput || (cB || cX)) && !dashed) {
int dashPower = 4;
switch (facing) {
case "Right":
if (!onBlockRight)
movementSpeed = (dashPower * movementSpeedMax);
force = 0;
dashedNonVertical = true;
timerDashedNonVertical.Enabled = true;
break;
case "RightUp":
if (!onBlockRight)
movementSpeed = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * dashPower * movementSpeedMax);
force = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * dashPower * movementSpeedMax);
dashedNonVertical = true;
timerDashedNonVertical.Enabled = true;
break;
case "Up":
movementSpeed = 0;
force = dashPower * 5;
break;
case "LeftUp":
if (!onBlockLeft)
movementSpeed = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * -dashPower * movementSpeedMax);
force = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * dashPower * movementSpeedMax);
dashedNonVertical = true;
timerDashedNonVertical.Enabled = true;
break;
case "Left":
if (!onBlockLeft)
movementSpeed = -dashPower * movementSpeedMax;
force = 0;
dashedNonVertical = true;
timerDashedNonVertical.Enabled = true;
break;
case "LeftDown":
if (!onBlockLeft)
movementSpeed = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * -dashPower * movementSpeedMax);
if (!onBlockDown)
force = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * -dashPower * movementSpeedMax);
dashedNonVertical = true;
timerDashedNonVertical.Enabled = true;
break;
case "Down":
movementSpeed = 0;
if (!onBlockDown)
force = -dashPower * 5;
break;
case "RightDown":
if (!onBlockRight)
movementSpeed = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * dashPower * movementSpeedMax);
if (!onBlockDown)
force = Convert.ToInt32((double)(Math.Sqrt(2) / (double)2) * -dashPower * movementSpeedMax);
dashedNonVertical = true;
timerDashedNonVertical.Enabled = true;
break;
}
PlaySound(SoundTypes.Dash);
timerDashCooldown.Enabled = true;
PlayerAnimationChanged(PlayerAnim.Dash);
dashed = true;
}
// Grab ability
if (grab && !grabAfterJumpCooldown && Math.Abs(movementSpeed) < movementSpeedMax) {
if (!grabbedBefore)
PlaySound(SoundTypes.Grab);
grabbedBefore = true;
force = 0;
movementSpeedTarget = 0;
if (!((leftInput || cLeft) || (rightInput || cRight) || (upInput || cUp) || (downInput || cDown)))
facing = onBlockLeft ? "Left" : onBlockRight ? "Right" : lastStraightFacing;
if (facing == "Up" && !onBlockUp) {
if (!onBlockUp)
force = movementSpeedMax / 5 * 4;
else
force = closeToBlockUpDist;
}
if (facing == "Down" && !onBlockDown) {
if (!closeToBlockDown)
force = -movementSpeedMax / 5 * 4;
else
force = -closeToBlockDownDist;
}
player.Top -= force;
} else // Gravity
{
if (closeToBlockDown) {
player.Top += closeToBlockDownDist - 1;
force = 0;
} else if (closeToBlockUp) {
player.Top -= closeToBlockUpDist;
}
player.Top -= force;
if (force > (slide ? -2 : -25) && !dashedNonVertical & !closeToBlockDown) {
force -= 1;
}
grabbedBefore = false;
}
closeToBlockDown = false;
onBlockDown = false;
onBlockUp = false;
foreach (PictureBox block in gameScreen.Controls.OfType<PictureBox>().Where(block => block.Tag != null)) {
// If the player is close to the block, it only gets closer by the difference between the edge of the player and the block (against bugs)
// From block top
if (block.Tag.ToString().Contains("collision")) {
if (block.Top + 1 - player.Bottom <= -force + 1 &&
playerLeftOffset <= block.Right && playerRightOffset >= block.Left &&
player.Bottom < block.Top) {
closeToBlockDown = true;
closeToBlockDownDist = block.Top - player.Bottom + 1;
}
}
// Interaction with blocks
if (block.Tag.ToString().Contains("collision") && player.Bounds.IntersectsWith(block.Bounds) &&
playerLeftOffset < block.Right &&
playerRightOffset > block.Left) {
// Bottom collision (top of block)
if (player.Bottom == block.Top + 1 && player.Top < block.Top) {
player.Top = block.Top - player.Height + 1;
force = 0;
jump = false;
onBlockDown = true;
grabFirstTimeSupport = false;
foreach (Terrain item in terrainArray) {
item.onBlockDown = true;
}
closeToBlockDown = false;
if (!timerDashCooldown.Enabled)
dashed = false;
if (!block.Tag.ToString().Contains("spring")) {
if (!touchedGround) {
soundMaterial = block.Tag.ToString();
PlaySound(SoundTypes.Land);
}
touchedGround = true;
}
}
// If the player is close to the block, it only gets closer by the difference between the edge of the player and the block (against bugs)
// From block bottom
if (block.Tag.ToString().Contains("collision") && !block.Tag.ToString().Contains("jump-through")) {
if (player.Top - block.Bottom <= force + 1 &&
playerLeftOffset <= block.Right && playerRightOffset >= block.Left &&
player.Bottom < block.Top) {
closeToBlockUp = true;
closeToBlockUpDist = player.Top - block.Bottom;
if (player.Top - block.Bottom == 0) {
onBlockUp = true;
}
}
}
// Top collision (bottom of block)
if ((player.Top < block.Bottom && player.Bottom > block.Bottom) && !block.Tag.ToString().Contains("jump-through"))
{
if (force > 3)
force = -3;
else
force *= -1;
player.Top = block.Bottom;
jumpCooldown = true;
timerJumpHeadBumpCooldown.Enabled = true;
}
}
if (block.Tag.ToString().Contains("spring") && player.Bounds.IntersectsWith(block.Bounds) &&
playerLeftOffset < block.Right &&
playerRightOffset - 1 > block.Left) {
force = 22;
jump = true;
dashed = false;
PlaySound(SoundTypes.Spring);
PlayerAnimationChanged(playerAnimQueue);
}
}
// Sideways movement
if (left ^ right && !slide && !grab) {
if (left) {
movementSpeedTarget = -movementSpeedMax;
if (closeToBlockLeft) {
movementSpeed = 0;
player.Left -= closeToBlockLeftDist;
} else if (movementSpeed != movementSpeedTarget) {
movementSpeed += movementSpeed < movementSpeedTarget ? 1 : movementSpeed > movementSpeedTarget ? -1 : 0;
if (!midAir)
movementSpeed += movementSpeed < movementSpeedTarget ? 1 : movementSpeedTarget - movementSpeed;
}
}
if (right) {
movementSpeedTarget = movementSpeedMax;
if (closeToBlockRight) {
movementSpeed = 0;
player.Left += closeToBlockRightDist;
} else if (movementSpeed != movementSpeedTarget) {
movementSpeed += movementSpeed < movementSpeedTarget ? 1 : movementSpeed > movementSpeedTarget ? -1 : 0;
if (!midAir)
movementSpeed += movementSpeed > movementSpeedTarget ? -1 : movementSpeedTarget - movementSpeed;
}
}
} else {
if (movementSpeed != 0) {
movementSpeedTarget = 0;
if (movementSpeed < -closeToBlockLeftDist && closeToBlockLeft) {
movementSpeed = 0;
player.Left -= closeToBlockLeftDist;
} else if (movementSpeed > closeToBlockRightDist && closeToBlockRight) {
movementSpeed = 0;
player.Left += closeToBlockRightDist;
} else
movementSpeed += movementSpeed < movementSpeedTarget ? 1 : movementSpeed > movementSpeedTarget ? -1 : 0;
}
}
player.Left += movementSpeed;
#endregion Movement
#region Level teleports
foreach (Terrain teleport in terrainArray.Where(teleport => teleport.pb.Tag.ToString().Contains("level"))) {
if (player.Bounds.IntersectsWith(teleport.pb.Bounds)) {
string levelNumber = "";
foreach (char letter in teleport.pb.Tag.ToString()) {
if (char.IsDigit(letter))
levelNumber += letter;
}
SpawnLevel(Convert.ToInt32(levelNumber));
}
}
// Level end
foreach (Terrain end in terrainArray.Where(end => end.pb.Tag.ToString() == "end")) {
if (player.Bounds.IntersectsWith(end.pb.Bounds)) {
timerEndAnim.Enabled = true;
}
}
#endregion
#region Elevators
foreach (Terrain elevator in terrainArray.Where(elevator => elevator.pb.Tag.ToString().Contains("elevator"))) {
if ((player.Bounds.IntersectsWith(elevator.pb.Bounds) || (elevator.onBlockLeftExclusive || elevator.onBlockRightExclusive)) && !elevator.moving)
elevator.moving = true;
if (elevator.moving) {
elevator.elevatorTexturePhase = 1;
elevator.ElevatorAnimation(player, grabbedOn, playerLeftOffset, playerRightOffset, movementSpeed);
if (elevator.resetForce)
force = 0;
if (elevator.playerKill) {
elevator.playerKill = false;
PlaySound(SoundTypes.Death);
SpawnLevel(currentLevel);
}
}
// Change elevator texture
switch (elevator.elevatorTexturePhase) {
case 0:
switch (elevator.pb.Width) {
case 120:
elevator.pb.BackgroundImage = Resources.elevator3_red;
break;
case 160:
elevator.pb.BackgroundImage = Resources.elevator4_red;
break;
case 200:
elevator.pb.BackgroundImage = Resources.elevator5_red;
break;
}
break;
case 1:
switch (elevator.pb.Width) {
case 120:
elevator.pb.BackgroundImage = Resources.elevator3_green;
break;
case 160:
elevator.pb.BackgroundImage = Resources.elevator4_green;
break;
case 200:
elevator.pb.BackgroundImage = Resources.elevator5_green;
break;
}
break;
case 2:
switch (elevator.pb.Width) {
case 120:
elevator.pb.BackgroundImage = Resources.elevator3_yellow;
break;
case 160:
elevator.pb.BackgroundImage = Resources.elevator4_yellow;
break;
case 200:
elevator.pb.BackgroundImage = Resources.elevator5_yellow;
break;
}
break;
}
}
#endregion
#region Falling blocks
foreach (Terrain fallingBlock in terrainArray.Where(fallingBlock => fallingBlock.pb.Tag.ToString().Contains("falling"))) {
if ((player.Bounds.IntersectsWith(fallingBlock.pb.Bounds) || (fallingBlock.onBlockLeftExclusive || fallingBlock.onBlockRightExclusive)) && !fallingBlock.falling && !fallingBlock.fallen)
fallingBlock.falling = true;
if (fallingBlock.falling) {
if (player.Bounds.IntersectsWith(fallingBlock.pb.Bounds) || (fallingBlock.onBlockLeftExclusive || fallingBlock.onBlockRightExclusive)) {
rodeOnFallingBlock = true;
fallingBlock.onFallingBlock = true;
} else {
fallingBlock.onFallingBlock = false;
}
foreach (Terrain terrain in terrainArray.Where(terrain => terrain.pb.Tag.ToString().Contains("collision"))) {
if (fallingBlock.pb.Bounds.IntersectsWith(terrain.pb.Bounds) &&
terrain != fallingBlock) {
fallingBlock.fallingOnGround = true;
fallingBlock.fallingGroundedPos = terrain.pb.Top - fallingBlock.pb.Height;
}
}
fallingBlock.FallingAnimation(player);
// Player jumps off when in motion
if (!((player.Bounds.IntersectsWith(fallingBlock.pb.Bounds) || (fallingBlock.onBlockLeftExclusive || fallingBlock.onBlockRightExclusive))) && fallingBlock.moving && rodeOnFallingBlock) {
force -= fallingBlock.fallingForce;
rodeOnFallingBlock = false;
}
}
}
#endregion
#region Strawberries
foreach (Strawberry strawberry in strawberryArray) {
if (player.Bounds.IntersectsWith(strawberry.pb.Bounds))
strawberry.tracking = true;
if (strawberry.tracking) {
strawberry.TrackTarget(player);
// If player is on ground, start CollectingTime timer
if (onBlockDown && !strawberry.timerCollectingTime.Enabled) {
strawberry.timerCollectingTime.Enabled = true;
} else if (!onBlockDown && strawberry.timerCollectingTime.Enabled) {
strawberry.timerCollectingTime.Enabled = false;
}
}
}
#endregion
#region Spikes
foreach (Terrain spike in terrainArray.Where(spike => spike.pb.Tag.ToString().Contains("spike"))) {
if (player.Bounds.IntersectsWith(spike.pb.Bounds) || spike.onBlockLeftExclusive || spike.onBlockRightExclusive) {
PlaySound(SoundTypes.Death);
SpawnLevel(currentLevel);
}
}
#endregion
#region Player texture
if (onBlockDown) {
if (Math.Abs(movementSpeed) > 0) {
playerAnimNow = PlayerAnim.Walk;
} else {
playerAnimNow = PlayerAnim.Idle;
}
}
if (midAir) {
playerAnimNow = PlayerAnim.Fall;
}
if (grab) {
if (Math.Abs(force) > 0) {
playerAnimNow = PlayerAnim.Climb;
} else {
playerAnimNow = PlayerAnim.Dangling;
}
}
// If state changed, change texture
if (playerAnimBefore != playerAnimNow ||
dashed != dashedBefore ||
lastStraightFacingBefore != lastStraightFacing) {
PlayerAnimationChanged(playerAnimNow);
}
#endregion
#region Sound queue from other classes
if (soundQueue != null) {
PlaySound((SoundTypes)soundQueue);
soundQueue = null;
}
#endregion
// Developer stats [F3]
lbDeveloperStats.Text =
$"Cursor: [{cursor.X}; {cursor.Y}]" +
$"\r\nPlayer: [{player.Left}; {player.Bottom}]" +
$"\r\nCameraMovementSpeed: {cameraMovementSpeed}" +
$"\r\nCameraMovementSpeedTarget: {cameraMovementSpeedTarget}" +
$"\r\nMovementSpeed: {movementSpeed}" +
$"\r\nMovementSpeedTarget: {movementSpeedTarget}" +
$"\r\nMovementSpeedMax: {movementSpeedMax}" +
$"\r\nForce: {force}" +
$"\r\nFacing: {facing}" +
$"\r\nJump: {jump}" +
$"\r\nJumpInput: {jumpInput}" +
$"\r\nMidAir: {midAir}" +
$"\r\nJumpCooldown: {jumpCooldown}" +
$"\r\nCloseToBlock: {(closeToBlockLeft ? "Left" : closeToBlockRight ? "Right" : "none")}" +
$"\r\nOnBlock: {(onBlockLeft ? "Left" : onBlockRight ? "Right" : "none")}" +
$"\r\nCloseToBlockDown: {closeToBlockDown} ({closeToBlockDownDist})" +
$"\r\nOnBlockDown: {onBlockDown}" +
$"\r\nPlayerBlockHeightDiff: {playerBlockHeightDiff}" +