r3-legacy/src/game-lib-vector4.js

93 lines
1.9 KiB
JavaScript

/**
* Runtime apiVector4 for updating instance objects
* @param graphics GameLib.D3.Graphics
* @param apiVector4 GameLib.API.Vector4
* @param parentObject GameLib.*
* @param grain Number
* @constructor
*/
GameLib.Vector4 = function (
graphics,
apiVector4,
parentObject,
grain
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiVector4)) {
apiVector4 = {};
}
GameLib.API.Vector4.call(
this,
apiVector4.x,
apiVector4.y,
apiVector4.z,
apiVector4.w
);
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
if (GameLib.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
this.instance = this.createInstance();
};
GameLib.Vector4.prototype = Object.create(GameLib.API.Vector4.prototype);
GameLib.Vector4.prototype.constructor = GameLib.Vector4;
/**
* Creates an instance vector3
* @param update
* @returns {*}
*/
GameLib.Vector4.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
instance.x = this.x;
instance.y = this.y;
instance.z = this.z;
instance.w = this.w;
} else {
instance = new THREE.Quaternion(this.x, this.y, this.z, this.w);
}
return instance;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
GameLib.Vector4.prototype.updateInstance = function() {
this.createInstance(true);
if (this.parentObject &&
this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
};
/**
* Converts runtime vector to API Vector
*/
GameLib.Vector4.prototype.toApiVector = function() {
return new GameLib.API.Vector4(
this.x,
this.y,
this.z,
this.w
);
};