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

75 lines
1.6 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
* @param parentObject GameLib.D3.*
2016-12-05 16:40:26 +01:00
* @param apiVector3 GameLib.D3.API.Vector3
2016-11-29 12:54:25 +01:00
* @param grain Number
* @constructor
*/
2016-12-05 16:40:26 +01:00
GameLib.D3.Vector3 = function RuntimeVector3(graphics, parentObject, apiVector3, grain) {
2016-11-29 12:54:25 +01:00
2016-12-05 16:40:26 +01:00
for (var property in apiVector3) {
if (apiVector3.hasOwnProperty(property)) {
this[property] = apiVector3[property];
2016-11-29 12:54:25 +01:00
}
}
2016-12-02 16:03:03 +01:00
GameLib.D3.Utils.Extend(GameLib.D3.Vector3, GameLib.D3.API.Vector3);
2016-11-29 12:54:25 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.parentObject = parentObject;
if (GameLib.D3.Utils.UndefinedOrNull(grain)) {
grain = 0.001;
}
this.grain = grain;
this.instance = this.createInstance();
};
/**
* Creates an instance vector3
* @param update
* @returns {*}
*/
2016-12-02 16:03:03 +01:00
GameLib.D3.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 {
2016-12-05 16:40:26 +01:00
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-02 16:03:03 +01:00
GameLib.D3.Vector3.prototype.updateInstance = function() {
2016-11-29 12:54:25 +01:00
this.createInstance(true);
if (this.parentObject.updateInstance) {
this.parentObject.updateInstance();
}
};
/**
* Converts runtime vector to API Vector
*/
2016-12-02 16:03:03 +01:00
GameLib.D3.Vector3.prototype.toApiVector = function() {
2016-12-01 18:37:57 +01:00
return new GameLib.D3.API.Vector3(
2016-11-29 12:54:25 +01:00
this.x,
this.y,
this.z
);
};