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

118 lines
3.1 KiB
JavaScript

/**
* Broadphase Runtime
* @param physics GameLib.GraphicsRuntime
* @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 = {};
}
GameLib.D3.API.Broadphase.call(
this,
apiBroadphase.id,
apiBroadphase.name,
apiBroadphase.broadphaseType,
apiBroadphase.parentEntity
);
GameLib.Component.call(this);
};
GameLib.D3.Broadphase.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.Broadphase.prototype.constructor = GameLib.D3.Broadphase;
/**
* Broadphase Types
* @type {number}
*/
GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE = 0x1;
GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID = 0x2;
GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP = 0x3;
/**
*
* @returns {*}
*/
GameLib.D3.Broadphase.prototype.createInstance = function() {
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) {
this.instance = new CANNON.NaiveBroadphase();
} else if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) {
this.instance = new CANNON.GridBroadphase();
} else if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) {
this.instance = new CANNON.SAPBroadphase();
} else {
console.warn('Unsupported broadphase type: ' + this.broadphaseType);
throw new Error('Unsupported broadphase type: ' + this.broadphaseType);
}
GameLib.Component.prototype.createInstance.call(this);
};
/**
*
*/
GameLib.D3.Broadphase.prototype.updateInstance = function(property) {
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) {
if (!(this.instance instanceof CANNON.NaiveBroadphase)) {
this.createInstance();
}
}
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) {
if (!(this.instance instanceof CANNON.GridBroadphase)) {
this.createInstance();
}
}
if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) {
if (!(this.instance instanceof CANNON.SAPBroadphase)) {
this.createInstance();
}
}
GameLib.Component.prototype.updateInstance.call(this, property);
};
/**
* 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
);
};