player physics

This commit is contained in:
Pierre Wessman 2025-10-01 14:57:54 +00:00
parent 3d15dc716b
commit 230fc1bc0c

View File

@ -73,6 +73,20 @@ export class Player extends Phaser.GameObjects.Container {
this.body.setOffset(-radius, -radius); // Center the body on the container
this.body.setCollideWorldBounds(true);
// Set up physics-based movement
// Max velocity based on speed attribute (e.g., speed 80 -> 8 m/s = 160 px/s)
const maxSpeed = (this.attributes.speed / 10) * SCALE;
this.body.setMaxVelocity(maxSpeed, maxSpeed);
// Drag simulates ice friction (higher = more friction)
// Lower drag = more gliding (realistic for ice)
this.body.setDrag(100, 100);
// Acceleration determines how quickly player reaches max speed
// Higher skill = better acceleration control
const acceleration = (this.attributes.skill / 10) * SCALE * 10;
this.body.setAcceleration(0, 0); // Will be set each frame based on target
this.createSprite();
}
@ -148,31 +162,9 @@ export class Player extends Phaser.GameObjects.Container {
}
/**
* Update player movement each frame
* Update player movement each frame (physics-based)
*/
public update(_delta: number) {
// Calculate distance to target
const dx = this.targetX - this.gameX;
const dy = this.targetY - this.gameY;
const distance = Math.sqrt(dx * dx + dy * dy);
// If close enough to target, stop
if (distance < 0.1) {
this.body.setVelocity(0, 0);
return;
}
// Calculate velocity based on speed attribute
// speed attribute (0-100) maps to actual m/s (e.g., 80 -> 8 m/s)
const baseSpeed = (this.attributes.speed / 10) * SCALE; // Convert to pixels/s
const speed = baseSpeed * this.speedMultiplier; // Apply speed multiplier
// Normalize direction and apply speed
const dirX = dx / distance;
const dirY = dy / distance;
this.body.setVelocity(dirX * speed, -dirY * speed); // Negative Y because screen coords
// Update game position based on physics body center
const centerX = (this.scene.game.config.width as number) / 2;
const centerY = (this.scene.game.config.height as number) / 2;
@ -180,5 +172,28 @@ export class Player extends Phaser.GameObjects.Container {
const bodyY = this.body.y + this.body.height / 2;
this.gameX = (bodyX - centerX) / SCALE;
this.gameY = -(bodyY - centerY) / SCALE;
// Calculate distance to target
const dx = this.targetX - this.gameX;
const dy = this.targetY - this.gameY;
const distance = Math.sqrt(dx * dx + dy * dy);
// If close enough to target, reduce acceleration (coast)
if (distance < 1.0) {
this.body.setAcceleration(0, 0);
return;
}
// Calculate acceleration direction toward target
const dirX = dx / distance;
const dirY = dy / distance;
// Acceleration magnitude based on skill attribute and speed multiplier
// Higher skill = better acceleration (more responsive)
const baseAcceleration = (this.attributes.skill / 10) * SCALE * 10;
const acceleration = baseAcceleration * this.speedMultiplier;
// Apply acceleration toward target (negative Y for screen coords)
this.body.setAcceleration(dirX * acceleration, -dirY * acceleration);
}
}