Fix goalie collision on failed saves by adding 500ms cooldown

When goalies fail to save shots, they now have a 500ms collision cooldown
to prevent the puck from bouncing off them repeatedly. This allows the
puck to continue into the goal after a failed save.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Pierre Wessman 2025-09-19 13:19:01 +02:00
parent 15c24608d4
commit 66c949e47d

View File

@ -12,6 +12,9 @@ class Puck {
this.bounceCount = 0; this.bounceCount = 0;
this.trail = []; this.trail = [];
this.maxTrailLength = 10; this.maxTrailLength = 10;
// Collision cooldown tracking
this.goalieCollisionCooldowns = new Map(); // Maps player ID to cooldown end time
} }
update(deltaTime, gameState, players) { update(deltaTime, gameState, players) {
@ -91,6 +94,24 @@ class Puck {
} }
} }
isGoalieOnCooldown(goalie) {
const playerId = goalie.id || `${goalie.team}-${goalie.role}`;
const cooldownEnd = this.goalieCollisionCooldowns.get(playerId);
if (cooldownEnd && Date.now() < cooldownEnd) {
return true;
}
// Clean up expired cooldowns
if (cooldownEnd && Date.now() >= cooldownEnd) {
this.goalieCollisionCooldowns.delete(playerId);
}
return false;
}
setGoalieCooldown(goalie, durationMs = 500) {
const playerId = goalie.id || `${goalie.team}-${goalie.role}`;
this.goalieCollisionCooldowns.set(playerId, Date.now() + durationMs);
}
checkBoardCollisions(gameState) { checkBoardCollisions(gameState) {
const rink = gameState.rink; const rink = gameState.rink;
let collision = false; let collision = false;
@ -302,6 +323,11 @@ class Puck {
let closestDistance = Infinity; let closestDistance = Infinity;
players.forEach(player => { players.forEach(player => {
// Skip goalies who are on collision cooldown
if (player.role === 'G' && this.isGoalieOnCooldown(player)) {
return;
}
const distance = this.position.distance(player.position); const distance = this.position.distance(player.position);
const collisionDistance = this.radius + player.radius; const collisionDistance = this.radius + player.radius;
@ -392,10 +418,13 @@ class Puck {
difficulty: difficulty difficulty: difficulty
}); });
} else { } else {
console.log("Failed save") console.log("Failed save - setting collision cooldown")
// Failed save - puck continues with reduced velocity (already bounced from collision) // Failed save - puck continues with reduced velocity (already bounced from collision)
// The collision physics have already been applied, so the puck will continue toward the goal // The collision physics have already been applied, so the puck will continue toward the goal
this.velocity = this.velocity.multiply(0.8); // Slightly reduce speed but let it continue this.velocity = this.velocity.multiply(0.8); // Slightly reduce speed but let it continue
// Set collision cooldown to prevent immediate re-collision with goalie
this.setGoalieCooldown(goalie, 500); // 500ms cooldown
} }
} }
@ -417,6 +446,9 @@ class Puck {
this.lastTeamTouch = null; this.lastTeamTouch = null;
this.bounceCount = 0; this.bounceCount = 0;
this.trail = []; this.trail = [];
// Clear all goalie collision cooldowns
this.goalieCollisionCooldowns.clear();
} }
faceoffDrop(winningTeam, location, participants) { faceoffDrop(winningTeam, location, participants) {