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

127 lines
2.8 KiB
JavaScript

/**
* Solver Runtime
* @param physics GameLib.D3.Graphics
* @param apiSolver GameLib.D3.API.Solver
* @constructor
*/
GameLib.D3.Solver = function (
physics,
apiSolver
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(apiSolver)) {
apiSolver = {};
}
if (apiSolver instanceof GameLib.D3.Solver) {
return apiSolver;
}
GameLib.D3.API.Solver.call(
this,
apiSolver.id,
apiSolver.name,
apiSolver.solverType,
apiSolver.iterations,
apiSolver.tolerance,
apiSolver.parentEntity
);
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_SOLVER
);
};
GameLib.D3.Solver.prototype = Object.create(GameLib.D3.API.Solver.prototype);
GameLib.D3.Solver.prototype.constructor = GameLib.D3.Solver;
/**
*
* @returns {*}
*/
GameLib.D3.Solver.prototype.createInstance = function() {
var instance = null;
if (this.solverType === GameLib.D3.Solver.GS_SOLVER) {
instance = new CANNON.GSSolver();
} else if (this.solverType === GameLib.D3.Solver.SPLIT_SOLVER) {
instance = new CANNON.SplitSolver();
} else {
console.warn('Unsupported solver type: ' + this.solverType);
throw new Error('Unsupported solver type: ' + this.solverType);
}
instance.tolerance = this.tolerance;
instance.iterations = this.iterations;
return instance;
};
/**
*
*/
GameLib.D3.Solver.prototype.updateInstance = function() {
if (this.solverType === GameLib.D3.Solver.GS_SOLVER) {
if (!(this.instance instanceof CANNON.GSSolver)) {
this.instance = new CANNON.GSSolver();
}
}
if (this.solverType === GameLib.D3.Solver.SPLIT_SOLVER) {
if (!(this.instance instanceof CANNON.SplitSolver)) {
this.instance = new CANNON.SplitSolver();
}
}
this.instance.iterations = this.iterations;
this.instance.tolerance = this.tolerance;
};
/**
* GameLib.D3.Solver to GameLib.D3.API.Solver
* @returns {GameLib.D3.API.Solver}
*/
GameLib.D3.Solver.prototype.toApiObject = function() {
var apiSolver = new GameLib.D3.API.Solver(
this.id,
this.name,
this.solverType,
this.iterations,
this.tolerance,
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiSolver;
};
/**
* GameLib.D3.Solver from Object Solver
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.Solver}
* @constructor
*/
GameLib.D3.Solver.FromObject = function(graphics, objectComponent) {
var apiSolver = GameLib.D3.API.Solver.FromObject(objectComponent);
return new GameLib.D3.Solver(
graphics,
apiSolver
);
};
/**
* Solver Types
* @type {number}
*/
GameLib.D3.Solver.GS_SOLVER = 0x1;
GameLib.D3.Solver.SPLIT_SOLVER = 0x2;