This commit is contained in:
Pierre Wessman 2025-10-01 14:54:58 +00:00
parent d2fb60df8d
commit 3d15dc716b
2 changed files with 25 additions and 2 deletions

View File

@ -31,6 +31,7 @@ export class Player extends Phaser.GameObjects.Container {
// Player state
public state: PlayerState;
private speedMultiplier: number = 1.0;
constructor(
scene: Phaser.Scene,
@ -57,6 +58,9 @@ export class Player extends Phaser.GameObjects.Container {
this.attributes = attributes;
this.state = 'defensive';
// Listen for goal events
this.scene.events.on('goal', this.onGoal, this);
// Add to scene
scene.add.existing(this);
scene.physics.add.existing(this);
@ -125,6 +129,24 @@ export class Player extends Phaser.GameObjects.Container {
this.targetY = targetY;
}
/**
* Handle goal event
*/
private onGoal(_data: { team: string; goal: string }) {
// Gradually decelerate player after goal
const decelerate = () => {
this.speedMultiplier *= 0.9;
if (this.speedMultiplier > 0.01) {
this.scene.time.delayedCall(50, decelerate);
} else {
this.speedMultiplier = 0;
}
};
decelerate();
}
/**
* Update player movement each frame
*/
@ -142,7 +164,8 @@ export class Player extends Phaser.GameObjects.Container {
// Calculate velocity based on speed attribute
// speed attribute (0-100) maps to actual m/s (e.g., 80 -> 8 m/s)
const speed = (this.attributes.speed / 10) * SCALE; // Convert to pixels/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;

View File

@ -42,7 +42,7 @@ export class GameScene extends Phaser.Scene {
'home',
'C',
-10,
0,
-10,
{ speed: 80, skill: 75 }
);