feat: unlock falling meteor hazards after score > 250
This commit is contained in:
parent
9394d2b9f3
commit
a3e49dffb9
34
js/game.js
34
js/game.js
@ -34,6 +34,8 @@ const state = {
|
|||||||
],
|
],
|
||||||
pterodactyls: [],
|
pterodactyls: [],
|
||||||
pteroSpawnTimer: 0,
|
pteroSpawnTimer: 0,
|
||||||
|
meteors: [],
|
||||||
|
meteorSpawnTimer: 0,
|
||||||
player: {
|
player: {
|
||||||
x: 160,
|
x: 160,
|
||||||
y: 0,
|
y: 0,
|
||||||
@ -111,6 +113,15 @@ function drawPterodactyls() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function drawMeteors() {
|
||||||
|
ctx.fillStyle = '#6b4d2e';
|
||||||
|
for (const m of state.meteors) {
|
||||||
|
const sx = m.x - state.cameraX;
|
||||||
|
if (sx + m.width < 0 || sx > canvas.width) continue;
|
||||||
|
ctx.fillRect(sx, m.y, m.width, m.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function intersects(a, b) {
|
function intersects(a, b) {
|
||||||
return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
|
return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
|
||||||
}
|
}
|
||||||
@ -189,6 +200,27 @@ function updatePterodactyls(dt) {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateMeteors(dt) {
|
||||||
|
if (state.score <= 250) return;
|
||||||
|
|
||||||
|
state.meteorSpawnTimer -= dt;
|
||||||
|
if (state.meteorSpawnTimer <= 0) {
|
||||||
|
const spawnX = state.cameraX + 80 + Math.random() * (canvas.width - 160);
|
||||||
|
state.meteors.push({ x: spawnX, y: -30, width: 24, height: 30, vy: 420 });
|
||||||
|
state.meteorSpawnTimer = 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
const p = state.player;
|
||||||
|
for (const m of state.meteors) {
|
||||||
|
m.y += m.vy * dt;
|
||||||
|
if (intersects(p, m)) {
|
||||||
|
applyHit('Meteor strike!');
|
||||||
|
m.y = canvas.height + 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.meteors = state.meteors.filter((m) => m.y < canvas.height + 80);
|
||||||
|
}
|
||||||
|
|
||||||
function updatePlayer(dt) {
|
function updatePlayer(dt) {
|
||||||
const p = state.player;
|
const p = state.player;
|
||||||
let move = 0;
|
let move = 0;
|
||||||
@ -253,12 +285,14 @@ function tick(ts) {
|
|||||||
updatePlayer(dt);
|
updatePlayer(dt);
|
||||||
updateAnts(dt);
|
updateAnts(dt);
|
||||||
updatePterodactyls(dt);
|
updatePterodactyls(dt);
|
||||||
|
updateMeteors(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
drawBackground();
|
drawBackground();
|
||||||
drawAnts();
|
drawAnts();
|
||||||
drawPterodactyls();
|
drawPterodactyls();
|
||||||
|
drawMeteors();
|
||||||
drawTRex(state.player);
|
drawTRex(state.player);
|
||||||
drawDeathText();
|
drawDeathText();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user