Death explosions
This commit is contained in:
1 parent
ea89cbc2cc
commit
82cba65527
5 files changed
+35
-24
No files matched your search
@@ -6,6 +6,7 @@ import {createSky} from './sky.js';
|
|||||||
import {audio, createOsc} from './sound.js';
|
import {audio, createOsc} from './sound.js';
|
||||||
import {collapseTerrain, createTerrain, isTerrain, landHeight, closestLand} from './terrain.js';
|
import {collapseTerrain, createTerrain, isTerrain, landHeight, closestLand} from './terrain.js';
|
||||||
import {WEAPON_TYPES, EXPLOSION_TYPES} from './weapons.js';
|
import {WEAPON_TYPES, EXPLOSION_TYPES} from './weapons.js';
|
||||||
|
import {sample} from './utils.js';
|
||||||
|
|
||||||
|
|
||||||
let state = 'start-turn';
|
let state = 'start-turn';
|
||||||
@@ -100,31 +101,31 @@ function update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if (state === 'explosion') {
|
else if (state === 'explosion') {
|
||||||
const {weapon} = projectile;
|
const explosionType = EXPLOSION_TYPES[explosion.type];
|
||||||
const explosionType = EXPLOSION_TYPES[weapon.explosion.type];
|
|
||||||
|
|
||||||
if (!explosionType.update(explosion)) {
|
if (!explosionType.update(explosion)) {
|
||||||
explosionType.clip(explosion, terrain);
|
explosionType.clip(explosion, terrain);
|
||||||
explosionType.stop(explosion);
|
explosionType.stop(explosion);
|
||||||
for (let player of players) {
|
for (let player of players) if (!player.dead) {
|
||||||
if (player.energy > 0) {
|
|
||||||
player.energy -= explosionType.damage(explosion, player) || 0;
|
player.energy -= explosionType.damage(explosion, player) || 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
explosion = null;
|
explosion = null;
|
||||||
state = 'land-collapse';
|
state = 'land-collapse';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (state === 'land-collapse') {
|
else if (state === 'land-collapse') {
|
||||||
collapseTerrain(terrain, projectile.x, 100); // FIXME: Hardcoded radius
|
const target = explosion ||projectile;
|
||||||
projectile = null;
|
if (target) {
|
||||||
|
collapseTerrain(terrain, target.x, 100); // FIXME: Hardcoded radius
|
||||||
|
}
|
||||||
state = 'land-players';
|
state = 'land-players';
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (state === 'land-players') {
|
else if (state === 'land-players') {
|
||||||
let stable = true;
|
let stable = true;
|
||||||
for (let player of players) {
|
for (let player of players) {
|
||||||
|
if (player.dead) continue;
|
||||||
const y = closestLand(terrain, player.x, player.y);
|
const y = closestLand(terrain, player.x, player.y);
|
||||||
if (player.y !== y) {
|
if (player.y !== y) {
|
||||||
player.y++;
|
player.y++;
|
||||||
@@ -135,7 +136,14 @@ function update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if (state === 'destroy-players') {
|
else if (state === 'destroy-players') {
|
||||||
state = 'end-turn';
|
const dyingPlayer = players.find(x => x.energy<=0 && !x.dead);
|
||||||
|
if (!dyingPlayer) {state = 'end-turn'; return}
|
||||||
|
|
||||||
|
const {x, y} = dyingPlayer;
|
||||||
|
const explosionType = sample(Object.values(EXPLOSION_TYPES));
|
||||||
|
explosion = explosionType.create({r:25}, x, y);
|
||||||
|
dyingPlayer.dead = true;
|
||||||
|
state = 'explosion';
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (state === 'end-turn') {
|
else if (state === 'end-turn') {
|
||||||
@@ -146,8 +154,9 @@ function update() {
|
|||||||
currentPlayer = wrap(0, currentPlayer+1, players.length);
|
currentPlayer = wrap(0, currentPlayer+1, players.length);
|
||||||
player = players[currentPlayer];
|
player = players[currentPlayer];
|
||||||
if (i++ === players.length) state = 'game-over';
|
if (i++ === players.length) state = 'game-over';
|
||||||
} while (player.energy <= 0)
|
} while (player.dead)
|
||||||
|
|
||||||
|
projectile = null;
|
||||||
state = 'start-turn';
|
state = 'start-turn';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,8 +174,8 @@ function draw() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function drawPlayer(player) {
|
function drawPlayer(player) {
|
||||||
const {x, y, a, c, energy} = player;
|
const {x, y, a, c, dead} = player;
|
||||||
if (energy <= 0) return;
|
if (dead) return;
|
||||||
const [px, py] = vec(x, y-3, a+180, 3);
|
const [px, py] = vec(x, y-3, a+180, 3);
|
||||||
drawLine(foreground, x, y-3, Math.round(px), Math.round(py), c);
|
drawLine(foreground, x, y-3, Math.round(px), Math.round(py), c);
|
||||||
drawRect(foreground, x-3, y-2, 6, 3, c);
|
drawRect(foreground, x-3, y-2, 6, 3, c);
|
||||||
@@ -187,8 +196,7 @@ function fadeProjectiles(amount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function drawExplosions() {
|
function drawExplosions() {
|
||||||
const {weapon} = projectile;
|
const explosionType = EXPLOSION_TYPES[explosion.type];
|
||||||
const explosionType = EXPLOSION_TYPES[weapon.explosion.type];
|
|
||||||
explosionType.draw(explosion, foreground);
|
explosionType.draw(explosion, foreground);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ export function randomInt(min, max) {
|
|||||||
return Math.round(random(min, max));
|
return Math.round(random(min, max));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sample(arr) {
|
|
||||||
return arr[randomInt(0, arr.length-1)];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function clamp(min, x, max) {
|
export function clamp(min, x, max) {
|
||||||
return Math.max(min, Math.min(x, max));
|
return Math.max(min, Math.min(x, max));
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import {createCanvas, drawRect, clipCanvas, plot} from './gfx.js';
|
import {createCanvas, drawRect, clipCanvas} from './gfx.js';
|
||||||
import {coords2index} from './math.js';
|
import {coords2index} from './math.js';
|
||||||
|
|
||||||
let cachedImageData;
|
let cachedImageData;
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
|
import {randomInt} from './math.js';
|
||||||
|
|
||||||
|
export function sample(arr) {
|
||||||
|
return arr[randomInt(0, arr.length-1)];
|
||||||
|
}
|
||||||
|
|
||||||
export function nextId() {
|
export function nextId() {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const id = nextId.id || 0;
|
const id = nextId.id || 0;
|
||||||
|
|||||||
+7
-6
@@ -9,10 +9,10 @@ export function drawExplosion(ctx, x, y, r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const WEAPON_TYPES = [
|
export const WEAPON_TYPES = [
|
||||||
// {id:'baby-missile', name:'Baby Missile', explosion:{type:'blast', r:5}},
|
{id:'baby-missile', name:'Baby Missile', explosion:{type:'blast', r:5}},
|
||||||
// {id:'missile', name:'Missile', explosion:{type:'blast', r:10}},
|
{id:'missile', name:'Missile', explosion:{type:'blast', r:10}},
|
||||||
// {id:'baby-nuke', name:'Baby Nuke', explosion:{type:'blast', r:50}},
|
{id:'baby-nuke', name:'Baby Nuke', explosion:{type:'blast', r:50}},
|
||||||
// {id:'nuke', name:'Nuke', explosion:{type:'blast', r:100}},
|
{id:'nuke', name:'Nuke', explosion:{type:'blast', r:100}},
|
||||||
{id:'dirt', name:'Dirt', explosion:{type:'dirt', r:25}},
|
{id:'dirt', name:'Dirt', explosion:{type:'dirt', r:25}},
|
||||||
{id:'large-dirt', name:'Ton of Dirt', explosion:{type:'dirt', r:75}},
|
{id:'large-dirt', name:'Ton of Dirt', explosion:{type:'dirt', r:75}},
|
||||||
];
|
];
|
||||||
@@ -23,7 +23,7 @@ export const EXPLOSION_TYPES = {
|
|||||||
const {r} = spec;
|
const {r} = spec;
|
||||||
const osc = createOsc();
|
const osc = createOsc();
|
||||||
osc.start();
|
osc.start();
|
||||||
return {x, y, r, cr:0, osc};
|
return {type:'blast', x, y, r, cr:0, osc};
|
||||||
},
|
},
|
||||||
update(explosion) {
|
update(explosion) {
|
||||||
return ++explosion.cr < explosion.r;
|
return ++explosion.cr < explosion.r;
|
||||||
@@ -53,7 +53,7 @@ export const EXPLOSION_TYPES = {
|
|||||||
const {r} = spec;
|
const {r} = spec;
|
||||||
const osc = createOsc();
|
const osc = createOsc();
|
||||||
osc.start();
|
osc.start();
|
||||||
return {x, y, r, cr:0, osc};
|
return {type:'dirt', x, y, r, cr:0, osc};
|
||||||
},
|
},
|
||||||
stop(explosion) {
|
stop(explosion) {
|
||||||
const {osc} = explosion;
|
const {osc} = explosion;
|
||||||
@@ -66,6 +66,7 @@ export const EXPLOSION_TYPES = {
|
|||||||
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+1);
|
||||||
drawExplosion(foreground, x, y, cr);
|
drawExplosion(foreground, x, y, cr);
|
||||||
},
|
},
|
||||||
clip(explosion, terrain) {
|
clip(explosion, terrain) {
|
||||||
|
|||||||
Reference in new issue
Block a user