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

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-12-01 18:37:57 +01:00
/**
* Helpers for displaying outlines or making 'invisible' scene objects visible
* @param id
* @param object GameLib.D3.Object
* @param name
* @param graphics
* @param helperType
* @constructor
*/
GameLib.D3.Helper = function Helper(
id,
object,
helperType,
name,
graphics
) {
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
2016-12-01 18:37:57 +01:00
}
this.id = id;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(object)) {
2016-12-01 18:37:57 +01:00
throw new Error('Cannot create helpers for unknown objects');
}
this.object = object;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(helperType)) {
2016-12-01 18:37:57 +01:00
helperType = GameLib.D3.Helper.HELPER_TYPE_EDGES;
}
this.helperType = helperType;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(name)) {
2016-12-01 18:37:57 +01:00
name = 'Helper (' + this.helperType + ')';
}
this.name = name;
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* Helper types
* @type {string}
*/
GameLib.D3.Helper.HELPER_TYPE_EDGES = 'edges';
GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT = 'directional-light';
GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT = 'spot-light';
GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT = 'point-light';
GameLib.D3.Helper.HELPER_TYPE_WIREFRAME = 'wireframe';
/**
* Creates a helper instance
* @param update
*/
GameLib.D3.Helper.prototype.createInstance = function(update) {
var instance = null;
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_EDGES) {
instance = new this.graphics.instance.EdgesHelper(this.object.instance, 0x007700);
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) {
instance = new this.graphics.instance.DirectionalLightHelper(this.object.instance);
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) {
instance = new this.graphics.instance.PointLightHelper(this.object.instance, 1);
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) {
instance = new this.graphics.instance.SpotLightHelper(this.object.instance);
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) {
instance = new this.graphics.instance.WireframeHelper(this.object.instance, 0x007700);
}
if (!instance) {
throw new Error('Unsupported helper type: ' + this.helperType);
}
if (update) {
this.instance = instance;
}
return instance;
};