Skip to content

Commit 3221522

Browse files
authored
Merge pull request #60 from MVPFlow/codex/fix-mobile-controls-and-slide-activation
fix: touch-only joystick and slide activation via double-tap jump
2 parents a0cadf1 + ce9c419 commit 3221522

1 file changed

Lines changed: 73 additions & 34 deletions

File tree

‎src/scenes/GameScene.ts‎

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ const MOBILE_JOYSTICK_BASE_SIZE = 140;
7979
const MOBILE_JOYSTICK_THUMB_SIZE = 70;
8080
const MOBILE_JOYSTICK_RADIUS = 48;
8181
const MOBILE_JOYSTICK_DEADZONE = 0.15;
82-
const MOBILE_SLIDE_TRIGGER_DEADZONE = 0.35;
83-
const MOBILE_SLIDE_STABLE_MS = 120;
8482
const MOBILE_JUMP_BUTTON_SIZE = 120;
83+
const MOBILE_JUMP_DOUBLE_TAP_WINDOW_MS = 260;
8584
const MOBILE_CONTROL_MARGIN = 24;
8685
const MOBILE_JUMP_BUTTON_DEPTH = 20;
8786
const MOBILE_JOYSTICK_DEPTH = 18;
@@ -94,8 +93,6 @@ export class GameScene extends Phaser.Scene {
9493
private cursors!: Phaser.Types.Input.Keyboard.CursorKeys;
9594
private keys!: { A: Phaser.Input.Keyboard.Key; D: Phaser.Input.Keyboard.Key; SPACE: Phaser.Input.Keyboard.Key };
9695
private moveDirection = 0;
97-
private joystickStrength = 0;
98-
private lastMoveDirectionChangeTime = 0;
9996
private startTime = 0;
10097
private lastSpawnTime = 0;
10198
private lastFishSpawnTime = 0;
@@ -137,6 +134,7 @@ export class GameScene extends Phaser.Scene {
137134
private currentJumpVelocity = JUMP_VELOCITY;
138135
private lastLeftTapTime = 0;
139136
private lastRightTapTime = 0;
137+
private lastJumpTapTime = 0;
140138
private slideEmitter!: Phaser.GameObjects.Particles.ParticleEmitter;
141139
private baseColor!: Phaser.GameObjects.Rectangle;
142140
private desertBackground!: Phaser.GameObjects.Image;
@@ -268,7 +266,7 @@ export class GameScene extends Phaser.Scene {
268266
this.cursors = this.input.keyboard!.createCursorKeys();
269267
this.keys = this.input.keyboard!.addKeys('A,D,SPACE') as { A: Phaser.Input.Keyboard.Key; D: Phaser.Input.Keyboard.Key; SPACE: Phaser.Input.Keyboard.Key };
270268

271-
const enableMobileControls = this.shouldUseMobileControls(width);
269+
const enableMobileControls = this.shouldUseMobileControls();
272270

273271
this.keyDownHandler = (event: KeyboardEvent) => {
274272
if (event.repeat) {
@@ -284,6 +282,8 @@ export class GameScene extends Phaser.Scene {
284282
this.createHud(width, height);
285283
if (enableMobileControls) {
286284
this.createMobileControls(width, height);
285+
} else {
286+
this.destroyMobileControls();
287287
}
288288

289289
this.statusText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, '', {
@@ -305,6 +305,7 @@ export class GameScene extends Phaser.Scene {
305305
this.currentJumpVelocity = JUMP_VELOCITY;
306306
this.lastLeftTapTime = 0;
307307
this.lastRightTapTime = 0;
308+
this.lastJumpTapTime = 0;
308309
this.slideEmitter.stop();
309310
this.player.clearTint();
310311
this.setFishPowerCount(0);
@@ -741,6 +742,18 @@ export class GameScene extends Phaser.Scene {
741742
this.resizeBackgrounds(gameSize.width, gameSize.height);
742743
this.resizeHud(gameSize.width, gameSize.height);
743744
this.layoutGameOverModal(gameSize.width, gameSize.height);
745+
746+
const shouldEnableMobileControls = this.shouldUseMobileControls();
747+
if (!shouldEnableMobileControls) {
748+
this.destroyMobileControls();
749+
return;
750+
}
751+
752+
if (!this.mobileControlsEnabled) {
753+
this.createMobileControls(gameSize.width, gameSize.height);
754+
return;
755+
}
756+
744757
this.layoutMobileControls(gameSize.width, gameSize.height);
745758
}
746759

@@ -940,9 +953,40 @@ export class GameScene extends Phaser.Scene {
940953
this.fishGroup?.setScale(1);
941954
}
942955

943-
private shouldUseMobileControls(viewportWidth: number) {
944-
const useTouchControls = this.sys.game.device.input.touch === true || viewportWidth < 700;
945-
return useTouchControls;
956+
private shouldUseMobileControls() {
957+
const isTouchDevice = this.sys.game.device.input.touch === true;
958+
const pointer = this.input.manager?.pointers?.[0];
959+
const pointerType = pointer && 'pointerType' in pointer ? (pointer as { pointerType?: string }).pointerType : undefined;
960+
const isTouchPointer = pointerType ? pointerType === 'touch' : true;
961+
return isTouchDevice && isTouchPointer;
962+
}
963+
964+
private destroyMobileControls() {
965+
if (!this.mobileControlsEnabled) {
966+
return;
967+
}
968+
969+
if (this.joystickMoveHandler) {
970+
this.input.off('pointermove', this.joystickMoveHandler);
971+
}
972+
if (this.joystickUpHandler) {
973+
this.input.off('pointerup', this.joystickUpHandler);
974+
}
975+
976+
this.joystickContainer?.destroy(true);
977+
this.joystickContainer = undefined;
978+
this.joystickBase = undefined;
979+
this.joystickThumb = undefined;
980+
this.joystickPointerId = undefined;
981+
982+
this.jumpButton?.destroy(true);
983+
this.jumpButton = undefined;
984+
this.jumpButtonImage = undefined;
985+
this.jumpButtonText = undefined;
986+
this.jumpButtonHitbox = undefined;
987+
988+
this.mobileControlsEnabled = false;
989+
this.lastJumpTapTime = 0;
946990
}
947991

948992
private createMobileControls(viewportWidth: number, viewportHeight: number) {
@@ -1003,7 +1047,7 @@ export class GameScene extends Phaser.Scene {
10031047
}
10041048
this.jumpButton?.setScale(0.96);
10051049
this.jumpButtonImage?.setTint(0xdfe8ff);
1006-
this.tryJump({ fromTouch: true });
1050+
this.handleJumpButtonTap();
10071051
});
10081052
this.jumpButtonHitbox.on('pointerup', () => {
10091053
this.jumpButton?.setScale(1);
@@ -1053,50 +1097,45 @@ export class GameScene extends Phaser.Scene {
10531097
const clampedX = Phaser.Math.Clamp(deltaX, -this.joystickRadius, this.joystickRadius);
10541098
const normalizedX = this.joystickRadius > 0 ? clampedX / this.joystickRadius : 0;
10551099
const nextDirection = Math.abs(normalizedX) < MOBILE_JOYSTICK_DEADZONE ? 0 : normalizedX < 0 ? -1 : 1;
1056-
if (nextDirection !== this.moveDirection) {
1057-
this.lastMoveDirectionChangeTime = this.time.now;
1058-
}
1059-
this.joystickStrength = normalizedX;
10601100
this.joystickThumb.setPosition(clampedX, 0);
10611101
this.moveDirection = nextDirection;
10621102
}
10631103

10641104
private resetJoystick() {
10651105
this.joystickPointerId = undefined;
10661106
this.moveDirection = 0;
1067-
this.joystickStrength = 0;
10681107
this.joystickThumb?.setPosition(0, 0);
10691108
}
10701109

1071-
private tryJump(options: { fromTouch?: boolean } = {}) {
1110+
private handleJumpButtonTap() {
1111+
const now = this.time.now;
1112+
const isDoubleTap =
1113+
this.lastJumpTapTime > 0 && now - this.lastJumpTapTime <= MOBILE_JUMP_DOUBLE_TAP_WINDOW_MS;
1114+
1115+
if (isDoubleTap && this.fishPowerCount === FISH_MAX_POWER && !this.slideActive) {
1116+
this.lastJumpTapTime = 0;
1117+
this.activateSlideMax();
1118+
return;
1119+
}
1120+
1121+
if (this.lastJumpTapTime > 0 && now - this.lastJumpTapTime > MOBILE_JUMP_DOUBLE_TAP_WINDOW_MS) {
1122+
this.lastJumpTapTime = 0;
1123+
}
1124+
1125+
this.lastJumpTapTime = now;
1126+
this.tryJump();
1127+
}
1128+
1129+
private tryJump() {
10721130
const playerBody = this.player.body as Phaser.Physics.Arcade.Body;
10731131
const isGrounded = playerBody.blocked.down || playerBody.touching.down;
10741132
if (!isGrounded) {
10751133
return;
10761134
}
1077-
if (options.fromTouch && this.shouldTriggerTouchSlideMax()) {
1078-
this.activateSlideMax();
1079-
}
10801135
this.player.setVelocityY(this.currentJumpVelocity);
10811136
this.audioManager.playSfx(this, SFX_PLAYER_JUMP);
10821137
}
10831138

1084-
private shouldTriggerTouchSlideMax() {
1085-
if (!this.mobileControlsEnabled) {
1086-
return false;
1087-
}
1088-
if (this.slideActive) {
1089-
return false;
1090-
}
1091-
if (this.fishPowerCount < FISH_MAX_POWER) {
1092-
return false;
1093-
}
1094-
const hasStrongDirection = Math.abs(this.joystickStrength) >= MOBILE_SLIDE_TRIGGER_DEADZONE;
1095-
const hasStableDirection =
1096-
Math.abs(this.moveDirection) === 1 && this.time.now - this.lastMoveDirectionChangeTime >= MOBILE_SLIDE_STABLE_MS;
1097-
return hasStrongDirection || hasStableDirection;
1098-
}
1099-
11001139
private handleDoubleTap(event: KeyboardEvent) {
11011140
const now = this.time.now;
11021141
const isLeft = event.code === 'ArrowLeft' || event.code === 'KeyA';

0 commit comments

Comments
 (0)