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

113 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-11-29 12:54:25 +01:00
/**
2016-12-05 16:40:26 +01:00
* Runtime apiVector3 for updating instance objects
2016-11-29 12:54:25 +01:00
* @param graphics GameLib.D3.Graphics
2016-12-15 14:53:39 +01:00
* @param apiVector3 GameLib.API.Vector3
* @param parentObject GameLib.*
2016-11-29 12:54:25 +01:00
* @param grain Number
* @constructor
*/
GameLib.Vector3 = function (
graphics,
apiVector3,
parentObject,
grain
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2016-11-29 12:54:25 +01:00
if (GameLib.Utils.UndefinedOrNull(apiVector3)) {
apiVector3 = {};
2016-11-29 12:54:25 +01:00
}
if (apiVector3 instanceof GameLib.Vector3) {
return apiVector3;
}
2017-01-17 17:16:10 +01:00
GameLib.API.Vector3.call(
this,
apiVector3.x,
apiVector3.y,
apiVector3.z
);
2016-11-29 12:54:25 +01:00
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
2016-11-29 12:54:25 +01:00
this.parentObject = parentObject;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(grain)) {
2016-11-29 12:54:25 +01:00
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;
2016-11-29 12:54:25 +01:00
/**
* Creates an instance vector3
* @param update
* @returns {*}
*/
2016-12-15 14:53:39 +01:00
GameLib.Vector3.prototype.createInstance = function(update) {
2016-11-29 12:54:25 +01:00
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
);
2016-11-29 12:54:25 +01:00
}
return instance;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
2016-12-15 14:53:39 +01:00
GameLib.Vector3.prototype.updateInstance = function() {
2016-11-29 12:54:25 +01:00
this.createInstance(true);
if (this.parentObject &&
this.parentObject.updateInstance) {
2016-11-29 12:54:25 +01:00
this.parentObject.updateInstance();
}
};
/**
* Converts runtime vector to API Vector
*/
2016-12-15 14:53:39 +01:00
GameLib.Vector3.prototype.toApiVector = function() {
return new GameLib.API.Vector3(
2016-11-29 12:54:25 +01:00
this.x,
this.y,
this.z
);
2017-01-02 17:05:40 +01:00
};
2017-01-04 16:12:30 +01:00
/**
* Creates a new copy of this Vector3
2017-01-04 16:12:30 +01:00
*/
GameLib.Vector3.prototype.copy = function() {
return new GameLib.Vector3(
this.graphics,
new GameLib.API.Vector3(
this.x,
this.y,
this.z
),
this.parentObject,
2017-01-04 16:12:30 +01:00
this.grain
)
};