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

128 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-12-01 18:37:57 +01:00
/**
* Helpers for displaying outlines or making 'invisible' scene objects visible
2017-01-12 17:40:17 +01:00
* @param graphics GameLib.D3.Graphics
* @param apiHelper GameLib.D3.API.Helper
2016-12-01 18:37:57 +01:00
* @constructor
*/
GameLib.D3.Helper = function Helper(
2017-01-12 17:40:17 +01:00
graphics,
apiHelper
2016-12-01 18:37:57 +01:00
) {
2017-01-12 17:40:17 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2016-12-01 18:37:57 +01:00
2017-01-12 17:40:17 +01:00
if (GameLib.Utils.UndefinedOrNull(apiHelper)) {
apiHelper = {};
2016-12-01 18:37:57 +01:00
}
2017-01-12 17:40:17 +01:00
GameLib.D3.API.Helper.call(
this,
apiHelper.id,
apiHelper.name,
apiHelper.helperType,
apiHelper.object,
apiHelper.parentEntity
);
2016-12-01 18:37:57 +01:00
this.instance = this.createInstance();
};
2017-01-12 17:40:17 +01:00
GameLib.D3.Helper.prototype = Object.create(GameLib.D3.API.Helper.prototype);
GameLib.D3.Helper.prototype.constructor = GameLib.D3.Helper;
2016-12-01 18:37:57 +01:00
/**
* 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';
2016-12-23 16:07:10 +01:00
GameLib.D3.Helper.HELPER_TYPE_SKELETON = 'skeleton';
2016-12-01 18:37:57 +01:00
/**
* Creates a helper instance
* @param update
*/
GameLib.D3.Helper.prototype.createInstance = function(update) {
var instance = null;
2017-01-12 17:40:17 +01:00
if (update) {
instance = this.instance;
}
2016-12-01 18:37:57 +01:00
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_EDGES) {
2017-01-04 16:12:30 +01:00
instance = new THREE.WireframeHelper(this.object.instance, 0x007700);
2016-12-01 18:37:57 +01:00
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) {
2016-12-23 16:07:10 +01:00
instance = new THREE.DirectionalLightHelper(this.object.instance);
2016-12-01 18:37:57 +01:00
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) {
2016-12-23 16:07:10 +01:00
instance = new THREE.PointLightHelper(this.object.instance, 1);
2016-12-01 18:37:57 +01:00
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) {
2016-12-23 16:07:10 +01:00
instance = new THREE.SpotLightHelper(this.object.instance);
2016-12-01 18:37:57 +01:00
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) {
2016-12-23 16:07:10 +01:00
instance = new THREE.WireframeHelper(this.object.instance, 0x007700);
}
if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SKELETON) {
2017-01-12 17:40:17 +01:00
instance = new THREE.SkeletonHelper(this.object.instance);
2016-12-01 18:37:57 +01:00
}
if (!instance) {
throw new Error('Unsupported helper type: ' + this.helperType);
}
return instance;
};
2017-01-12 17:40:17 +01:00
/**
* Updates the instance with the current state
*/
GameLib.D3.Helper.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* Converts a GameLib.D3.Helper to a new GameLib.D3.API.Helper
* @returns {GameLib.D3.API.Helper}
*/
GameLib.D3.Helper.prototype.toApiHelper = function() {
return new GameLib.D3.API.Helper(
this.id,
this.name,
this.helperType,
GameLib.Utils.IdOrNull(this.object),
GameLib.Utils.IdOrNull(this.parentEntity)
);
};
/**
* Converts from an Object Helper to a GameLib.D3.Helper
* @param graphics GameLib.D3.Graphics
* @param objectHelper Object
* @returns {GameLib.D3.Helper}
* @constructor
*/
GameLib.D3.Helper.FromObjectHelper = function(graphics, objectHelper) {
var apiHelper = GameLib.D3.API.Helper.FromObjectHelper(objectHelper);
return new GameLib.D3.Helper(
graphics,
apiHelper
);
};