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

179 lines
3.8 KiB
JavaScript

/**
* Runtime apiVector3 for updating instance objects
* @param implementation GameLib.GraphicsRuntime
* @param apiVector3 GameLib.API.Vector3
* @param parentObject GameLib.*
* @param grain Number
* @constructor
*/
GameLib.Vector3 = function (
implementation,
apiVector3,
parentObject,
grain
) {
this.implementation = implementation;
if (implementation instanceof GameLib.GraphicsRuntime) {
this.physics = null;
this.graphics = implementation;
this.graphics.isNotThreeThrow();
} else if (implementation instanceof GameLib.PhysicsRuntime) {
this.graphics = null;
this.physics = implementation;
this.physics.isNotCannonThrow();
} else {
throw new Error('Unhandled implementation : ' + implementation);
}
if (GameLib.Utils.UndefinedOrNull(apiVector3)) {
apiVector3 = {};
}
GameLib.API.Vector3.call(
this,
apiVector3.x,
apiVector3.y,
apiVector3.z
);
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = this;
}
this.parentObject = parentObject;
if (GameLib.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
this.createInstance();
};
GameLib.Vector3.prototype = Object.create(GameLib.API.Vector3.prototype);
GameLib.Vector3.prototype.constructor = GameLib.Vector3;
/**
* Creates an instance vector3
* @returns {*}
*/
GameLib.Vector3.prototype.createInstance = function() {
if (this.graphics) {
this.instance = new THREE.Vector3(
this.x,
this.y,
this.z
);
} else if (this.physics) {
this.instance = new CANNON.Vec3(
this.x,
this.y,
this.z
)
}
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
GameLib.Vector3.prototype.updateInstance = function(property, preventParentUpdate) {
this.instance.x = this.x;
this.instance.y = this.y;
this.instance.z = this.z;
if (!preventParentUpdate &&
this.parentObject &&
this.parentObject !== this &&
this.parentObject.updateInstance) {
this.parentObject.updateInstance(property);
}
};
/**
* Converts runtime vector to API Vector
*/
GameLib.Vector3.prototype.toApiObject = function() {
return new GameLib.API.Vector3(
this.x,
this.y,
this.z
);
};
/**
* Creates a new copy of this Vector3
*/
GameLib.Vector3.prototype.copy = function() {
return new GameLib.Vector3(
this.implementation,
new GameLib.API.Vector3(
this.x,
this.y,
this.z
),
this.parentObject,
this.grain
)
};
GameLib.Vector3.prototype.clone = function() {
return new GameLib.Vector3(
this.implementation,
new GameLib.API.Vector3(
this.x,
this.y,
this.z
),
this.parentObject,
this.grain
)
};
/**
* Create a negative version of this vector
* @returns {GameLib.Vector3}
*/
GameLib.Vector3.prototype.negativeCopy = function() {
return new GameLib.Vector3(
this.implementation,
new GameLib.API.Vector3(
-this.x,
-this.y,
-this.z
),
this.parentObject,
this.grain
)
};
/**
* Applies rotation specified by axis / angle to this vector - its a wrapper for three..
* @param axis
* @param angle
*/
GameLib.Vector3.prototype.applyAxisAngle = function(axis, angle) {
this.instance.applyAxisAngle(
new THREE.Vector3(
axis.x,
axis.y,
axis.z
),
angle
);
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
};
GameLib.Vector3.prototype.setFrom = function(vector3) {
this.x = vector3.x;
this.y = vector3.y;
this.z = vector3.z;
};