r3-legacy/src/game-lib-d3-particle.js

198 lines
5.4 KiB
JavaScript
Raw Normal View History

/**
* Creates a Particle object
* @param graphics GameLib.D3.Graphics
* @param apiParticle GameLib.D3.API.Particle
* @constructor
*/
GameLib.D3.Particle = function(
graphics,
apiParticle
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiParticle)) {
apiParticle = {};
}
if (apiParticle instanceof GameLib.D3.Particle) {
return apiParticle;
}
GameLib.D3.API.Particle.call(
this,
apiParticle.id,
apiParticle.name,
apiParticle.lifeTime,
apiParticle.elapsed,
apiParticle.mesh,
apiParticle.positionOffsetType,
apiParticle.positionOffset,
apiParticle.positionOffsetFn,
apiParticle.directionType,
apiParticle.direction,
apiParticle.directionFn,
apiParticle.scaleType,
apiParticle.scale,
apiParticle.scaleFn,
apiParticle.rotationType,
apiParticle.rotation,
apiParticle.rotationFn,
apiParticle.parentEngine,
apiParticle.parentEntity
);
if (this.mesh instanceof GameLib.D3.API.Mesh) {
this.mesh = new GameLib.D3.Mesh(
graphics,
this.mesh
)
}
if (this.positionOffset instanceof GameLib.API.Vector3) {
this.positionOffset = new GameLib.Vector3(
graphics,
this.positionOffset,
this
);
} else {
console.warn('positionOffset not instance of API.Vector3');
throw new Error('positionOffset not instance of API.Vector3');
}
if (this.direction instanceof GameLib.API.Vector3) {
this.direction = new GameLib.Vector3(
graphics,
this.direction,
this
);
} else {
console.warn('direction not instance of API.Vector3');
throw new Error('direction not instance of API.Vector3');
}
if (this.scale instanceof GameLib.API.Vector3) {
this.scale = new GameLib.Vector3(
graphics,
this.scale,
this
);
} else {
console.warn('scale not instance of API.Vector3');
throw new Error('scale not instance of API.Vector3');
}
if (this.rotation instanceof GameLib.API.Vector3) {
this.rotation = new GameLib.Vector3(
graphics,
this.rotation,
this
);
} else {
console.warn('rotation not instance of API.Vector3');
throw new Error('rotation not instance of API.Vector3');
}
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_PARTICLE,
{
mesh : GameLib.D3.Mesh,
parentEngine : GameLib.D3.ParticleEngine
}
);
};
GameLib.D3.Particle.prototype = Object.create(GameLib.D3.API.Particle.prototype);
GameLib.D3.Particle.prototype.constructor = GameLib.D3.Particle;
GameLib.D3.Particle.POSITION_OFFSET_TYPE_CONSTANT = 0x1;
GameLib.D3.Particle.POSITION_OFFSET_TYPE_RANDOM = 0x2;
GameLib.D3.Particle.POSITION_OFFSET_TYPE_FUNCTION = 0x3;
GameLib.D3.Particle.DIRECTION_TYPE_CONSTANT = 0x1;
GameLib.D3.Particle.DIRECTION_TYPE_RANDOM = 0x2;
GameLib.D3.Particle.DIRECTION_TYPE_FUNCTION = 0x3;
GameLib.D3.Particle.SCALE_TYPE_CONSTANT = 0x1;
GameLib.D3.Particle.SCALE_TYPE_LINEAR = 0x2;
GameLib.D3.Particle.SCALE_TYPE_EXPONENTIAL = 0x3;
GameLib.D3.Particle.SCALE_TYPE_RANDOM = 0x4;
GameLib.D3.Particle.SCALE_TYPE_FUNCTION = 0x5;
GameLib.D3.Particle.ROTATION_TYPE_CONSTANT = 0x1;
GameLib.D3.Particle.ROTATION_TYPE_RANDOM = 0x2;
GameLib.D3.Particle.ROTATION_TYPE_FUNCTION = 0x3;
/**
* Particle create instance
*/
GameLib.D3.Particle.prototype.createInstance = function() {
if (!this.mesh) {
console.warn('no mesh to clone from - failed dependency check?');
return
}
this.instance = true;
GameLib.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Particle.prototype.updateInstance = function(property) {
console.log('Update Particle Property : ' + property);
};
/**
* Converts a GameLib.D3.Particle to a new GameLib.D3.API.Particle
* @returns {GameLib.D3.API.Particle}
*/
GameLib.D3.Particle.prototype.toApiObject = function() {
return new GameLib.D3.API.Particle(
this.id,
this.name,
this.lifeTime,
this.elapsed,
GameLib.Utils.IdOrNull(this.mesh),
this.positionOffsetType,
this.positionOffset.toApiObject(),
this.positionOffsetFn,
this.directionType,
this.direction.toApiObject(),
this.directionFn,
this.scaleType,
this.scale.toApiObject(),
this.scaleFn,
this.rotationType,
this.rotation.toApiObject(),
this.rotationFn,
GameLib.Utils.IdOrNull(this.parentEngine),
GameLib.Utils.IdOrNull(this.parentEntity)
);
};
/**
* Converts from an Object Particle to a GameLib.D3.Particle
* @param graphics GameLib.D3.Graphics
* @param objectParticle Object
* @returns {GameLib.D3.Particle}
* @constructor
*/
GameLib.D3.Particle.FromObject = function(graphics, objectParticle) {
var apiParticle = GameLib.D3.API.Particle.FromObject(objectParticle);
return new GameLib.D3.Particle(
graphics,
apiParticle
);
};