Terrain colors

This commit is contained in:
zenoamaro committed 2019-06-28 02:18:54 +02:00
1 parent 041e6550b8
commit cb93c61774
3 files changed
+50 -17

No files matched your search

+7 -3
View File
@@ -11,7 +11,7 @@ import {AI_TYPES} from './ai.js';
let idle = false; let idle = false;
let state = 'start-turn'; let state = 'start-game';
const players = []; const players = [];
let currentPlayer = 0; let currentPlayer = 0;
let projectile = null; let projectile = null;
@@ -60,8 +60,12 @@ for (let color of PLAYER_COLORS) {
function update() { function update() {
idle = false; idle = false;
if (state === 'start-turn') { if (state === 'start-game') {
wind = randomInt(-MAX_WIND, +MAX_WIND); wind = randomInt(-MAX_WIND, +MAX_WIND);
state = 'start-turn';
}
else if (state === 'start-turn') {
state = 'aim'; state = 'aim';
} }
@@ -272,7 +276,7 @@ function fadeProjectiles(amount) {
function drawExplosions() { function drawExplosions() {
const explosionType = EXPLOSION_TYPES[explosion.type]; const explosionType = EXPLOSION_TYPES[explosion.type];
explosionType.draw(explosion, foreground); explosionType.draw(explosion, foreground, terrain);
} }
function drawStatus() { function drawStatus() {
+36 -7
View File
@@ -1,5 +1,6 @@
import {createCanvas, drawRect, clipCanvas} from './gfx.js'; import {createCanvas, drawRect, clipCanvas, drawCircle} from './gfx.js';
import {coords2index} from './math.js'; import {coords2index, random, randomInt, clamp} from './math.js';
import {sample} from './utils.js';
let cachedImageData; let cachedImageData;
@@ -10,18 +11,46 @@ function cacheImageData(ctx) {
export function createTerrain(width, height) { export function createTerrain(width, height) {
const terrain = createCanvas(width, height); const terrain = createCanvas(width, height);
const generateTerrain = sample(Object.values(TERRAIN_TYPES));
generateTerrain(terrain); generateTerrain(terrain);
return terrain; return terrain;
} }
export function generateTerrain(ctx) { const TERRAIN_TYPES = {
mountain(ctx) {
ctx.color = sample(['palegreen', 'white']); // FIXME
const {width, height} = ctx.canvas; const {width, height} = ctx.canvas;
const stepCount = 8;
const stepSize = width / stepCount;
let cy = random(.3, .7) * height;
let dy = 0;
for (let x=0; x<width; x++) { for (let x=0; x<width; x++) {
const h = Math.round(120 + 30 * Math.sin(x/25)); if (x % stepSize === 0) dy = ((random(.3, .7)*height)-cy)/stepSize;
drawRect(ctx, x, height-h, 1, h, 'burlywood'); if (x % (stepSize/16) === 0) dy = dy - random(-1, 1);
cy = clamp(0, Math.round(cy + dy), height);
drawRect(ctx, x, cy, 1, height-cy, ctx.color);
} }
cacheImageData(ctx); cacheImageData(ctx);
} },
sand(ctx) {
ctx.color = 'burlywood'; // FIXME
const {width, height} = ctx.canvas;
const stepCount = 16;
const stepSize = width / stepCount;
for (let s=0; s<=stepCount; s++) {
drawCircle(ctx, s*stepSize, height, randomInt(stepSize, stepSize*2), ctx.color);
if (s % 3 === 0) drawCircle(ctx, s*stepSize, 0, randomInt(0, stepSize*4), ctx.color);
}
cacheImageData(ctx);
collapseTerrain(ctx, width/2, width/2);
}
};
export function collapseTerrain(ctx, ox, r) { export function collapseTerrain(ctx, ox, r) {
const {height} = ctx.canvas; const {height} = ctx.canvas;
@@ -37,7 +66,7 @@ export function collapseTerrain(ctx, ox, r) {
} }
ctx.clearRect(left+x, 0, 1, height); ctx.clearRect(left+x, 0, 1, height);
drawRect(ctx, left+x, height-land, 1, land, 'burlywood'); drawRect(ctx, left+x, height-land, 1, land, ctx.color);
} }
cacheImageData(ctx); cacheImageData(ctx);
+7 -7
View File
@@ -8,8 +8,8 @@ export function drawExplosion(ctx, x, y, r) {
drawCircle(ctx, x, y, r, `rgb(${color}, 0, 0)`); drawCircle(ctx, x, y, r, `rgb(${color}, 0, 0)`);
} }
export function drawDirt(ctx, x, y, r) { export function drawDirt(ctx, x, y, r, c) {
drawCircle(ctx, x, y, r, 'burlywood'); drawCircle(ctx, x, y, r, c);
} }
export const WEAPON_TYPES = [ export const WEAPON_TYPES = [
@@ -72,12 +72,12 @@ export const EXPLOSION_TYPES = {
update(explosion) { update(explosion) {
return ++explosion.cr < explosion.r; return ++explosion.cr < explosion.r;
}, },
draw(explosion, foreground) { draw(explosion, foreground, terrain) {
const {x, y, cr, osc} = explosion; const {x, y, cr, osc} = explosion;
const f = explosion.cr % 2 === 0 ? 220 + explosion.cr : 0; const f = explosion.cr % 2 === 0 ? 220 + explosion.cr : 0;
osc.frequency.setValueAtTime(f, audio.currentTime); osc.frequency.setValueAtTime(f, audio.currentTime);
osc.frequency.setValueAtTime(0, audio.currentTime+0.1); osc.frequency.setValueAtTime(0, audio.currentTime+0.1);
drawDirt(foreground, x, y, cr); drawDirt(foreground, x, y, cr, terrain.color);
}, },
clip(explosion, terrain) { clip(explosion, terrain) {
const {x, y, cr} = explosion; const {x, y, cr} = explosion;
@@ -100,7 +100,7 @@ export const EXPLOSION_TYPES = {
update(explosion) { update(explosion) {
return ++explosion.cr < explosion.r; return ++explosion.cr < explosion.r;
}, },
draw(explosion, foreground) { draw(explosion, foreground, terrain) {
const {x, y, cr, osc, pattern} = explosion; const {x, y, cr, osc, pattern} = explosion;
let row = []; let row = [];
@@ -112,7 +112,7 @@ export const EXPLOSION_TYPES = {
for (let cy=0; cy<pattern.length; cy++) { for (let cy=0; cy<pattern.length; cy++) {
const row = pattern[cy]; const row = pattern[cy];
for (let cx=0; cx<row.length; cx++) { for (let cx=0; cx<row.length; cx++) {
if (row[cx]) plot(foreground, x-cy+cx, y-cy, 'burlywood'); if (row[cx]) plot(foreground, x-cy+cx, y-cy, terrain.color);
} }
} }
@@ -126,7 +126,7 @@ export const EXPLOSION_TYPES = {
for (let cy=0; cy<pattern.length; cy++) { for (let cy=0; cy<pattern.length; cy++) {
const row = pattern[cy]; const row = pattern[cy];
for (let cx=0; cx<row.length; cx++) { for (let cx=0; cx<row.length; cx++) {
if (row[cx]) plot(terrain, x-cy+cx, y-cy, 'burlywood'); if (row[cx]) plot(terrain, x-cy+cx, y-cy, terrain.color);
} }
} }
}, },