Add particles

This commit is contained in:
asraelite 2018-03-04 01:21:34 +00:00
parent dda4a44386
commit 9435e887a4
11 changed files with 1684 additions and 4826 deletions

View file

@ -15,7 +15,10 @@
version="1.1" version="1.1"
id="svg8" id="svg8"
inkscape:version="0.92.2 5c3e80d, 2017-08-06" inkscape:version="0.92.2 5c3e80d, 2017-08-06"
sodipodi:docname="background.svg"> sodipodi:docname="background.svg"
inkscape:export-filename="/home/asraelite/code/js/improcket/dist/img/background.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<defs <defs
id="defs2" /> id="defs2" />
<sodipodi:namedview <sodipodi:namedview
@ -25,9 +28,9 @@
borderopacity="1.0" borderopacity="1.0"
inkscape:pageopacity="0.0" inkscape:pageopacity="0.0"
inkscape:pageshadow="2" inkscape:pageshadow="2"
inkscape:zoom="0.079999996" inkscape:zoom="0.05656854"
inkscape:cx="1460.2236" inkscape:cx="3137.2136"
inkscape:cy="3294.9439" inkscape:cy="3051.1973"
inkscape:document-units="mm" inkscape:document-units="mm"
inkscape:current-layer="layer1" inkscape:current-layer="layer1"
showgrid="false" showgrid="false"

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Before After
Before After

4947
dist/img/stars_back.svg vendored

File diff suppressed because it is too large Load diff

Before

Width:  |  Height:  |  Size: 204 KiB

After

Width:  |  Height:  |  Size: 92 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 44 KiB

Before After
Before After

1457
dist/img/stars_front.svg vendored

File diff suppressed because it is too large Load diff

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

View file

@ -16,7 +16,7 @@ export function render() {
renderLayer(patterns.back, 0.3, 1); renderLayer(patterns.back, 0.3, 1);
renderLayer(patterns.middle, 0.5, 0.3); renderLayer(patterns.middle, 0.5, 0.3);
renderLayer(patterns.front, 0.7, 0.3); //renderLayer(patterns.front, 0.7, 0.3);
} }
function renderLayer(pattern, speed = 1, scale = 1) { function renderLayer(pattern, speed = 1, scale = 1) {

View file

@ -3,11 +3,14 @@ import {images as assets} from '../assets.mjs';
import * as world from '../world/index.mjs'; import * as world from '../world/index.mjs';
export function render() { export function render() {
world.particles.forEach(renderParticle);
world.celestials.forEach(renderCelestial); world.celestials.forEach(renderCelestial);
world.ships.forEach(renderShip); world.ships.forEach(renderShip);
if (typeof window.q !== 'undefined') {
context.fillStyle = 'red';
} }
function renderParticle(particle) {
context.fillStyle = particle.color;
context.fillRect(...particle.com, particle.size, particle.size);
} }
function renderShip(ship) { function renderShip(ship) {

View file

@ -20,6 +20,10 @@ export default class Body {
return [this.x, this.y]; return [this.x, this.y];
} }
get speed() {
return Math.sqrt(this.xvel ** 2 + this.yvel ** 2);
}
getWorldPoint(lx, ly) { getWorldPoint(lx, ly) {
let [cx, cy] = this.localCom; let [cx, cy] = this.localCom;
let [nx, ny] = this.rotateVector(lx - cx, ly - cy, this.r); let [nx, ny] = this.rotateVector(lx - cx, ly - cy, this.r);
@ -38,6 +42,10 @@ export default class Body {
(y * Math.cos(this.r) - x * Math.sin(this.r))]; (y * Math.cos(this.r) - x * Math.sin(this.r))];
} }
relativeVector(x, y) {
return this.rotateVector(x, y, this.r);
}
tickMotion() { tickMotion() {
this.x += this.xvel; this.x += this.xvel;
this.y += this.yvel; this.y += this.yvel;

View file

@ -6,6 +6,7 @@ export {getSectorFromWorld, getContainedSectors} from './sector.mjs';
export const entities = new Set(); export const entities = new Set();
export const celestials = new Set(); export const celestials = new Set();
export const ships = new Set(); export const ships = new Set();
export const particles = new Set();
export let playerShip = null; export let playerShip = null;
@ -16,12 +17,14 @@ export function setPlayerShip(ship) {
export function init() { export function init() {
entities.clear(); entities.clear();
celestials.clear(); celestials.clear();
ships.clear();
particles.clear();
spawn.player(); spawn.player();
spawn.startPlanet(); spawn.startPlanet();
} }
export function tick() { export function tick() {
particles.forEach(p => p.tick());
celestials.forEach(c => c.tick()); celestials.forEach(c => c.tick());
entities.forEach(e => e.tick()); entities.forEach(e => e.tick());
ships.forEach(s => s.tick()); ships.forEach(s => s.tick());

63
js/world/particle.mjs Normal file
View file

@ -0,0 +1,63 @@
import Body from './body.mjs';
import {celestials, particles} from './index.mjs';
export function createThrustExhaust(thruster) {
let ship = thruster.ship;
let [fx, fy] = ship.relativeVector(0, 0.2);
let [dx, dy] = ship.relativeVector((Math.random() - 0.5) * 0.5, 0.5);
let [cx, cy] = thruster.com;
particles.add(new Particle(cx + dx, cy + dy, {
xvel: ship.xvel + fx,
yvel: ship.yvel + fy,
color: '#f4c542',
lifetime: 10,
size: 0.07
}));
}
class Particle extends Body {
constructor(x, y, {
xvel = 0,
yvel = 0,
spray = 0.1,
fizzle = 0,
maxFizzle = fizzle * 2,
color = '#fff',
gravity = false,
lifetime = 50,
size = 0.1,
friction = 0.99
}) {
super(x, y, 0.1);
this.size = size;
this.xvel = xvel + (Math.random() - 0.5) * spray;
this.yvel = yvel + (Math.random() - 0.5) * spray;
this.friction = friction;
this.fizzle = fizzle;
this.maxFizzle = maxFizzle;
this.color = color;
this.gravity = gravity;
this.life = lifetime;
}
get com() {
return [this.x - this.size / 2, this.y - this.size / 2];
}
tick() {
if (!this.life--) {
particles.delete(this);
return;
}
this.tickMotion();
if (this.gravity) this.tickGravity(celestials);
this.xvel *= this.friction;
this.yvel *= this.friction;
this.x += (Math.random() - 0.5) * this.fizzle;
this.y += (Math.random() - 0.5) * this.fizzle;
if (this.fizzle < this.mazFizzle) this.fizzle *= 1.05;
}
}

View file

@ -1,6 +1,7 @@
import Module from './module.mjs'; import Module from './module.mjs';
import Body from './body.mjs'; import Body from './body.mjs';
import * as world from './index.mjs'; import * as world from './index.mjs';
import * as particle from './particle.mjs';
export default class Ship extends Body { export default class Ship extends Body {
constructor(x, y) { constructor(x, y) {
@ -20,6 +21,13 @@ export default class Ship extends Body {
this.tickMotion(); this.tickMotion();
this.tickGravity(world.celestials); this.tickGravity(world.celestials);
this.resolveCollisions(); this.resolveCollisions();
this.modules.forEach(m => {
if (m.type == 'thruster' && m.power !== 0) {
for (let i = 0; i < 2; i++) particle.createThrustExhaust(m);
}
});
this.modules.forEach(m => m.reset()); this.modules.forEach(m => m.reset());
} }