r3-legacy/src/game-lib-d3-custom-code.js

100 lines
2.3 KiB
JavaScript

/**
* Creates a CustomCode object
* @param apiCustomCode GameLib.D3.API.CustomCode
* @constructor
*/
GameLib.D3.CustomCode = function(
apiCustomCode
) {
if (GameLib.Utils.UndefinedOrNull(apiCustomCode)) {
apiCustomCode = {};
}
GameLib.D3.API.CustomCode.call(
this,
apiCustomCode.id,
apiCustomCode.name,
apiCustomCode.code,
apiCustomCode.domElementId,
apiCustomCode.parentEntity,
apiCustomCode.args
);
this.buildIdToObject();
this.instance = this.createInstance();
};
GameLib.D3.CustomCode.prototype = Object.create(GameLib.D3.API.CustomCode.prototype);
GameLib.D3.CustomCode.prototype.constructor = GameLib.D3.CustomCode;
/**
* Creates a camera instance of 'graphics' type (only THREE for now)
* @returns {THREE.CustomCode}
*/
GameLib.D3.CustomCode.prototype.createInstance = function(update) {
var instance = function(deltaTime) {
this.args['deltaTime'] = deltaTime;
var f = new Function(this.code).apply(this.parentEntity, this.args);
f();
};
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.CustomCode.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* Converts a GameLib.D3.CustomCode to a new GameLib.D3.API.CustomCode
* @returns {GameLib.D3.API.CustomCode}
*/
GameLib.D3.CustomCode.prototype.toApiCustomCode = function() {
var apiArgs = [];
if (this.args) {
apiArgs = this.args.map(
function(arg) {
return GameLib.Utils.IdOrNull(arg);
}
)
}
return new GameLib.D3.API.CustomCode(
this.id,
this.name,
this.code,
this.domElementId,
GameLib.Utils.IdOrNull(this.parentEntity),
apiArgs
);
};
/**
* Converts from an Object CustomCode to a GameLib.D3.CustomCode
* @param objectCustomCode Object
* @returns {GameLib.D3.CustomCode}
* @constructor
*/
GameLib.D3.CustomCode.FromObjectCustomCode = function(objectCustomCode) {
var apiCustomCode = GameLib.D3.API.CustomCode.FromObjectCustomCode(objectCustomCode);
return new GameLib.D3.CustomCode(
apiCustomCode
);
};
GameLib.D3.CustomCode.prototype.update = function(deltaTime) {
this.instance(deltaTime);
};