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