r3-legacy/src/game-lib-d3-shadow-a.js

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-01-29 12:42:46 +01:00
/**
* GameLib.D3.Shadow
* @param graphics GameLib.GraphicsRuntime
* @param apiShadow GameLib.D3.API.Shadow
* @constructor
*/
GameLib.D3.Shadow = function(
graphics,
apiShadow
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiShadow)) {
apiShadow = {
shadowType : GameLib.D3.API.Shadow.SHADOW_TYPE_NORMAL
};
}
GameLib.D3.API.Shadow.call(
this,
apiShadow.id,
apiShadow.name,
apiShadow.shadowType,
apiShadow.camera,
apiShadow.bias,
apiShadow.mapSize,
apiShadow.radius,
apiShadow.parentEntity
);
if (this.camera instanceof GameLib.D3.API.Camera.Perspective) {
this.camera = new GameLib.D3.Camera.Perspective(
this.graphics,
this.camera
)
}
this.mapSize = new GameLib.Vector2(
this.graphics,
this.mapSize,
this
);
GameLib.Component.call(
this,
{
camera : GameLib.D3.Camera
}
);
2018-01-29 12:42:46 +01:00
};
GameLib.D3.Shadow.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.Shadow.prototype.constructor = GameLib.D3.Shadow;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Shadow.prototype.createInstance = function() {
/**
* Shadow objects are not responsible for creating their instances right now -
* They are implicitly created through threejs light instances. This means,
* they only update their instances.
* @type {Object}
*/
this.instance = {};
GameLib.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Shadow.prototype.updateInstance = function(property) {
if (typeof this.instance !== 'object') {
console.warn('this shadow instance is not ready for an update: ' + this.name);
return;
}
if (GameLib.Utils.UndefinedOrNull(property)) {
console.warn('no property for light: ' + this.name);
}
if (property === 'shadowType') {
console.warn('changing shadowType has no effect');
}
if (property === 'camera') {
this.instance.camera = this.camera.instance;
}
if (property === 'bias') {
this.instance.bias = this.bias;
}
if (property === 'mapSize') {
this.instance.mapSize.x = this.mapSize.x;
this.instance.mapSize.y = this.mapSize.y;
}
if (property === 'radius') {
this.instance.radius = this.radius;
}
2018-02-08 13:57:15 +01:00
GameLib.Component.prototype.updateInstance.call(this, property);
2018-01-29 12:42:46 +01:00
};
/**
* Converts a GameLib.D3.Shadow to a GameLib.D3.API.Shadow
* @returns {GameLib.D3.API.Shadow}
*/
GameLib.D3.Shadow.prototype.toApiObject = function() {
return new GameLib.D3.API.Shadow(
this.id,
this.name,
this.shadowType,
GameLib.Utils.IdOrNull(this.camera),
this.bias,
this.mapSize.toApiObject(),
this.radius,
GameLib.Utils.IdOrNull(this.parentEntity)
);
};
GameLib.D3.Shadow.prototype.updateFromInstance = function() {
this.bias = this.instance.bias;
this.mapSize.x = this.instance.mapSize.x;
this.mapSize.y = this.instance.mapSize.y;
2018-01-29 12:42:46 +01:00
this.radius = this.instance.radius;
this.camera.instance = this.instance.camera;
this.camera.updateFromInstance();
};