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

172 lines
3.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
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
* @param parentObject GameLib.*
2016-11-29 12:54:25 +01:00
* @param grain Number
* @constructor
*/
GameLib.Vector3 = function (
2017-06-24 02:42:28 +02:00
implementation,
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
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 = this;
}
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
* @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(
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
if (this.parentObject &&
this.parentObject !== this &&
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
/**
* 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
),
this.parentObject,
2017-01-04 16:12:30 +01:00
this.grain
)
};
2017-09-25 21:55:39 +02:00
/**
* Create a negative version of this vector
* @returns {GameLib.Vector3}
*/
GameLib.Vector3.prototype.negativeCopy = function() {
return new GameLib.Vector3(
this.implementation,
new GameLib.API.Vector3(
-this.x,
-this.y,
-this.z
),
this.parentObject,
this.grain
)
};
/**
* Applies rotation specified by axis / angle to this vector - its a wrapper for three..
* @param axis
* @param angle
*/
GameLib.Vector3.prototype.applyAxisAngle = function(axis, angle) {
this.instance.applyAxisAngle(
new THREE.Vector3(
axis.x,
axis.y,
axis.z
),
angle
);
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
};
GameLib.Vector3.prototype.setFrom = function(vector3) {
this.x = vector3.x;
this.y = vector3.y;
this.z = vector3.z;
};