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

124 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-06-24 02:42:28 +02:00
/**
* 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() {
if (this.solverType === GameLib.D3.Solver.GS_SOLVER) {
2017-10-23 14:52:35 +02:00
this.instance = new CANNON.GSSolver();
2017-06-24 02:42:28 +02:00
} else if (this.solverType === GameLib.D3.Solver.SPLIT_SOLVER) {
2017-10-23 14:52:35 +02:00
this.instance = new CANNON.SplitSolver();
2017-06-24 02:42:28 +02:00
} else {
2017-10-23 14:52:35 +02:00
throw new Error('unsupported solver type: ' + this.solverType);
2017-06-24 02:42:28 +02:00
}
2017-10-23 14:52:35 +02:00
this.instance.tolerance = this.tolerance;
this.instance.iterations = this.iterations;
2017-06-24 02:42:28 +02:00
2017-10-23 14:52:35 +02:00
GameLib.Component.prototype.createInstance.call(this);
2017-06-24 02:42:28 +02:00
};
/**
*
*/
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;