287 lines
8.4 KiB
JavaScript
287 lines
8.4 KiB
JavaScript
/**
|
|
* Creates a ParticleEngine object
|
|
* @param graphics GameLib.GraphicsRuntime
|
|
* @param apiParticleEngine GameLib.D3.API.ParticleEngine
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.ParticleEngine = function(
|
|
graphics,
|
|
apiParticleEngine
|
|
) {
|
|
|
|
this.graphics = graphics;
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiParticleEngine)) {
|
|
apiParticleEngine = {};
|
|
}
|
|
|
|
GameLib.D3.API.ParticleEngine.call(
|
|
this,
|
|
apiParticleEngine.id,
|
|
apiParticleEngine.name,
|
|
apiParticleEngine.position,
|
|
apiParticleEngine.direction,
|
|
apiParticleEngine.enabled,
|
|
apiParticleEngine.templateParticle,
|
|
apiParticleEngine.particlesPerSecond,
|
|
apiParticleEngine.frequency,
|
|
apiParticleEngine.elapsed,
|
|
apiParticleEngine.camera,
|
|
apiParticleEngine.pulse,
|
|
apiParticleEngine.parentEntity
|
|
);
|
|
|
|
this.position = new GameLib.Vector3(
|
|
graphics,
|
|
this.position,
|
|
this
|
|
);
|
|
|
|
this.direction = new GameLib.Vector3(
|
|
graphics,
|
|
this.direction,
|
|
this
|
|
);
|
|
|
|
if (this.templateParticle instanceof GameLib.D3.API.Particle) {
|
|
this.templateParticle = new GameLib.D3.Particle(
|
|
graphics,
|
|
this.templateParticle
|
|
)
|
|
}
|
|
|
|
this.particles = [];
|
|
|
|
this.disabledForRemoval = false;
|
|
|
|
GameLib.Component.call(
|
|
this,
|
|
{
|
|
templateParticle : GameLib.D3.Particle,
|
|
camera : GameLib.D3.Camera
|
|
}
|
|
);
|
|
|
|
};
|
|
|
|
GameLib.D3.ParticleEngine.prototype = Object.create(GameLib.Component.prototype);
|
|
GameLib.D3.ParticleEngine.prototype.constructor = GameLib.D3.ParticleEngine;
|
|
|
|
/**
|
|
* We don't use a 3rd party particle engine right now
|
|
* @returns true
|
|
*/
|
|
GameLib.D3.ParticleEngine.prototype.createInstance = function() {
|
|
|
|
this.instance = true;
|
|
|
|
// if (this.templateParticle) {
|
|
//
|
|
// this.templateParticle.mesh.position.x = this.position.x;
|
|
// this.templateParticle.mesh.position.y = this.position.y;
|
|
// this.templateParticle.mesh.position.z = this.position.z;
|
|
//
|
|
// this.templateParticle.direction.x = this.direction.x;
|
|
// this.templateParticle.direction.y = this.direction.y;
|
|
// this.templateParticle.direction.z = this.direction.z;
|
|
//
|
|
// this.templateParticle.scale.x = this.scale.x;
|
|
// this.templateParticle.scale.y = this.scale.y;
|
|
// this.templateParticle.scale.z = this.scale.z;
|
|
// }
|
|
|
|
GameLib.Component.prototype.createInstance.call(this);
|
|
};
|
|
|
|
/**
|
|
* Updates the instance with the current state
|
|
*/
|
|
GameLib.D3.ParticleEngine.prototype.updateInstance = function(property) {
|
|
|
|
if (property === 'particlesPerSecond') {
|
|
this.frequency = Number(1 / this.particlesPerSecond);
|
|
}
|
|
|
|
if (property === 'frequency') {
|
|
this.particlesPerSecond = Math.round(1 / this.frequency);
|
|
}
|
|
|
|
if (property === 'position') {
|
|
this.position.instance.x = this.position.x;
|
|
this.position.instance.y = this.position.y;
|
|
this.position.instance.z = this.position.z;
|
|
this.templateParticle.mesh.position = this.position.clone();
|
|
this.templateParticle.mesh.updateInstance('position', true);
|
|
}
|
|
|
|
if (property === 'direction') {
|
|
this.direction.instance.x = this.direction.x;
|
|
this.direction.instance.y = this.direction.y;
|
|
this.direction.instance.z = this.direction.z;
|
|
this.templateParticle.direction = this.direction.clone();
|
|
this.templateParticle.direction.updateInstance('direction', true);
|
|
}
|
|
|
|
GameLib.Component.prototype.updateInstance.call(this, property);
|
|
};
|
|
|
|
GameLib.D3.ParticleEngine.prototype.remove = function() {
|
|
|
|
if (this.removeSubscription) {
|
|
console.log('already another remove subscription for ' + this.name);
|
|
return;
|
|
}
|
|
|
|
this.removeSubscription = GameLib.Event.Subscribe(
|
|
GameLib.Event.REMOVE_PARTICLE_ENGINE,
|
|
function(data){
|
|
if (data.component === this) {
|
|
|
|
if (this.isClone) {
|
|
/**
|
|
* We only remove the things we cloned, the mesh, particle, and this
|
|
*/
|
|
GameLib.Event.Emit(
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
{
|
|
component: this.templateParticle.mesh
|
|
}
|
|
);
|
|
|
|
GameLib.Event.Emit(
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
{
|
|
component: this.templateParticle
|
|
}
|
|
);
|
|
|
|
GameLib.Event.Emit(
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
{
|
|
component: this
|
|
}
|
|
)
|
|
} else {
|
|
GameLib.Component.prototype.remove.call(this);
|
|
}
|
|
|
|
this.removeSubscription.remove();
|
|
|
|
this.removeSubscription = null;
|
|
}
|
|
}.bind(this)
|
|
);
|
|
|
|
/**
|
|
* Below signals the particle system to continue processing the particles, but don't create more, and
|
|
* we wait for the system to signal REMOVE_PARTICLE_ENGINE so we can destroy ourselves
|
|
* @type {boolean}
|
|
*/
|
|
this.disabledForRemoval = true;
|
|
};
|
|
|
|
// GameLib.D3.ParticleEngine.prototype.getChildrenComponents = function() {
|
|
//
|
|
// var components = [];
|
|
//
|
|
// if (this.templateParticle) {
|
|
// components.push(this.templateParticle);
|
|
//
|
|
// if (this.templateParticle.mesh) {
|
|
// components.push(this.templateParticle.mesh);
|
|
//
|
|
// if (this.templateParticle.mesh.materials && this.templateParticle.mesh.materials[0]) {
|
|
//
|
|
// components.push(this.templateParticle.mesh.materials[0]);
|
|
//
|
|
// if (this.templateParticle.mesh.materials[0].diffuseMap) {
|
|
// components.push(this.templateParticle.mesh.materials[0].diffuseMap);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// return components;
|
|
// };
|
|
|
|
GameLib.D3.ParticleEngine.prototype.clone = function() {
|
|
var mesh = this.templateParticle.mesh.clone();
|
|
var templateParticle = this.templateParticle.clone();
|
|
templateParticle.mesh = mesh;
|
|
templateParticle.instance = mesh.instance;
|
|
var engine = GameLib.Component.prototype.clone.call(this);
|
|
engine.templateParticle = templateParticle;
|
|
return engine;
|
|
};
|
|
|
|
GameLib.D3.ParticleEngine.prototype.processParticles = function(delta) {
|
|
// this.particles = this.particles.reduce(
|
|
// function(result, particle){
|
|
//
|
|
// particle.position.x += particle.userData.direction.x * delta;
|
|
// particle.position.y += particle.userData.direction.y * delta;
|
|
// particle.position.z += particle.userData.direction.z * delta;
|
|
//
|
|
// particle.userData.elapsed += delta;
|
|
// if (particle.userData.elapsed > particle.userData.lifeTime) {
|
|
// particle.parent.remove(particle);
|
|
// } else {
|
|
// result.push(particle);
|
|
// }
|
|
//
|
|
// return result;
|
|
// },
|
|
// []
|
|
// );
|
|
};
|
|
|
|
GameLib.D3.ParticleEngine.prototype.createNewParticle = function(camera) {
|
|
//
|
|
// var particle = this.templateParticle.clone(camera);
|
|
//
|
|
// this.particles.push(particle);
|
|
|
|
};
|
|
|
|
/**
|
|
* Converts a GameLib.D3.ParticleEngine to a new GameLib.D3.API.ParticleEngine
|
|
* @returns {GameLib.D3.API.ParticleEngine}
|
|
*/
|
|
GameLib.D3.ParticleEngine.prototype.toApiObject = function() {
|
|
|
|
return new GameLib.D3.API.ParticleEngine(
|
|
this.id,
|
|
this.name,
|
|
this.position.toApiObject(),
|
|
this.direction.toApiObject(),
|
|
this.enabled,
|
|
GameLib.Utils.IdOrNull(this.templateParticle),
|
|
this.particlesPerSecond,
|
|
this.frequency,
|
|
this.elapsed,
|
|
GameLib.Utils.IdOrNull(this.camera),
|
|
this.pulse,
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
|
);
|
|
|
|
};
|
|
|
|
/**
|
|
* Converts from an Object ParticleEngine to a GameLib.D3.ParticleEngine
|
|
* @param graphics GameLib.GraphicsRuntime
|
|
* @param objectParticleEngine Object
|
|
* @returns {GameLib.D3.ParticleEngine}
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.ParticleEngine.FromObject = function(graphics, objectParticleEngine) {
|
|
|
|
var apiParticleEngine = GameLib.D3.API.ParticleEngine.FromObject(objectParticleEngine);
|
|
|
|
return new GameLib.D3.ParticleEngine(
|
|
graphics,
|
|
apiParticleEngine
|
|
);
|
|
|
|
};
|