diff options
-rw-r--r-- | js/hill/game.js | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/js/hill/game.js b/js/hill/game.js index 831e98e..6413119 100644 --- a/js/hill/game.js +++ b/js/hill/game.js @@ -40,8 +40,8 @@ function initialize() { // Calculate the y-coordinate of the terrain line at the x-coordinate of the object let terrainY = ((terrain.end.y - terrain.start.y) / (terrain.end.x - terrain.start.x)) * (object.x - terrain.start.x) + terrain.start.y; - // Set the y-coordinate of the object to the y-coordinate of the terrain line - object.y = terrainY; + // Set the y-coordinate of the object to the y-coordinate of the terrain line minus the height of the object + object.y = terrainY - object.height; objects.push(object); } @@ -110,8 +110,20 @@ const gameLoop = () => { player.vy = 0; } - checkCollision(); + for (let object of objects) { + // Update y-coordinate based on gravity + object.y += gravity; + + // Calculate the y-coordinate of the terrain line at the x-coordinate of the object + let terrainY = ((terrain.end.y - terrain.start.y) / (terrain.end.x - terrain.start.x)) * (object.x - terrain.start.x) + terrain.start.y; + // If the new y-coordinate is below the terrain line, set it to the terrain line + if (object.y + object.height > terrainY) { + object.y = terrainY - object.height; + } + } + + checkCollision(); initialize(); draw(); requestAnimationFrame(gameLoop); |