Terrain and tanks

This commit is contained in:
zenoamaro committed 2019-06-26 03:04:33 +02:00
1 parent 49591cbea3
commit 175cbc15b3
2 files changed
+26 -12

No files matched your search

+17 -4
View File
@@ -1,13 +1,18 @@
import {createCanvas, drawRect, clipCanvas} from './gfx.js';
import {createCanvas, drawRect, clipCanvas, plot} from './gfx.js';
import {randomInt} from './math.js';
export function createTerrain(width, height) {
const terrain = createCanvas(width, height);
generateTerrain(terrain, width, height);
generateTerrain(terrain);
return terrain;
}
export function generateTerrain(ctx, width, height) {
drawRect(ctx, 0, height-80, width, 80, 'burlywood');
export function generateTerrain(ctx) {
const {width, height} = ctx.canvas;
for (let x=0; x<width; x++) {
const h = Math.round(80 + 8 * Math.sin(x/25));
drawRect(ctx, x, height-h, 1, h, 'burlywood');
}
}
export function isTerrain(ctx, x, y) {
@@ -15,6 +20,14 @@ export function isTerrain(ctx, x, y) {
return imageData.data[3] > 0;
}
export function landHeight(ctx, x) {
const {height} = ctx.canvas;
const imageData = ctx.getImageData(x, 0, 1, height);
for (let y=height-1; y>=0; y--) {
if (imageData.data[y*4+3] === 0) return y;
}
}
export function clipTerrain(ctx, fn) {
return clipCanvas(ctx, fn);
}