Compare commits
2 Commits
d2fb60df8d
...
230fc1bc0c
| Author | SHA1 | Date | |
|---|---|---|---|
| 230fc1bc0c | |||
| 3d15dc716b |
@ -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);
|
||||
@ -69,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();
|
||||
}
|
||||
|
||||
@ -126,30 +144,27 @@ export class Player extends Phaser.GameObjects.Container {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update player movement each frame
|
||||
* Handle goal event
|
||||
*/
|
||||
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);
|
||||
private onGoal(_data: { team: string; goal: string }) {
|
||||
// Gradually decelerate player after goal
|
||||
const decelerate = () => {
|
||||
this.speedMultiplier *= 0.9;
|
||||
|
||||
// If close enough to target, stop
|
||||
if (distance < 0.1) {
|
||||
this.body.setVelocity(0, 0);
|
||||
return;
|
||||
if (this.speedMultiplier > 0.01) {
|
||||
this.scene.time.delayedCall(50, decelerate);
|
||||
} else {
|
||||
this.speedMultiplier = 0;
|
||||
}
|
||||
};
|
||||
|
||||
decelerate();
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// 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 player movement each frame (physics-based)
|
||||
*/
|
||||
public update(_delta: number) {
|
||||
// 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;
|
||||
@ -157,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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ export class GameScene extends Phaser.Scene {
|
||||
'home',
|
||||
'C',
|
||||
-10,
|
||||
0,
|
||||
-10,
|
||||
{ speed: 80, skill: 75 }
|
||||
);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user