Fix pterodactyl duck lane collisions and prevent duplicate score saves
This commit is contained in:
parent
b68f71beb2
commit
4470266234
28
js/game.js
28
js/game.js
@ -62,6 +62,7 @@ function createInitialState() {
|
|||||||
|
|
||||||
let state = createInitialState();
|
let state = createInitialState();
|
||||||
let saveEligible = false;
|
let saveEligible = false;
|
||||||
|
let scoreSavedThisRun = false;
|
||||||
|
|
||||||
window.addEventListener('keydown', (e) => {
|
window.addEventListener('keydown', (e) => {
|
||||||
if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) e.preventDefault();
|
if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) e.preventDefault();
|
||||||
@ -79,6 +80,7 @@ restartBtnEl.addEventListener('click', () => {
|
|||||||
deathScreenEl.classList.add('hidden');
|
deathScreenEl.classList.add('hidden');
|
||||||
saveScoreFormEl.classList.add('hidden');
|
saveScoreFormEl.classList.add('hidden');
|
||||||
saveEligible = false;
|
saveEligible = false;
|
||||||
|
scoreSavedThisRun = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
saveScoreFormEl.addEventListener('submit', async (e) => {
|
saveScoreFormEl.addEventListener('submit', async (e) => {
|
||||||
@ -93,6 +95,7 @@ saveScoreFormEl.addEventListener('submit', async (e) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
saveEligible = false;
|
saveEligible = false;
|
||||||
|
scoreSavedThisRun = true;
|
||||||
saveScoreFormEl.classList.add('hidden');
|
saveScoreFormEl.classList.add('hidden');
|
||||||
await populateLeaderboard();
|
await populateLeaderboard();
|
||||||
});
|
});
|
||||||
@ -174,8 +177,8 @@ async function populateLeaderboard() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const qualifies = scores.length < 10 || state.score > (scores[scores.length - 1]?.score ?? -1);
|
const qualifies = scores.length < 10 || state.score > (scores[scores.length - 1]?.score ?? -1);
|
||||||
saveEligible = qualifies;
|
saveEligible = qualifies && !scoreSavedThisRun;
|
||||||
saveScoreFormEl.classList.toggle('hidden', !qualifies);
|
saveScoreFormEl.classList.toggle('hidden', !saveEligible);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function kill(reason) {
|
async function kill(reason) {
|
||||||
@ -224,16 +227,31 @@ function updateAnts(dt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PTERODACTYL_LANES = [
|
||||||
|
// low lane: standing dino collides, ducking dino should pass underneath
|
||||||
|
{ y: groundY - 80, height: 28 }
|
||||||
|
];
|
||||||
|
|
||||||
|
function getPterodactylHitbox(flyer) {
|
||||||
|
return {
|
||||||
|
x: flyer.x + 6,
|
||||||
|
y: flyer.y + 5,
|
||||||
|
width: flyer.width - 12,
|
||||||
|
height: flyer.height - 10
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function updatePterodactyls(dt) {
|
function updatePterodactyls(dt) {
|
||||||
if (state.score <= 100) return;
|
if (state.score <= 100) return;
|
||||||
state.pteroSpawnTimer -= dt;
|
state.pteroSpawnTimer -= dt;
|
||||||
|
|
||||||
if (state.pteroSpawnTimer <= 0) {
|
if (state.pteroSpawnTimer <= 0) {
|
||||||
|
const lane = PTERODACTYL_LANES[Math.floor(Math.random() * PTERODACTYL_LANES.length)];
|
||||||
state.pterodactyls.push({
|
state.pterodactyls.push({
|
||||||
x: state.cameraX + canvas.width + 80,
|
x: state.cameraX + canvas.width + 80,
|
||||||
y: groundY - (state.player.standingHeight - 24),
|
y: lane.y,
|
||||||
width: 54,
|
width: 54,
|
||||||
height: 26,
|
height: lane.height,
|
||||||
vx: -280
|
vx: -280
|
||||||
});
|
});
|
||||||
state.pteroSpawnTimer = 2.2;
|
state.pteroSpawnTimer = 2.2;
|
||||||
@ -242,7 +260,7 @@ function updatePterodactyls(dt) {
|
|||||||
const p = state.player;
|
const p = state.player;
|
||||||
for (const flyer of state.pterodactyls) {
|
for (const flyer of state.pterodactyls) {
|
||||||
flyer.x += flyer.vx * dt;
|
flyer.x += flyer.vx * dt;
|
||||||
if (intersects(p, flyer)) applyHit('Pterodactyl collision. Duck next time!');
|
if (intersects(p, getPterodactylHitbox(flyer))) applyHit('Pterodactyl collision. Duck next time!');
|
||||||
}
|
}
|
||||||
state.pterodactyls = state.pterodactyls.filter((f) => f.x + f.width > state.cameraX - 150);
|
state.pterodactyls = state.pterodactyls.filter((f) => f.x + f.width > state.cameraX - 150);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user