2016-11-29 12:54:25 +01:00
|
|
|
/**
|
2016-12-05 16:40:26 +01:00
|
|
|
* Runtime apiVector3 for updating instance objects
|
2017-06-24 02:42:28 +02:00
|
|
|
* @param implementation GameLib.D3.Graphics
|
2016-12-15 14:53:39 +01:00
|
|
|
* @param apiVector3 GameLib.API.Vector3
|
2017-01-17 13:24:45 +01:00
|
|
|
* @param parentObject GameLib.*
|
2016-11-29 12:54:25 +01:00
|
|
|
* @param grain Number
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-01-17 13:24:45 +01:00
|
|
|
GameLib.Vector3 = function (
|
2017-06-24 02:42:28 +02:00
|
|
|
implementation,
|
2017-01-17 13:24:45 +01:00
|
|
|
apiVector3,
|
|
|
|
parentObject,
|
|
|
|
grain
|
|
|
|
) {
|
2017-06-24 02:42:28 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2016-11-29 12:54:25 +01:00
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiVector3)) {
|
|
|
|
apiVector3 = {};
|
2016-11-29 12:54:25 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiVector3 instanceof GameLib.Vector3) {
|
|
|
|
return apiVector3;
|
|
|
|
}
|
|
|
|
|
2017-01-17 17:16:10 +01:00
|
|
|
GameLib.API.Vector3.call(
|
2017-01-17 13:24:45 +01:00
|
|
|
this,
|
|
|
|
apiVector3.x,
|
|
|
|
apiVector3.y,
|
|
|
|
apiVector3.z
|
|
|
|
);
|
2016-11-29 12:54:25 +01:00
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
|
2017-09-14 05:42:38 +02:00
|
|
|
parentObject = this;
|
2017-01-17 13:24:45 +01:00
|
|
|
}
|
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();
|
|
|
|
};
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
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
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-06-24 02:42:28 +02:00
|
|
|
GameLib.Vector3.prototype.createInstance = function() {
|
2016-11-29 12:54:25 +01:00
|
|
|
|
|
|
|
var instance = null;
|
|
|
|
|
2017-06-24 02:42:28 +02:00
|
|
|
if (this.graphics) {
|
|
|
|
instance = new THREE.Vector3(
|
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
this.z
|
|
|
|
);
|
|
|
|
} else if (this.physics) {
|
|
|
|
instance = new CANNON.Vec3(
|
2017-01-17 13:24:45 +01:00
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
this.z
|
2017-06-24 02:42:28 +02:00
|
|
|
)
|
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
|
|
|
|
2017-06-24 02:42:28 +02:00
|
|
|
this.instance.x = this.x;
|
|
|
|
this.instance.y = this.y;
|
|
|
|
this.instance.z = this.z;
|
2016-11-29 12:54:25 +01:00
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (this.parentObject &&
|
2017-09-14 05:42:38 +02:00
|
|
|
this.parentObject !== this &&
|
2017-01-17 13:24:45 +01:00
|
|
|
this.parentObject.updateInstance) {
|
2016-11-29 12:54:25 +01:00
|
|
|
this.parentObject.updateInstance();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts runtime vector to API Vector
|
|
|
|
*/
|
2017-05-16 14:51:57 +02:00
|
|
|
GameLib.Vector3.prototype.toApiObject = function() {
|
2016-12-15 14:53:39 +01:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2017-01-17 13:24:45 +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(
|
2017-06-24 02:42:28 +02:00
|
|
|
this.implementation,
|
2017-01-04 16:12:30 +01:00
|
|
|
new GameLib.API.Vector3(
|
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
this.z
|
|
|
|
),
|
2017-01-17 13:24:45 +01:00
|
|
|
this.parentObject,
|
2017-01-04 16:12:30 +01:00
|
|
|
this.grain
|
|
|
|
)
|
|
|
|
};
|
2017-09-11 17:47:32 +02:00
|
|
|
|
|
|
|
GameLib.Vector3.prototype.setFrom = function(vector3) {
|
|
|
|
this.x = vector3.x;
|
|
|
|
this.y = vector3.y;
|
|
|
|
this.z = vector3.z;
|
|
|
|
};
|