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

92 lines
1.9 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-15 14:53:39 +01:00
* @param apiVector3 GameLib.API.Vector3
2016-11-29 12:54:25 +01:00
* @param grain Number
* @constructor
*/
2017-01-06 16:53:53 +01:00
GameLib.Vector3 = function (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-15 14:53:39 +01:00
GameLib.Utils.Extend(GameLib.Vector3, GameLib.API.Vector3);
2016-11-29 12:54:25 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
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();
};
/**
* 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 {
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-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.updateInstance) {
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
/**
* Converts runtime vector to API Vector
*/
GameLib.Vector3.prototype.copy = function() {
return new GameLib.Vector3(
this.graphics,
this.parentObject,
new GameLib.API.Vector3(
this.x,
this.y,
this.z
),
this.grain
)
};