Particle system appears to be working
This commit is contained in:
parent
246453a25e
commit
13508b7fd9
@ -2,13 +2,18 @@
|
||||
|
||||
import MomentumEngine from "../../src/es6";
|
||||
|
||||
let white = new MomentumEngine.Classes.Color(255, 255, 255);
|
||||
let KeyConsts = MomentumEngine.Consts.Input.Keys;
|
||||
|
||||
|
||||
class BlueParticle extends MomentumEngine.Classes.Rect {
|
||||
|
||||
constructor (x, y) {
|
||||
super(x, y, 1, 1, white);
|
||||
super(x, y, 1, 1, new MomentumEngine.Classes.Color(255, 255, 255, 0));
|
||||
this.timeToLive = 5000;
|
||||
}
|
||||
|
||||
update (delta) {
|
||||
this.color.a = this.color.a - (delta * 0.00025);
|
||||
}
|
||||
|
||||
}
|
||||
@ -25,7 +30,10 @@ window.onload = function () {
|
||||
width: width,
|
||||
height: height,
|
||||
fixRatio: true,
|
||||
desiredFps: 60
|
||||
desiredFps: 60,
|
||||
inputs: {
|
||||
keyboard: true
|
||||
}
|
||||
});
|
||||
|
||||
let black = new MomentumEngine.Classes.Color(0, 0, 0),
|
||||
@ -34,14 +42,33 @@ window.onload = function () {
|
||||
let mainScene = new MomentumEngine.Classes.Rect(0, 0, width, height, black);
|
||||
particles.addChildEntity(mainScene);
|
||||
|
||||
let rect = new MomentumEngine.Classes.Rect(width / 10 - baseSize, height - (baseSize * 10), baseSize * 2, baseSize * 2, red),
|
||||
emitter = new MomentumEngine.Classes.Emitter(baseSize, baseSize, 2, new MomentumEngine.Classes.Vector2D(1, 1), BlueParticle);
|
||||
let rect = new MomentumEngine.Classes.Rect(width / 2 - baseSize, height / 2 - baseSize, baseSize * 2, baseSize * 2, red),
|
||||
emitter = new MomentumEngine.Classes.Emitter(baseSize, baseSize, 4, new MomentumEngine.Classes.Vector2D(0, 1), BlueParticle);
|
||||
|
||||
mainScene.addChildEntity(rect);
|
||||
rect.addChildEntity(emitter);
|
||||
rect.setVelocity(0.01, 0);
|
||||
rect.setAcceleration(0.01, 0);
|
||||
|
||||
rect.update = function (delta) {
|
||||
|
||||
if (particles.inputs.keyboard.isPressed(KeyConsts.UP)) {
|
||||
rect.pos.y -= (0.2 * delta);
|
||||
}
|
||||
|
||||
if (particles.inputs.keyboard.isPressed(KeyConsts.DOWN)) {
|
||||
rect.pos.y += (0.2 * delta);
|
||||
}
|
||||
|
||||
if (particles.inputs.keyboard.isPressed(KeyConsts.LEFT)) {
|
||||
rect.pos.x -= (0.2 * delta);
|
||||
}
|
||||
|
||||
if (particles.inputs.keyboard.isPressed(KeyConsts.RIGHT)) {
|
||||
rect.pos.x += (0.2 * delta);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//emitter.spread = Math.PI / 32;
|
||||
emitter.setParticleParent(mainScene);
|
||||
emitter.emitting = true;
|
||||
|
||||
|
@ -14,16 +14,13 @@ class Emitter extends Entity {
|
||||
this.particleVelocity = velocity;
|
||||
this.particleClass = particle;
|
||||
|
||||
this.emitRate = rate;
|
||||
this.rate = rate;
|
||||
this.emitting = false;
|
||||
this.spread = Math.PI;
|
||||
this._lastEmitTime = this._creationTime;
|
||||
this._wasEmitting = false;
|
||||
|
||||
this.particles = [];
|
||||
|
||||
this.spread = function () {
|
||||
return Math.PI / 1;
|
||||
}
|
||||
this._particles = [];
|
||||
|
||||
}
|
||||
|
||||
@ -38,14 +35,14 @@ class Emitter extends Entity {
|
||||
let ParticleClass = this.particleClass,
|
||||
parent = this._particleParent || this._parent;
|
||||
|
||||
let angle = this.particleVelocity.angle() + this.spread() - (Math.random() * this.spread() * 2),
|
||||
let angle = this.particleVelocity.angle() + this.spread - (Math.random() * this.spread * 2),
|
||||
magnitude = this.particleVelocity.length(),
|
||||
velocity = Vector2D.fromAngle(angle, magnitude);
|
||||
|
||||
let particle = new ParticleClass(this._calculatedPos.x, this._calculatedPos.y);
|
||||
particle.velocity = velocity;
|
||||
|
||||
this.particles.push(particle);
|
||||
//this._particles.push(particle);
|
||||
parent.addChildEntity(particle);
|
||||
|
||||
}
|
||||
@ -66,13 +63,12 @@ class Emitter extends Entity {
|
||||
this._lastEmitTime = currentTime;
|
||||
}
|
||||
|
||||
// In honour the code of Alex Evans
|
||||
let emitDelta = currentTime - this._lastEmitTime;
|
||||
if (emitDelta > this.emitRate) {
|
||||
if (emitDelta > this.rate) {
|
||||
|
||||
let emissions = ~~(emitDelta / this.emitRate);
|
||||
let emissions = ~~(emitDelta / this.rate);
|
||||
|
||||
this._lastEmitTime = currentTime + (emitDelta - (this.emitRate * emissions));
|
||||
this._lastEmitTime = currentTime + (emitDelta - (this.rate * emissions));
|
||||
|
||||
for (let i = 0; i < emissions; i++) {
|
||||
this._emit();
|
||||
|
Loading…
x
Reference in New Issue
Block a user