bugfix: has puck
This commit is contained in:
parent
ac5b83afee
commit
c0ced95d46
@ -18,6 +18,7 @@ class Puck {
|
||||
this.updatePosition(deltaTime);
|
||||
this.checkBoardCollisions(gameState);
|
||||
this.checkPlayerCollisions(players, gameState);
|
||||
this.checkPuckPossession(players);
|
||||
this.updateTrail();
|
||||
}
|
||||
|
||||
@ -26,6 +27,31 @@ class Puck {
|
||||
this.position = this.position.add(this.velocity.multiply(deltaTime));
|
||||
}
|
||||
|
||||
checkPuckPossession(players) {
|
||||
const puckCarrier = players.find(player => player.state.hasPuck);
|
||||
if (!puckCarrier) return;
|
||||
|
||||
const distance = this.position.distance(puckCarrier.position);
|
||||
const maxPossessionDistance = 25; // Maximum distance to maintain puck possession
|
||||
|
||||
if (distance > maxPossessionDistance) {
|
||||
puckCarrier.state.hasPuck = false;
|
||||
// Puck becomes loose and slightly moves in a random direction
|
||||
if (this.velocity.magnitude() < 10) {
|
||||
const randomDirection = new Vector2(
|
||||
(Math.random() - 0.5) * 2,
|
||||
(Math.random() - 0.5) * 2
|
||||
).normalize();
|
||||
this.velocity = randomDirection.multiply(20);
|
||||
}
|
||||
} else if (distance < 15) {
|
||||
// Keep puck close to player when they have possession
|
||||
const directionToPlayer = puckCarrier.position.subtract(this.position).normalize();
|
||||
this.position = puckCarrier.position.subtract(directionToPlayer.multiply(12));
|
||||
this.velocity = puckCarrier.velocity.multiply(0.8);
|
||||
}
|
||||
}
|
||||
|
||||
updateTrail() {
|
||||
if (this.velocity.magnitude() > 50) {
|
||||
this.trail.unshift({
|
||||
@ -111,11 +137,11 @@ class Puck {
|
||||
});
|
||||
|
||||
if (closestPlayer) {
|
||||
this.handlePlayerCollision(closestPlayer, gameState);
|
||||
this.handlePlayerCollision(closestPlayer, gameState, players);
|
||||
}
|
||||
}
|
||||
|
||||
handlePlayerCollision(player, gameState) {
|
||||
handlePlayerCollision(player, gameState, players) {
|
||||
const distance = this.position.distance(player.position);
|
||||
const minDistance = this.radius + player.radius;
|
||||
|
||||
@ -136,14 +162,14 @@ class Puck {
|
||||
this.velocity = this.velocity.subtract(direction.multiply(impulse * player.mass * this.restitution));
|
||||
|
||||
if (this.velocity.magnitude() < 100 && player.role !== 'G') {
|
||||
this.pickupPuck(player, gameState);
|
||||
this.pickupPuck(player, gameState, players);
|
||||
} else if (player.role === 'G' && this.velocity.magnitude() > 50) {
|
||||
this.handleGoalieSave(player, gameState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pickupPuck(player, gameState) {
|
||||
pickupPuck(player, gameState, players) {
|
||||
if (player.state.hasPuck) return;
|
||||
|
||||
players.forEach(p => p.state.hasPuck = false);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user