This commit is contained in:
zenoamaro committed 2019-06-26 03:05:17 +02:00
1 parent c4ab55ac2f
commit cfd6a59c5b
3 files changed
+48 -8

No files matched your search

+16 -1
View File
@@ -10,11 +10,26 @@ export function createTerrain(width, height) {
export function generateTerrain(ctx) {
const {width, height} = ctx.canvas;
for (let x=0; x<width; x++) {
const h = Math.round(80 + 30 * Math.sin(x/25));
const h = Math.round(120 + 30 * Math.sin(x/25));
drawRect(ctx, x, height-h, 1, h, 'burlywood');
}
}
export function collapseTerrain(ctx) {
const {width, height} = ctx.canvas;
for (let x=0; x<width; x++) {
const imageData = ctx.getImageData(x, 0, 1, height);
let land = 0;
for (let y=0; y<height*4; y+=4) {
if (imageData.data[y+3] > 0) land++;
}
ctx.clearRect(x, 0, 1, height);
drawRect(ctx, x, height-land, 1, land, 'burlywood');
}
}
export function isTerrain(ctx, x, y) {
const imageData = ctx.getImageData(x, y, 1, 1);
return imageData.data[3] > 0;