From 521ec559bfc6860e25d0a39720b2fb94082972c8 Mon Sep 17 00:00:00 2001 From: zenoamaro Date: Wed, 26 Jun 2019 02:39:19 +0200 Subject: [PATCH] Fix tank landing --- main.js | 6 +++--- terrain.js | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index c44bf3f..c6e7524 100644 --- a/main.js +++ b/main.js @@ -4,7 +4,7 @@ import {key} from './input.js'; import {clamp, deg2rad, parable, randomInt, vec, wrap} from './math.js'; import {createSky} from './sky.js'; import {audio, createOsc} from './sound.js'; -import {clipTerrain, collapseTerrain, createTerrain, isTerrain, landHeight} from './terrain.js'; +import {clipTerrain, collapseTerrain, createTerrain, isTerrain, landHeight, closestLand} from './terrain.js'; import {drawExplosion, WEAPON_TYPES, EXPLOSION_TYPES} from './weapons.js'; @@ -35,7 +35,7 @@ for (let color of PLAYER_COLORS) { const x = Math.round(50 + (W-100) / 5 * i); const a = x > W/2 ? 45 : 180-45; players.push({ - x, y: landHeight(terrain, x), a, + x, y: landHeight(terrain, x)+1, a, p: PLAYER_INITIAL_POWER, c: color, weapon: WEAPON_TYPES[i % WEAPON_TYPES.length], energy: PLAYER_MAX_ENERGY, @@ -122,7 +122,7 @@ function update() { else if (state === 'land-players') { let stable = true; for (let player of players) { - const y = landHeight(terrain, player.x); + const y = closestLand(terrain, player.x, player.y); if (player.y !== y) { player.y++; stable = false; diff --git a/terrain.js b/terrain.js index 66077df..19e313c 100644 --- a/terrain.js +++ b/terrain.js @@ -37,6 +37,15 @@ export function isTerrain(ctx, x, y) { return imageData.data[3] > 0; } +export function closestLand(ctx, x, y) { + const {height} = ctx.canvas; + const imageData = ctx.getImageData(x, 0, 1, height); + for (let i=y; i