r3-legacy/src/game-lib-d3-broadphase.js

123 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-06-23 16:04:42 +02:00
/**
* Broadphase Runtime
* @param physics GameLib.GraphicsRuntime
2017-06-23 16:04:42 +02:00
* @param apiBroadphase GameLib.D3.API.Broadphase
* @constructor
*/
GameLib.D3.Broadphase = function (
physics,
apiBroadphase
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(apiBroadphase)) {
apiBroadphase = {};
}
if (apiBroadphase instanceof GameLib.D3.Broadphase) {
return apiBroadphase;
}
GameLib.D3.API.Broadphase.call(
this,
apiBroadphase.id,
apiBroadphase.name,
apiBroadphase.broadphaseType,
apiBroadphase.parentEntity
);
GameLib.Component.call(
this,
2017-06-24 02:42:28 +02:00
GameLib.Component.COMPONENT_BROADPHASE
2017-06-23 16:04:42 +02:00
);
};
GameLib.D3.Broadphase.prototype = Object.create(GameLib.D3.API.Broadphase.prototype);
GameLib.D3.Broadphase.prototype.constructor = GameLib.D3.Broadphase;
/**
*
* @returns {*}
*/
GameLib.D3.Broadphase.prototype.createInstance = function() {
2017-06-24 02:42:28 +02:00
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) {
2017-10-23 14:52:35 +02:00
this.instance = new CANNON.NaiveBroadphase();
2017-06-24 02:42:28 +02:00
} else if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) {
2017-10-23 14:52:35 +02:00
this.instance = new CANNON.GridBroadphase();
2017-06-24 02:42:28 +02:00
} else if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) {
2017-10-23 14:52:35 +02:00
this.instance = new CANNON.SAPBroadphase();
2017-06-23 16:04:42 +02:00
} else {
console.warn('Unsupported broadphase type: ' + this.broadphaseType);
throw new Error('Unsupported broadphase type: ' + this.broadphaseType);
}
2017-10-23 14:52:35 +02:00
GameLib.Component.prototype.createInstance.call(this);
2017-06-23 16:04:42 +02:00
};
/**
*
*/
GameLib.D3.Broadphase.prototype.updateInstance = function() {
2017-06-24 02:42:28 +02:00
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) {
if (!(this.instance instanceof CANNON.NaiveBroadphase)) {
2017-10-23 14:52:35 +02:00
this.createInstance();
2017-06-24 02:42:28 +02:00
}
}
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) {
if (!(this.instance instanceof CANNON.GridBroadphase)) {
2017-10-23 14:52:35 +02:00
this.createInstance();
2017-06-24 02:42:28 +02:00
}
}
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) {
if (!(this.instance instanceof CANNON.SAPBroadphase)) {
2017-10-23 14:52:35 +02:00
this.createInstance();
2017-06-24 02:42:28 +02:00
}
}
2017-06-23 16:04:42 +02:00
};
/**
* GameLib.D3.Broadphase to GameLib.D3.API.Broadphase
* @returns {GameLib.D3.API.Broadphase}
*/
GameLib.D3.Broadphase.prototype.toApiObject = function() {
var apiBroadphase = new GameLib.D3.API.Broadphase(
this.id,
this.name,
this.broadphaseType,
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiBroadphase;
};
/**
* GameLib.D3.Broadphase from Object Broadphase
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.Broadphase}
* @constructor
*/
GameLib.D3.Broadphase.FromObject = function(graphics, objectComponent) {
var apiBroadphase = GameLib.D3.API.Broadphase.FromObject(objectComponent);
return new GameLib.D3.Broadphase(
graphics,
apiBroadphase
);
};
/**
* Broadphase Types
* @type {number}
*/
GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE = 0x1;
2017-06-24 02:42:28 +02:00
GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID = 0x2;
GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP = 0x3;