diff options
author | elioat <hi@eli.li> | 2023-12-31 16:47:46 -0500 |
---|---|---|
committer | elioat <hi@eli.li> | 2023-12-31 16:47:46 -0500 |
commit | 1092243478dbe00b60202e31d98c0a81f2b8c962 (patch) | |
tree | 2a8e1646e554ddf3e5a97280618f547352fa9152 /js | |
parent | 6b7318545a102f5e01eaa571e12e3d3adab6d8ce (diff) | |
download | tour-1092243478dbe00b60202e31d98c0a81f2b8c962.tar.gz |
*
Diffstat (limited to 'js')
-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); |