Spaces:
Running
Running
make game speed at delta time
Browse files- index.html +29 -13
index.html
CHANGED
|
@@ -540,6 +540,10 @@
|
|
| 540 |
totalAICollected: 0,
|
| 541 |
rareAICollected: 0,
|
| 542 |
gameStartTime: Date.now(),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
};
|
| 544 |
|
| 545 |
// Room generation with deterministic seeding
|
|
@@ -2177,23 +2181,24 @@
|
|
| 2177 |
}
|
| 2178 |
|
| 2179 |
// Update
|
| 2180 |
-
function update() {
|
| 2181 |
const player = game.player;
|
| 2182 |
-
const speed = 0
|
|
|
|
| 2183 |
let dx = 0,
|
| 2184 |
dy = 0;
|
| 2185 |
|
| 2186 |
if (keys["arrowup"] || keys["w"]) {
|
| 2187 |
-
dy = -
|
| 2188 |
player.facing = "up";
|
| 2189 |
} else if (keys["arrowdown"] || keys["s"]) {
|
| 2190 |
-
dy =
|
| 2191 |
player.facing = "down";
|
| 2192 |
} else if (keys["arrowleft"] || keys["a"]) {
|
| 2193 |
-
dx = -
|
| 2194 |
player.facing = "left";
|
| 2195 |
} else if (keys["arrowright"] || keys["d"]) {
|
| 2196 |
-
dx =
|
| 2197 |
player.facing = "right";
|
| 2198 |
}
|
| 2199 |
|
|
@@ -2298,15 +2303,16 @@
|
|
| 2298 |
}
|
| 2299 |
}
|
| 2300 |
|
| 2301 |
-
player.animFrame += 0
|
| 2302 |
}
|
| 2303 |
|
| 2304 |
// Update particles
|
| 2305 |
game.particles = game.particles.filter((p) => {
|
| 2306 |
-
|
| 2307 |
-
p.
|
| 2308 |
-
p.
|
| 2309 |
-
p.
|
|
|
|
| 2310 |
return p.life > 0;
|
| 2311 |
});
|
| 2312 |
}
|
|
@@ -3436,8 +3442,18 @@
|
|
| 3436 |
}
|
| 3437 |
|
| 3438 |
// Game loop
|
| 3439 |
-
function gameLoop() {
|
| 3440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3441 |
render();
|
| 3442 |
requestAnimationFrame(gameLoop);
|
| 3443 |
}
|
|
|
|
| 540 |
totalAICollected: 0,
|
| 541 |
rareAICollected: 0,
|
| 542 |
gameStartTime: Date.now(),
|
| 543 |
+
|
| 544 |
+
// Timing
|
| 545 |
+
lastTime: 0,
|
| 546 |
+
deltaTime: 0,
|
| 547 |
};
|
| 548 |
|
| 549 |
// Room generation with deterministic seeding
|
|
|
|
| 2181 |
}
|
| 2182 |
|
| 2183 |
// Update
|
| 2184 |
+
function update(deltaTime) {
|
| 2185 |
const player = game.player;
|
| 2186 |
+
const speed = 4.0; // tiles per second
|
| 2187 |
+
const frameSpeed = speed * (deltaTime / 1000); // convert to per-frame movement
|
| 2188 |
let dx = 0,
|
| 2189 |
dy = 0;
|
| 2190 |
|
| 2191 |
if (keys["arrowup"] || keys["w"]) {
|
| 2192 |
+
dy = -frameSpeed;
|
| 2193 |
player.facing = "up";
|
| 2194 |
} else if (keys["arrowdown"] || keys["s"]) {
|
| 2195 |
+
dy = frameSpeed;
|
| 2196 |
player.facing = "down";
|
| 2197 |
} else if (keys["arrowleft"] || keys["a"]) {
|
| 2198 |
+
dx = -frameSpeed;
|
| 2199 |
player.facing = "left";
|
| 2200 |
} else if (keys["arrowright"] || keys["d"]) {
|
| 2201 |
+
dx = frameSpeed;
|
| 2202 |
player.facing = "right";
|
| 2203 |
}
|
| 2204 |
|
|
|
|
| 2303 |
}
|
| 2304 |
}
|
| 2305 |
|
| 2306 |
+
player.animFrame += 5.0 * (deltaTime / 1000); // 5 animation cycles per second
|
| 2307 |
}
|
| 2308 |
|
| 2309 |
// Update particles
|
| 2310 |
game.particles = game.particles.filter((p) => {
|
| 2311 |
+
const frameMultiplier = deltaTime / 16.67; // normalize to 60fps
|
| 2312 |
+
p.x += p.vx * frameMultiplier;
|
| 2313 |
+
p.y += p.vy * frameMultiplier;
|
| 2314 |
+
p.vy += 0.5 * frameMultiplier;
|
| 2315 |
+
p.life -= frameMultiplier;
|
| 2316 |
return p.life > 0;
|
| 2317 |
});
|
| 2318 |
}
|
|
|
|
| 3442 |
}
|
| 3443 |
|
| 3444 |
// Game loop
|
| 3445 |
+
function gameLoop(currentTime) {
|
| 3446 |
+
// Calculate delta time
|
| 3447 |
+
if (game.lastTime === 0) {
|
| 3448 |
+
game.lastTime = currentTime;
|
| 3449 |
+
}
|
| 3450 |
+
game.deltaTime = currentTime - game.lastTime;
|
| 3451 |
+
game.lastTime = currentTime;
|
| 3452 |
+
|
| 3453 |
+
// Cap delta time to prevent large jumps (e.g., when tab is inactive)
|
| 3454 |
+
game.deltaTime = Math.min(game.deltaTime, 33.33); // Max 30fps minimum
|
| 3455 |
+
|
| 3456 |
+
update(game.deltaTime);
|
| 3457 |
render();
|
| 3458 |
requestAnimationFrame(gameLoop);
|
| 3459 |
}
|