Spaces:
Paused
Paused
asemxin commited on
Commit ·
0c4f8d5
1
Parent(s): 68a9f2f
feat: reduce mobile control sensitivity for better touch experience
Browse files- app/components/GameCanvas.tsx +2 -2
- lib/constants.ts +2 -0
- lib/engine/Game.ts +6 -1
- lib/engine/Physics.ts +21 -4
app/components/GameCanvas.tsx
CHANGED
|
@@ -87,7 +87,7 @@ export default function GameCanvas({ onGameOver, startLevel = 0 }: GameCanvasPro
|
|
| 87 |
useEffect(() => {
|
| 88 |
if (!canvasRef.current) return;
|
| 89 |
|
| 90 |
-
const game = new Game(canvasRef.current, startLevel);
|
| 91 |
gameRef.current = game;
|
| 92 |
|
| 93 |
game.setOnGameOver(handleGameOver);
|
|
@@ -96,7 +96,7 @@ export default function GameCanvas({ onGameOver, startLevel = 0 }: GameCanvasPro
|
|
| 96 |
return () => {
|
| 97 |
game.stop();
|
| 98 |
};
|
| 99 |
-
}, [handleGameOver, startLevel]);
|
| 100 |
|
| 101 |
// Calculate scale for CSS transform
|
| 102 |
const scale = dimensions.width / GAME_CONFIG.CANVAS_WIDTH;
|
|
|
|
| 87 |
useEffect(() => {
|
| 88 |
if (!canvasRef.current) return;
|
| 89 |
|
| 90 |
+
const game = new Game(canvasRef.current, startLevel, isMobile);
|
| 91 |
gameRef.current = game;
|
| 92 |
|
| 93 |
game.setOnGameOver(handleGameOver);
|
|
|
|
| 96 |
return () => {
|
| 97 |
game.stop();
|
| 98 |
};
|
| 99 |
+
}, [handleGameOver, startLevel, isMobile]);
|
| 100 |
|
| 101 |
// Calculate scale for CSS transform
|
| 102 |
const scale = dimensions.width / GAME_CONFIG.CANVAS_WIDTH;
|
lib/constants.ts
CHANGED
|
@@ -14,7 +14,9 @@ export const GAME_CONFIG = {
|
|
| 14 |
PLAYER_WIDTH: 32,
|
| 15 |
PLAYER_HEIGHT: 48,
|
| 16 |
PLAYER_SPEED: 5,
|
|
|
|
| 17 |
PLAYER_JUMP_FORCE: -14,
|
|
|
|
| 18 |
|
| 19 |
// Tile size
|
| 20 |
TILE_SIZE: 32,
|
|
|
|
| 14 |
PLAYER_WIDTH: 32,
|
| 15 |
PLAYER_HEIGHT: 48,
|
| 16 |
PLAYER_SPEED: 5,
|
| 17 |
+
MOBILE_PLAYER_SPEED: 3.5, // Reduced speed for mobile touch controls
|
| 18 |
PLAYER_JUMP_FORCE: -14,
|
| 19 |
+
MOBILE_JUMP_FORCE: -12, // Slightly lower jump for mobile
|
| 20 |
|
| 21 |
// Tile size
|
| 22 |
TILE_SIZE: 32,
|
lib/engine/Game.ts
CHANGED
|
@@ -29,8 +29,9 @@ export class Game {
|
|
| 29 |
private callbacks: GameCallbacks = {};
|
| 30 |
private totalScore: number = 0;
|
| 31 |
private currentTheme: LevelTheme = 'overworld';
|
|
|
|
| 32 |
|
| 33 |
-
constructor(canvas: HTMLCanvasElement, startLevel: number = 0) {
|
| 34 |
this.canvas = canvas;
|
| 35 |
this.ctx = canvas.getContext('2d')!;
|
| 36 |
this.renderer = new Renderer(this.ctx);
|
|
@@ -38,6 +39,10 @@ export class Game {
|
|
| 38 |
this.collision = new Collision();
|
| 39 |
this.levelManager = new LevelManager();
|
| 40 |
this.keys = { left: false, right: false, jump: false };
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
// Set starting level
|
| 43 |
if (startLevel > 0) {
|
|
|
|
| 29 |
private callbacks: GameCallbacks = {};
|
| 30 |
private totalScore: number = 0;
|
| 31 |
private currentTheme: LevelTheme = 'overworld';
|
| 32 |
+
private isMobile: boolean = false;
|
| 33 |
|
| 34 |
+
constructor(canvas: HTMLCanvasElement, startLevel: number = 0, isMobile: boolean = false) {
|
| 35 |
this.canvas = canvas;
|
| 36 |
this.ctx = canvas.getContext('2d')!;
|
| 37 |
this.renderer = new Renderer(this.ctx);
|
|
|
|
| 39 |
this.collision = new Collision();
|
| 40 |
this.levelManager = new LevelManager();
|
| 41 |
this.keys = { left: false, right: false, jump: false };
|
| 42 |
+
this.isMobile = isMobile;
|
| 43 |
+
|
| 44 |
+
// Set mobile mode for physics
|
| 45 |
+
this.physics.setMobileMode(isMobile);
|
| 46 |
|
| 47 |
// Set starting level
|
| 48 |
if (startLevel > 0) {
|
lib/engine/Physics.ts
CHANGED
|
@@ -3,9 +3,23 @@
|
|
| 3 |
import { PlayerState, Enemy, KeyState } from './types';
|
| 4 |
import { GAME_CONFIG } from '../constants';
|
| 5 |
|
| 6 |
-
const { GRAVITY, FRICTION, MAX_FALL_SPEED, PLAYER_SPEED, PLAYER_JUMP_FORCE } = GAME_CONFIG;
|
| 7 |
|
| 8 |
export class Physics {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
applyGravity(entity: { vy: number; isFalling?: boolean }): void {
|
| 10 |
entity.vy += GRAVITY;
|
| 11 |
if (entity.vy > MAX_FALL_SPEED) {
|
|
@@ -21,12 +35,15 @@ export class Physics {
|
|
| 21 |
}
|
| 22 |
|
| 23 |
updatePlayerMovement(player: PlayerState, keys: KeyState): void {
|
|
|
|
|
|
|
|
|
|
| 24 |
// Horizontal movement
|
| 25 |
if (keys.left) {
|
| 26 |
-
player.vx = -
|
| 27 |
player.facingRight = false;
|
| 28 |
} else if (keys.right) {
|
| 29 |
-
player.vx =
|
| 30 |
player.facingRight = true;
|
| 31 |
} else {
|
| 32 |
this.applyFriction(player);
|
|
@@ -34,7 +51,7 @@ export class Physics {
|
|
| 34 |
|
| 35 |
// Jump
|
| 36 |
if (keys.jump && !player.isJumping && !player.isFalling) {
|
| 37 |
-
player.vy =
|
| 38 |
player.isJumping = true;
|
| 39 |
}
|
| 40 |
|
|
|
|
| 3 |
import { PlayerState, Enemy, KeyState } from './types';
|
| 4 |
import { GAME_CONFIG } from '../constants';
|
| 5 |
|
| 6 |
+
const { GRAVITY, FRICTION, MAX_FALL_SPEED, PLAYER_SPEED, PLAYER_JUMP_FORCE, MOBILE_PLAYER_SPEED, MOBILE_JUMP_FORCE } = GAME_CONFIG;
|
| 7 |
|
| 8 |
export class Physics {
|
| 9 |
+
private isMobile: boolean = false;
|
| 10 |
+
|
| 11 |
+
setMobileMode(isMobile: boolean): void {
|
| 12 |
+
this.isMobile = isMobile;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
private getPlayerSpeed(): number {
|
| 16 |
+
return this.isMobile ? MOBILE_PLAYER_SPEED : PLAYER_SPEED;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
private getJumpForce(): number {
|
| 20 |
+
return this.isMobile ? MOBILE_JUMP_FORCE : PLAYER_JUMP_FORCE;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
applyGravity(entity: { vy: number; isFalling?: boolean }): void {
|
| 24 |
entity.vy += GRAVITY;
|
| 25 |
if (entity.vy > MAX_FALL_SPEED) {
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
updatePlayerMovement(player: PlayerState, keys: KeyState): void {
|
| 38 |
+
const speed = this.getPlayerSpeed();
|
| 39 |
+
const jumpForce = this.getJumpForce();
|
| 40 |
+
|
| 41 |
// Horizontal movement
|
| 42 |
if (keys.left) {
|
| 43 |
+
player.vx = -speed;
|
| 44 |
player.facingRight = false;
|
| 45 |
} else if (keys.right) {
|
| 46 |
+
player.vx = speed;
|
| 47 |
player.facingRight = true;
|
| 48 |
} else {
|
| 49 |
this.applyFriction(player);
|
|
|
|
| 51 |
|
| 52 |
// Jump
|
| 53 |
if (keys.jump && !player.isJumping && !player.isFalling) {
|
| 54 |
+
player.vy = jumpForce;
|
| 55 |
player.isJumping = true;
|
| 56 |
}
|
| 57 |
|