/** * Runtime apiVector3 for updating instance objects * @param implementation GameLib.D3.Graphics * @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.D3.Graphics) { this.physics = null; this.graphics = implementation; this.graphics.isNotThreeThrow(); } else if (implementation instanceof GameLib.D3.Physics) { this.graphics = null; this.physics = implementation; this.physics.isNotCannonThrow(); } else { throw new Error('Unhandled implementation : ' + implementation); } if (GameLib.Utils.UndefinedOrNull(apiVector3)) { apiVector3 = {}; } if (apiVector3 instanceof GameLib.Vector3) { return 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.instance = 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() { var instance = null; if (this.graphics) { instance = new THREE.Vector3( this.x, this.y, this.z ); } else if (this.physics) { instance = new CANNON.Vec3( this.x, this.y, this.z ) } return instance; }; /** * Updates the instance vector, calls updateInstance on the parent object */ GameLib.Vector3.prototype.updateInstance = function() { this.instance.x = this.x; this.instance.y = this.y; this.instance.z = this.z; if (this.parentObject && this.parentObject !== this && this.parentObject.updateInstance) { this.parentObject.updateInstance(); } }; /** * 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.setFrom = function(vector3) { this.x = vector3.x; this.y = vector3.y; this.z = vector3.z; };