From cfd6a59c5b14f8c9e05f9d12f6536f18ebeda821 Mon Sep 17 00:00:00 2001 From: zenoamaro Date: Sun, 23 Jun 2019 23:23:50 +0200 Subject: [PATCH] Sky --- main.js | 20 +++++++++++++------- sky.js | 19 +++++++++++++++++++ terrain.js | 17 ++++++++++++++++- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 sky.js diff --git a/main.js b/main.js index 6978f50..06bf072 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,10 @@ import {key} from './input.js'; -import {createTerrain, clipTerrain, isTerrain, landHeight} from './terrain.js'; +import {createTerrain, clipTerrain, isTerrain, landHeight, collapseTerrain} from './terrain.js'; import {createCanvas, drawLine, loop, drawText, drawRect} from './gfx.js'; import {wrap, clamp, parable, deg2rad, vec, randomInt} from './math.js'; import {drawExplosion, WEAPONS} from './weapons.js'; import {createOsc, audio} from './sound.js'; +import {createSky} from './sky.js'; const W = 640; const H = 400; const Z = 2; @@ -21,10 +22,9 @@ fb.canvas.style.width = `${W * Z}px`; fb.canvas.style.height = `${H * Z}px`; document.body.appendChild(fb.canvas); -// Init terrain +// Init aux canvases +const sky = createSky(W, H); const terrain = createTerrain(W, H); - -// Init projectiles const projectiles = createCanvas(W, H); // Init players @@ -70,7 +70,7 @@ function update() { else if (state === 'fire') { prevProjectile = {...projectile}; - if (fadeCount++ === 2) { + if (fadeCount++ === 1) { fadeProjectiles(1); fadeCount = 0; } @@ -87,13 +87,18 @@ function update() { clipTerrain(terrain, (ctx) => drawExplosion(ctx, projectile.x, projectile.y, weapon.xr)); projectile.osc.stop(); projectile = null; - state = 'land'; + state = 'collapse-land'; return; } } } - else if (state === 'land') { + else if (state === 'collapse-land') { + collapseTerrain(terrain); + state = 'land-players'; + } + + else if (state === 'land-players') { let stable = true; for (let player of players) { const y = landHeight(terrain, player.x); @@ -113,6 +118,7 @@ function update() { function draw() { fb.clearRect(0, 0, W, H); + fb.drawImage(sky.canvas, 0, 0); fb.drawImage(terrain.canvas, 0, 0); if (projectile) drawProjectile(); fb.drawImage(projectiles.canvas, 0, 0); diff --git a/sky.js b/sky.js new file mode 100644 index 0000000..fc61024 --- /dev/null +++ b/sky.js @@ -0,0 +1,19 @@ +import {createCanvas, drawRect, clipCanvas, plot} from './gfx.js'; +import {randomInt} from './math.js'; + +export function createSky(width, height) { + const ctx = createCanvas(width, height); + generateSky(ctx); + return ctx; +} + +export function generateSky(ctx) { + const {width, height} = ctx.canvas; + const [r, g, b] = [70*0.6, 130*0.6, 180*0.6]; + + for (let y=0; y 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;