r3-legacy/src/game-lib-light.js

128 lines
3.5 KiB
JavaScript

/**
* Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created
* @param graphics GameLib.D3.Graphics
* @param apiLight GameLib.D3.API.Light
* @constructor
*/
GameLib.D3.Light = function Light(
graphics,
apiLight
) {
for (var property in apiLight) {
if (apiLight.hasOwnProperty(property)) {
this[property] = apiLight[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* Light Types
* @type {number}
*/
GameLib.D3.Light.LIGHT_TYPE_AMBIENT = 0x1;
GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL = 0x2;
GameLib.D3.Light.LIGHT_TYPE_POINT = 0x3;
GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Light.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
}
if ((!instance) ||
(instance && instance.lightType != this.lightType)) {
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) {
instance = new this.graphics.instance.AmbientLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
instance = new this.graphics.instance.DirectionalLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
instance = new this.graphics.instance.PointLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
instance.distance = this.distance;
instance.decay = this.decay;
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) {
instance = new this.graphics.instance.SpotLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
}
}
if (!instance) {
console.warn('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?');
throw new Error('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?');
}
instance.gameLibObject = this;
instance.name = this.name;
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
instance.rotation.x = this.rotation.x;
instance.rotation.y = this.rotation.y;
instance.rotation.z = this.rotation.z;
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Light.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.Light.prototype.clone = function() {
return _.cloneDeep(this);
};