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

104 lines
2.4 KiB
JavaScript

/**
* Creates a CustomCode object
* @param apiCustomCode GameLib.D3.API.CustomCode
* @constructor
*/
GameLib.D3.CustomCode = function(
apiCustomCode
) {
if (GameLib.Utils.UndefinedOrNull(apiCustomCode)) {
apiCustomCode = {};
}
if (apiCustomCode instanceof GameLib.D3.CustomCode) {
return apiCustomCode;
}
GameLib.D3.API.CustomCode.call(
this,
apiCustomCode.id,
apiCustomCode.name,
apiCustomCode.parentEntity,
apiCustomCode.code,
apiCustomCode.domElementId,
apiCustomCode.args
);
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_CUSTOM_CODE,
{
'args' : [GameLib.Component]
}
);
};
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.toApiObject = function() {
var apiArgs = this.args.map(
function(arg) {
return GameLib.Utils.IdOrNull(arg);
}
);
return new GameLib.D3.API.CustomCode(
this.id,
this.name,
GameLib.Utils.IdOrNull(this.parentEntity),
this.code,
this.domElementId,
apiArgs
);
};
/**
* Converts from an Object CustomCode to a GameLib.D3.CustomCode
* @param objectCustomCode Object
* @returns {GameLib.D3.CustomCode}
* @constructor
*/
GameLib.D3.CustomCode.FromObject = function(objectCustomCode) {
var apiCustomCode = GameLib.D3.API.CustomCode.FromObject(objectCustomCode);
return new GameLib.D3.CustomCode(
apiCustomCode
);
};
GameLib.D3.CustomCode.prototype.update = function(deltaTime) {
this.instance(deltaTime);
};