-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
1572 lines (1325 loc) · 53.1 KB
/
game.js
File metadata and controls
1572 lines (1325 loc) · 53.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
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
// ============================================================================
// JUMPING DEV - Main Game File
// A jumping jack simulator game built with Phaser 3
// ============================================================================
// Game configuration
const config = {
type: Phaser.AUTO,
width: 1000,
height: 800,
parent: 'game-area',
backgroundColor: '#87CEEB',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 2000 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
// Game state variables
let game;
let character = {};
let keys = {};
let previousKeyStates = {}; // Track previous frame key states for press detection
let gameState = 'idle'; // idle, jumping, landing, waiting_for_reset
let messageText = null;
let ouchText = null; // Separate text for OUCH message
let jumpData = {
isJumping: false,
jumpStartTime: 0,
wKeyPressTime: 0, // Track when W was first pressed
jumpChargeTime: 0, // How long W was held
maxHeight: 0,
currentHeight: 0,
leftArmMaxAngle: 0,
rightArmMaxAngle: 0,
leftLegMaxSpread: 0,
rightLegMaxSpread: 0,
leftArmPeakedInAir: false,
rightArmPeakedInAir: false,
legsSpreading: false,
limbsMoved: { leftArm: false, rightArm: false, leftLeg: false, rightLeg: false },
didSplits: false
};
// Game scoring
let totalScore = 0;
let currentJumpNumber = 0;
let jumpScores = [];
let lastJumpBreakdown = null;
// Difficulty settings
let isHardMode = false;
let difficultyMultiplier = 1.0;
// Character physics constants (base values)
const BASE_JUMP_VELOCITY = -800;
const JUMP_MAX_HEIGHT = 200;
const BASE_ARM_ROTATION_SPEED = 0.5; // Faster arm movement
const BASE_ARM_FALL_SPEED = 0.3; // Ragdoll-like falling
const ARM_GRAVITY = 0.015; // Gravity acceleration for arms
const BASE_LEG_ROTATION_SPEED = 0.975; // 30% faster (0.75 * 1.3)
const BASE_LEG_RETURN_SPEED = 1.17; // 30% faster (0.9 * 1.3)
const MAX_ARM_ANGLE = 210; // degrees (arms can go past vertical and touch overhead)
const MAX_LEG_ANGLE = 60; // degrees (legs spread - allow splits)
// Active physics values (modified by difficulty)
let JUMP_VELOCITY = BASE_JUMP_VELOCITY;
let ARM_ROTATION_SPEED = BASE_ARM_ROTATION_SPEED;
let ARM_FALL_SPEED = BASE_ARM_FALL_SPEED;
let LEG_ROTATION_SPEED = BASE_LEG_ROTATION_SPEED;
let LEG_RETURN_SPEED = BASE_LEG_RETURN_SPEED;
// Ground and character positioning
let groundY;
let characterGroundY;
let targetMarkers = { left: null, right: null };
// Fireworks particles
let fireworksParticles = [];
// ============================================================================
// PHASER LIFECYCLE FUNCTIONS
// ============================================================================
function preload() {
// No external assets needed - we'll draw everything programmatically
}
function create() {
groundY = this.cameras.main.height * 0.8;
// Position character in middle of grass area (10% from bottom = 90% from top)
characterGroundY = this.cameras.main.height * 0.9;
// Create background layers
createBackground(this);
// Create character
createCharacter(this);
// Create target markers
createTargetMarkers(this);
// Setup keyboard input
setupInput(this);
// Create OUCH message text (5% from top, red, centered)
ouchText = this.add.text(
this.cameras.main.width / 2,
this.cameras.main.height * 0.05,
'',
{
fontSize: '48px',
fontFamily: 'Arial',
color: '#FF0000',
fontStyle: 'bold',
stroke: '#FFFFFF',
strokeThickness: 6
}
);
ouchText.setOrigin(0.5, 0.5);
ouchText.setVisible(false);
// Create general message text (30% from top, black, centered)
messageText = this.add.text(
this.cameras.main.width / 2,
this.cameras.main.height * 0.3,
'',
{
fontSize: '28px',
fontFamily: 'Arial',
color: '#000000',
fontStyle: 'bold',
stroke: '#FFFFFF',
strokeThickness: 4,
align: 'center'
}
);
messageText.setOrigin(0.5, 0.5);
messageText.setVisible(false);
// Create footer text at bottom of game area
const footerText = this.add.text(
this.cameras.main.width / 2,
this.cameras.main.height - 15,
'Made 100% with Claude Code. View source code on GitHub',
{
fontSize: '14px',
fontFamily: 'Arial',
color: '#FFFFFF',
alpha: 0.7
}
);
footerText.setOrigin(0.5, 1);
// Make the entire footer text clickable (simulate link appearance)
footerText.setInteractive({ useHandCursor: true });
footerText.on('pointerover', () => {
footerText.setColor('#00ff88');
});
footerText.on('pointerout', () => {
footerText.setColor('#FFFFFF');
});
footerText.on('pointerdown', () => {
window.open('https://github.com/kurtisf/jumping-dev', '_blank');
});
// Initialize UI
updateScoreDisplay();
}
function update(time, delta) {
// Always check for input (includes waiting_for_reset and splits states)
if (gameState === 'idle' || gameState === 'jumping' || gameState === 'waiting_for_reset' || gameState === 'splits') {
handleInput(delta);
}
if (gameState === 'idle' || gameState === 'jumping') {
updateCharacterPhysics(delta);
if (gameState === 'jumping') {
trackJumpData();
}
}
// Handle splits animation
if (gameState === 'splits') {
updateSplitsAnimation(delta);
}
// Update animations (but not legs if in waiting_for_reset state)
updateAnimations(delta);
// Update fireworks particles
updateFireworks(delta);
}
// ============================================================================
// BACKGROUND CREATION
// ============================================================================
function createBackground(scene) {
const width = scene.cameras.main.width;
const height = scene.cameras.main.height;
// Sky (top 60%) - Already set via backgroundColor
// Add clouds
createClouds(scene);
// Add flying crow
createCrow(scene);
// Add sun
createSun(scene);
// Bushes layer - moved down 10% more (70% -> 80% from top)
const bushY = height * 0.8;
createBushes(scene, bushY);
// Grass layer (bottom 20%)
createGrass(scene, groundY);
}
function createClouds(scene) {
const clouds = [];
const cloudCount = 8; // Increased from 4 to 8
for (let i = 0; i < cloudCount; i++) {
const cloud = scene.add.graphics();
cloud.fillStyle(0xFFFFFF, 0.7);
// Draw fluffy cloud shape with randomized sizes
const cloudScale = 0.7 + Math.random() * 0.6; // Vary cloud sizes
cloud.fillCircle(0, 0, 25 * cloudScale);
cloud.fillCircle(20 * cloudScale, -5 * cloudScale, 30 * cloudScale);
cloud.fillCircle(40 * cloudScale, 0, 25 * cloudScale);
cloud.fillCircle(20 * cloudScale, 10 * cloudScale, 20 * cloudScale);
// Spread clouds more evenly across the width with more spacing
const x = (i * scene.cameras.main.width * 1.5) / cloudCount;
const y = 50 + Math.random() * 200; // More vertical variation
cloud.setPosition(x, y);
cloud.cloudSpeed = 0.15 + Math.random() * 0.25; // Slightly varied speeds
clouds.push(cloud);
}
// Animate clouds
scene.time.addEvent({
delay: 50,
callback: () => {
clouds.forEach(cloud => {
cloud.x -= cloud.cloudSpeed;
if (cloud.x < -100) {
cloud.x = scene.cameras.main.width + 100;
}
});
},
loop: true
});
}
function createCrow(scene) {
// Create crow that flies across screen periodically
const crow = scene.add.graphics();
crow.fillStyle(0x000000, 1);
// Draw simple crow silhouette
// Body
crow.fillEllipse(0, 0, 12, 8);
// Head
crow.fillCircle(-6, -3, 5);
// Beak
crow.fillTriangle(-10, -3, -13, -2, -10, -1);
// Wing (will animate)
crow.wingUp = false;
const redrawCrow = (wingUp) => {
crow.clear();
crow.fillStyle(0x000000, 1);
// Body
crow.fillEllipse(0, 0, 12, 8);
// Head
crow.fillCircle(-6, -3, 5);
// Beak
crow.fillTriangle(-10, -3, -13, -2, -10, -1);
// Wings
if (wingUp) {
crow.fillEllipse(-3, -6, 10, 4);
crow.fillEllipse(3, -6, 10, 4);
} else {
crow.fillEllipse(-3, 2, 10, 4);
crow.fillEllipse(3, 2, 10, 4);
}
};
// Start off screen (right side)
const screenWidth = scene.cameras.main.width;
crow.setPosition(screenWidth * 3, 100); // Start 3 screen widths away
crow.setScale(1.5);
// Animate crow flying across (3 screen widths total, on screen for 1/3 of time)
const totalDistance = screenWidth * 3;
const duration = 45000; // 45 seconds for full journey
scene.tweens.add({
targets: crow,
x: -screenWidth,
duration: duration,
repeat: -1,
ease: 'Linear',
onRepeat: () => {
// Reset to start position
crow.setPosition(screenWidth * 3, 80 + Math.random() * 100);
}
});
// Flap wings
scene.time.addEvent({
delay: 200,
callback: () => {
crow.wingUp = !crow.wingUp;
redrawCrow(crow.wingUp);
},
loop: true
});
redrawCrow(false);
}
function createSun(scene) {
const sunX = scene.cameras.main.width - 100;
const sunY = 80;
// Rotating sun body
const sunBody = scene.add.graphics();
sunBody.lineStyle(4, 0xFFAA00, 1);
sunBody.fillStyle(0xFFDD00, 1);
sunBody.fillCircle(0, 0, 40);
// Draw sun rays
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const x1 = Math.cos(angle) * 45;
const y1 = Math.sin(angle) * 45;
const x2 = Math.cos(angle) * 60;
const y2 = Math.sin(angle) * 60;
sunBody.lineBetween(x1, y1, x2, y2);
}
sunBody.setPosition(sunX, sunY);
// Static face
const sunFace = scene.add.graphics();
sunFace.fillStyle(0x000000, 1);
// Eyes
sunFace.fillCircle(-12, -5, 4);
sunFace.fillCircle(12, -5, 4);
// Smile
sunFace.lineStyle(3, 0x000000, 1);
sunFace.arc(0, 5, 15, 0, Math.PI, false);
sunFace.strokePath();
sunFace.setPosition(sunX, sunY);
// Rotate sun body (60 seconds per rotation)
scene.tweens.add({
targets: sunBody,
angle: 360,
duration: 60000,
repeat: -1,
ease: 'Linear'
});
}
function createBushes(scene, y) {
const bushes = [];
const bushCount = 10;
for (let i = 0; i < bushCount; i++) {
const bushContainer = scene.add.container();
const bushGraphics = [];
// Randomize bush shape - each bush is unique
const numCircles = 4 + Math.floor(Math.random() * 3); // 4-6 circles per bush
const bushHeight = 60;
const bushWidth = 50 + Math.random() * 30;
for (let j = 0; j < numCircles; j++) {
const circle = scene.add.graphics();
circle.fillStyle(0x228B22, 1);
const offsetX = (Math.random() - 0.5) * bushWidth * 0.8;
// Circles only go upward from bottom (flat bottom)
const offsetY = -Math.random() * bushHeight;
const radius = 15 + Math.random() * 20;
circle.fillCircle(offsetX, offsetY, radius);
bushContainer.add(circle);
bushGraphics.push({ graphics: circle, offsetX, offsetY, radius });
}
// Add flat bottom base - wider light colored portion, upside down bowl dark portion
const baseGraphics = scene.add.graphics();
// Light colored base (wider, draw first at bottom)
baseGraphics.fillStyle(0x228B22, 1);
baseGraphics.fillEllipse(0, 0, bushWidth * 1.2, 18);
// Dark portion - rounded upside down bowl going upward from middle
baseGraphics.fillStyle(0x1a6b1a, 1);
// Use fillEllipse to create rounded bowl shape, positioned to go upward
baseGraphics.fillEllipse(0, -10, bushWidth * 0.8, 20); // Centered higher, 20px tall (half of 40)
bushContainer.add(baseGraphics);
// Position bushes with bottom touching grass line
const baseX = (i * scene.cameras.main.width) / bushCount + (Math.random() - 0.5) * 50;
bushContainer.setPosition(baseX, y);
bushContainer.baseX = baseX;
bushContainer.baseY = y;
bushContainer.swayOffset = i * 0.5;
bushContainer.bushGraphics = bushGraphics;
bushes.push(bushContainer);
}
// Animate bush swaying - wind effect (top moves more than bottom)
let swayTime = 0;
scene.time.addEvent({
delay: 50,
callback: () => {
swayTime += 0.03;
bushes.forEach(bush => {
// Base sway at bottom
const baseSway = Math.sin(swayTime + bush.swayOffset) * 2;
// Update each circle with varying sway based on height
bush.bushGraphics.forEach(item => {
const heightRatio = Math.abs(item.offsetY) / 60; // 0 at bottom, 1 at top
const sway = baseSway * (1 + heightRatio * 2); // Top moves 3x more than bottom
item.graphics.clear();
item.graphics.fillStyle(0x228B22, 1);
item.graphics.fillCircle(item.offsetX + sway, item.offsetY, item.radius);
});
});
},
loop: true
});
}
function createGrass(scene, y) {
const grass = scene.add.graphics();
grass.fillStyle(0x32CD32, 1);
grass.fillRect(0, y, scene.cameras.main.width, scene.cameras.main.height - y);
// Add grass texture details - positioned lower in the grass area
grass.lineStyle(2, 0x228B22, 0.5);
const grassHeight = scene.cameras.main.height - y;
for (let i = 0; i < 100; i++) {
const x = Math.random() * scene.cameras.main.width;
// Position grass stalks in lower 60% of grass area
const grassY = y + grassHeight * 0.4 + Math.random() * (grassHeight * 0.6);
grass.lineBetween(x, grassY, x + Math.random() * 10 - 5, grassY - 10 - Math.random() * 10);
}
}
// ============================================================================
// CHARACTER CREATION
// ============================================================================
function createCharacter(scene) {
const centerX = scene.cameras.main.width / 2;
const characterHeight = scene.cameras.main.height * 0.5;
// Character proportions - shorter torso
const torsoHeight = characterHeight * 0.3; // Reduced from 0.5 to 0.3
const headSize = characterHeight * 0.075; // Half the previous size
const armLength = characterHeight * 0.35;
const legLength = characterHeight * 0.45; // Increased to be more visible
// Create character container
character.container = scene.add.container(centerX, characterGroundY);
// Create legs (draw first so they're behind body)
character.leftLeg = createLeg(scene, -15, -legLength, legLength, true);
character.rightLeg = createLeg(scene, 15, -legLength, legLength, false);
// Create torso
character.torso = scene.add.graphics();
character.torso.fillStyle(0x808080, 1); // Heather gray hoodie
character.torso.fillRoundedRect(-40, -legLength - torsoHeight, 80, torsoHeight, 10);
// Hoodie details - two drawstrings on each side of neck
character.torso.lineStyle(2, 0x505050, 1);
// Left drawstring
character.torso.lineBetween(-8, -legLength - torsoHeight + 5, -8, -legLength - torsoHeight + 20);
// Right drawstring
character.torso.lineBetween(8, -legLength - torsoHeight + 5, 8, -legLength - torsoHeight + 20);
// Small knots at the end
character.torso.fillStyle(0x505050, 1);
character.torso.fillCircle(-8, -legLength - torsoHeight + 20, 3);
character.torso.fillCircle(8, -legLength - torsoHeight + 20, 3);
// Create head
character.head = scene.add.graphics();
character.head.fillStyle(0xFFDBAC, 1); // Skin tone
character.head.fillCircle(0, -legLength - torsoHeight - headSize, headSize);
// Placeholder face (simple features for now)
character.face = scene.add.graphics();
character.face.fillStyle(0x000000, 1);
// Eyes
character.face.fillCircle(-8, -legLength - torsoHeight - headSize, 3);
character.face.fillCircle(8, -legLength - torsoHeight - headSize, 3);
// Smile
character.face.lineStyle(2, 0x000000, 1);
character.face.arc(0, -legLength - torsoHeight - headSize + 5, 10, 0, Math.PI, false);
character.face.strokePath();
// Create arms (draw last so they're in front)
// Arms attach at shoulders (top of torso)
character.leftArm = createArm(scene, -40, -legLength - torsoHeight + 10, armLength, true);
character.rightArm = createArm(scene, 40, -legLength - torsoHeight + 10, armLength, false);
// Add all parts to container
character.container.add([
character.leftLeg.container,
character.rightLeg.container,
character.torso,
character.leftArm.container,
character.rightArm.container,
character.head,
character.face
]);
// Store initial position for reset
character.initialY = characterGroundY;
character.velocity = 0;
}
function createArm(scene, x, y, length, isLeft) {
const arm = {
container: scene.add.container(x, y),
angle: 0,
targetAngle: 0,
isMovingUp: false,
velocity: 0, // For ragdoll physics
isLeft: isLeft
};
// Upper arm (hoodie sleeve)
const upperArm = scene.add.graphics();
upperArm.fillStyle(0x808080, 1);
upperArm.fillRoundedRect(-8, 0, 16, length * 0.6, 5);
// Lower arm (skin)
const lowerArm = scene.add.graphics();
lowerArm.fillStyle(0xFFDBAC, 1);
lowerArm.fillRoundedRect(-7, length * 0.6, 14, length * 0.4, 5);
// Hand
const hand = scene.add.graphics();
hand.fillStyle(0xFFDBAC, 1);
hand.fillCircle(0, length, 10);
arm.container.add([upperArm, lowerArm, hand]);
arm.graphics = { upperArm, lowerArm, hand };
return arm;
}
function createLeg(scene, x, y, length, isLeft) {
const leg = {
container: scene.add.container(x, y),
angle: 0,
targetAngle: 0,
isSpreading: false
};
// Upper leg (jeans)
const upperLeg = scene.add.graphics();
upperLeg.fillStyle(0x4169E1, 1); // Blue jeans
upperLeg.fillRoundedRect(-10, 0, 20, length * 0.55, 5);
// Lower leg (jeans)
const lowerLeg = scene.add.graphics();
lowerLeg.fillStyle(0x4169E1, 1);
lowerLeg.fillRoundedRect(-9, length * 0.55, 18, length * 0.45, 5);
// Shoe (white high-top)
const shoe = scene.add.graphics();
shoe.fillStyle(0xFFFFFF, 1);
shoe.fillRoundedRect(-12, length, 24, 15, 3);
shoe.lineStyle(2, 0xCCCCCC, 1);
shoe.strokeRoundedRect(-12, length, 24, 15, 3);
leg.container.add([upperLeg, lowerLeg, shoe]);
leg.graphics = { upperLeg, lowerLeg, shoe };
return leg;
}
// ============================================================================
// TARGET MARKERS
// ============================================================================
function createTargetMarkers(scene) {
const centerX = scene.cameras.main.width / 2;
const markerWidth = 39; // 30% wider (30 * 1.3 = 39)
const markerDistance = 84; // 40% further apart (60 * 1.4 = 84)
// Left marker - positioned at character's foot level
targetMarkers.left = scene.add.graphics();
targetMarkers.left.lineStyle(3, 0xFFFFFF, 0.8);
targetMarkers.left.strokeRect(-markerWidth / 2, -5, markerWidth, 10);
targetMarkers.left.setPosition(centerX - markerDistance, characterGroundY + 5);
targetMarkers.leftX = centerX - markerDistance;
// Right marker - positioned at character's foot level
targetMarkers.right = scene.add.graphics();
targetMarkers.right.lineStyle(3, 0xFFFFFF, 0.8);
targetMarkers.right.strokeRect(-markerWidth / 2, -5, markerWidth, 10);
targetMarkers.right.setPosition(centerX + markerDistance, characterGroundY + 5);
targetMarkers.rightX = centerX + markerDistance;
targetMarkers.tolerance = markerWidth * 0.1; // 10% tolerance (more forgiving)
targetMarkers.maxOvershoot = 35; // Absolute pixel distance from marker before splits
targetMarkers.markerWidth = markerWidth;
}
// ============================================================================
// INPUT HANDLING
// ============================================================================
function setupInput(scene) {
keys.W = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
keys.Q = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q);
keys.E = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.E);
keys.Z = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);
keys.X = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);
keys.SPACE = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
keys.H = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.H);
keys.ESC = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ESC);
}
function handleInput(delta) {
// Helper function to detect if a key was just pressed (not held)
const wasJustPressed = (key) => {
const keyName = key;
const isDown = keys[keyName].isDown;
const wasDown = previousKeyStates[keyName] || false;
return isDown && !wasDown;
};
// Update previous key states at end of function
const updatePreviousStates = () => {
previousKeyStates.W = keys.W.isDown;
previousKeyStates.Q = keys.Q.isDown;
previousKeyStates.E = keys.E.isDown;
previousKeyStates.Z = keys.Z.isDown;
previousKeyStates.X = keys.X.isDown;
previousKeyStates.SPACE = keys.SPACE.isDown;
};
// Reset from waiting_for_reset or splits state with SPACEBAR only
if (gameState === 'waiting_for_reset' || gameState === 'splits') {
if (wasJustPressed('SPACE')) {
resetCharacter();
updatePreviousStates();
return;
}
}
// Start game on first key press
if (gameState === 'idle') {
if (keys.W.isDown || keys.Q.isDown || keys.E.isDown || keys.Z.isDown || keys.X.isDown) {
startJump();
}
}
if (gameState === 'jumping') {
// Jump control (W key) - starts immediately, continues while held
if (character.container.y === character.initialY && character.velocity === 0) {
// On ground - check if W was just pressed to start jump
if (keys.W.isDown && jumpData.wKeyPressTime === 0) {
// Start jump immediately
jumpData.wKeyPressTime = Date.now();
character.velocity = JUMP_VELOCITY * 0.3; // Initial 30% velocity
jumpData.jumpStartTime = Date.now();
}
} else if (character.container.y < character.initialY) {
// In air - apply continued upward force while W is held (if not released)
if (keys.W.isDown && jumpData.wKeyPressTime > 0) {
const holdDuration = Date.now() - jumpData.wKeyPressTime;
// Apply additional upward velocity while W is held (up to 300ms for 60% of original max height)
if (holdDuration < 300) {
// Add upward force while held - shorter duration limits max height to 60%
character.velocity -= 50; // Strong upward boost per frame
}
} else if (jumpData.wKeyPressTime > 0) {
// W was released - mark it so we don't accept W input until reset
jumpData.wKeyPressTime = -1; // -1 means "released, awaiting reset"
}
}
// Left arm (Q key)
if (keys.Q.isDown) {
character.leftArm.isMovingUp = true;
character.leftArm.targetAngle = MAX_ARM_ANGLE;
jumpData.limbsMoved.leftArm = true;
} else {
character.leftArm.isMovingUp = false;
}
// Right arm (E key)
if (keys.E.isDown) {
character.rightArm.isMovingUp = true;
character.rightArm.targetAngle = MAX_ARM_ANGLE;
jumpData.limbsMoved.rightArm = true;
} else {
character.rightArm.isMovingUp = false;
}
// Left leg (Z key) - only works in air - spreads outward to the left
if (keys.Z.isDown && character.container.y < character.initialY) {
character.leftLeg.isSpreading = true;
character.leftLeg.targetAngle = MAX_LEG_ANGLE; // Positive angle spreads left
jumpData.limbsMoved.leftLeg = true;
} else {
character.leftLeg.isSpreading = false;
character.leftLeg.targetAngle = 0;
}
// Right leg (X key) - only works in air - spreads outward to the right
if (keys.X.isDown && character.container.y < character.initialY) {
character.rightLeg.isSpreading = true;
character.rightLeg.targetAngle = -MAX_LEG_ANGLE; // Negative angle spreads right
jumpData.limbsMoved.rightLeg = true;
} else {
character.rightLeg.isSpreading = false;
character.rightLeg.targetAngle = 0;
}
}
// Always update previous key states at end
updatePreviousStates();
}
// ============================================================================
// CHARACTER PHYSICS & ANIMATION
// ============================================================================
function updateSplitsAnimation(delta) {
// Animate legs spreading to 180 degrees from wherever they currently are
const targetSplits = 90; // Each leg goes 90 degrees (total 180)
const splitsSpeed = 2.0; // Fast animation
// Spread legs outward from current position to 90 degrees
// Left leg spreads outward (positive direction)
if (character.leftLeg.angle < targetSplits) {
character.leftLeg.angle += splitsSpeed * (delta / 16);
if (character.leftLeg.angle > targetSplits) {
character.leftLeg.angle = targetSplits;
}
}
// Right leg spreads outward (negative direction)
if (character.rightLeg.angle > -targetSplits) {
character.rightLeg.angle -= splitsSpeed * (delta / 16);
if (character.rightLeg.angle < -targetSplits) {
character.rightLeg.angle = -targetSplits;
}
}
// Lower character all the way down - torso should be ON the ground level (full splits)
const targetY = characterGroundY + 150; // Much lower - full splits on ground
if (character.container.y < targetY) {
character.container.y += 300 * (delta / 1000); // Descend faster
if (character.container.y > targetY) {
character.container.y = targetY;
}
}
// Update leg rotations
character.leftLeg.container.setRotation(Phaser.Math.DegToRad(character.leftLeg.angle));
character.rightLeg.container.setRotation(Phaser.Math.DegToRad(character.rightLeg.angle));
// Once fully in splits and on ground, score and wait for reset
if (character.leftLeg.angle >= targetSplits &&
character.rightLeg.angle <= -targetSplits &&
character.container.y >= targetY) {
// Score the splits jump with all 1's (only do this once)
if (gameState === 'splits') {
const breakdown = calculateScore();
lastJumpBreakdown = breakdown;
const totalJumpScore = breakdown.total;
jumpScores.push(totalJumpScore);
totalScore += totalJumpScore;
updateScoreDisplay();
updateBreakdownDisplay(breakdown);
// Now stay in splits, waiting for user to reset
gameState = 'waiting_for_reset';
// Show regular jump complete message in black
messageText.setText('Jump Complete\n\nPress Space to Reset');
messageText.setVisible(true);
// Keep OUCH visible at the top
}
}
}
function updateCharacterPhysics(delta) {
// Apply gravity to character
if (character.velocity !== 0 || character.container.y < character.initialY) {
character.velocity += 2000 * (delta / 1000);
character.container.y += character.velocity * (delta / 1000);
// Check landing
if (character.container.y >= character.initialY) {
character.container.y = character.initialY;
character.velocity = 0;
if (gameState === 'jumping') {
// Freeze arms in their current position
character.leftArm.velocity = 0;
character.rightArm.velocity = 0;
character.leftArm.isMovingUp = false;
character.rightArm.isMovingUp = false;
landJump();
}
// Splits scoring now happens in updateSplitsAnimation when animation completes
}
}
}
function updateAnimations(delta) {
// Animate arms
updateArmRotation(character.leftArm, delta);
updateArmRotation(character.rightArm, delta);
// Animate legs
updateLegRotation(character.leftLeg, delta);
updateLegRotation(character.rightLeg, delta);
}
function updateArmRotation(arm, delta) {
// Freeze arms in landing and waiting_for_reset states
if (gameState === 'landing' || gameState === 'waiting_for_reset') {
arm.container.setRotation(Phaser.Math.DegToRad(arm.angle));
return;
}
// Arms swing outward: left arm goes counter-clockwise (negative), right arm goes clockwise (positive)
const direction = arm.isLeft ? 1 : -1; // Direction for rotation
if (arm.isMovingUp) {
// Swing arm up with velocity
arm.velocity += ARM_ROTATION_SPEED * (delta / 16);
} else {
// Ragdoll physics - gravity pulls arm down
arm.velocity -= ARM_FALL_SPEED * (delta / 16);
}
// Apply velocity to angle
arm.angle += arm.velocity * direction;
// Left arm swings from 0 to +200 (counter-clockwise, outward and up)
// Right arm swings from 0 to -200 (clockwise, outward and up)
if (arm.isLeft) {
if (arm.angle > MAX_ARM_ANGLE) {
arm.angle = MAX_ARM_ANGLE;
arm.velocity = 0;
}
if (arm.angle < 0) {
arm.angle = 0;
arm.velocity = 0;
}
} else {
if (arm.angle < -MAX_ARM_ANGLE) {
arm.angle = -MAX_ARM_ANGLE;
arm.velocity = 0;
}
if (arm.angle > 0) {
arm.angle = 0;
arm.velocity = 0;
}
}
arm.container.setRotation(Phaser.Math.DegToRad(arm.angle));
}
function updateLegRotation(leg, delta) {
// Freeze legs in landing position during waiting_for_reset state
if (gameState === 'waiting_for_reset') {
// Don't update leg angle, keep it frozen
leg.container.setRotation(Phaser.Math.DegToRad(leg.angle));
return;
}
// During splits animation, let updateSplitsAnimation handle leg movement
if (gameState === 'splits') {
// Don't interfere with splits animation
return;
}
// Only allow leg movement if character is in the air
if (character.container.y < character.initialY) {
if (leg.isSpreading) {
// Spread leg
const diff = leg.targetAngle - leg.angle;
leg.angle += Math.sign(diff) * LEG_ROTATION_SPEED * (delta / 16);
if (Math.abs(leg.angle - leg.targetAngle) < 0.5) {
leg.angle = leg.targetAngle;
}
} else {
// Return to center
leg.angle -= Math.sign(leg.angle) * LEG_RETURN_SPEED * (delta / 16);
if (Math.abs(leg.angle) < 0.5) {
leg.angle = 0;
}
}
} else {
// On ground - force legs to be straight (except in waiting_for_reset)
leg.angle = 0;
}
leg.container.setRotation(Phaser.Math.DegToRad(leg.angle));
}
// ============================================================================
// JUMP TRACKING & SCORING
// ============================================================================
function startJump() {
gameState = 'jumping';
currentJumpNumber++;
// Clear last score immediately when starting a new jump
document.getElementById('lastScore').textContent = '-';
document.getElementById('lastStars').textContent = '';
// Reset jump data
jumpData = {
isJumping: true,
jumpStartTime: Date.now(),
wKeyPressTime: 0,
jumpChargeTime: 0,
maxHeight: 0,
currentHeight: 0,
leftArmMaxAngle: 0,
rightArmMaxAngle: 0,
leftLegMaxSpread: 0,
rightLegMaxSpread: 0,
leftArmPeakedInAir: false,
rightArmPeakedInAir: false,
legsSpreading: false,
limbsMoved: { leftArm: false, rightArm: false, leftLeg: false, rightLeg: false },
didSplits: false
};
updateScoreDisplay();
}
function trackJumpData() {
// Track height
jumpData.currentHeight = character.initialY - character.container.y;
if (jumpData.currentHeight > jumpData.maxHeight) {
jumpData.maxHeight = jumpData.currentHeight;
}
// Track arm angles
const leftArmAngle = Math.abs(character.leftArm.angle);
const rightArmAngle = Math.abs(character.rightArm.angle);
if (leftArmAngle > jumpData.leftArmMaxAngle) {
jumpData.leftArmMaxAngle = leftArmAngle;
}
if (rightArmAngle > jumpData.rightArmMaxAngle) {
jumpData.rightArmMaxAngle = rightArmAngle;
}
// Check if arms peaked while in air
if (character.container.y < character.initialY) {
if (leftArmAngle > 80) jumpData.leftArmPeakedInAir = true;
if (rightArmAngle > 80) jumpData.rightArmPeakedInAir = true;
}
// Track leg spread
const leftLegSpread = Math.abs(character.leftLeg.angle);