World now takes a config object. Cleaner and many more options now supported.

This commit is contained in:
Richard Davey 2017-08-16 22:51:46 +01:00
parent 579489d47a
commit 67cba936da
3 changed files with 25 additions and 23 deletions

View file

@ -39,13 +39,13 @@ var Body = new Class({
this.vel = { x: 0, y: 0 }; this.vel = { x: 0, y: 0 };
this.accel = { x: 0, y: 0 }; this.accel = { x: 0, y: 0 };
this.friction = { x: 0, y: 0 }; this.friction = { x: 0, y: 0 };
this.maxVel = { x: 100, y: 100 }; this.maxVel = { x: world.defaults.maxVelocityX, y: world.defaults.maxVelocityY };
this.standing = false; this.standing = false;
this.gravityFactor = 1; this.gravityFactor = world.defaults.gravityFactor;
this.bounciness = 0; this.bounciness = world.defaults.bounciness;
this.minBounceVelocity = 40; this.minBounceVelocity = world.defaults.minBounceVelocity;
this.accelGround = 0; this.accelGround = 0;
this.accelAir = 0; this.accelAir = 0;
@ -57,7 +57,7 @@ var Body = new Class({
this.debugShowBody = true; this.debugShowBody = true;
this.debugShowVelocity = true; this.debugShowVelocity = true;
this.debugBodyColor = world.debugColors.body; this.debugBodyColor = world.defaults.bodyDebugColor;
// min 44 deg, max 136 deg // min 44 deg, max 136 deg
this.slopeStanding = { min: 0.767944870877505, max: 2.3736477827122884 }; this.slopeStanding = { min: 0.767944870877505, max: 2.3736477827122884 };
@ -131,7 +131,7 @@ var Body = new Class({
var x = pos.x + this.size.x / 2; var x = pos.x + this.size.x / 2;
var y = pos.y + this.size.y / 2; var y = pos.y + this.size.y / 2;
graphic.lineStyle(1, this.world.debugColors.velocity, 1); graphic.lineStyle(1, this.world.defaults.velocityDebugColor, 1);
graphic.lineBetween(x, y, x + this.vel.x, y + this.vel.y); graphic.lineBetween(x, y, x + this.vel.x, y + this.vel.y);
} }
} }

View file

@ -4,6 +4,7 @@ var Body = require('./Body');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var COLLIDES = require('./COLLIDES'); var COLLIDES = require('./COLLIDES');
var CollisionMap = require('./CollisionMap'); var CollisionMap = require('./CollisionMap');
var GetFastValue = require('../../utils/object/GetFastValue');
var Set = require('../../structs/Set'); var Set = require('../../structs/Set');
var Solver = require('./Solver'); var Solver = require('./Solver');
var TYPE = require('./TYPE'); var TYPE = require('./TYPE');
@ -12,46 +13,47 @@ var World = new Class({
initialize: initialize:
function World (scene, gravity, cellSize, showDebug) function World (scene, config)
{ {
if (gravity === undefined) { gravity = 0; }
if (cellSize === undefined) { cellSize = 64; }
if (showDebug === undefined) { showDebug = false; }
this.scene = scene; this.scene = scene;
this.events = scene.sys.events; this.events = scene.sys.events;
this.bodies = new Set(); this.bodies = new Set();
this.gravity = gravity; this.gravity = GetFastValue(config, 'gravity', 0);
// Spatial hash cell dimensions // Spatial hash cell dimensions
this.cellSize = cellSize; this.cellSize = GetFastValue(config, 'cellSize', 64);
this.collisionMap = new CollisionMap(); this.collisionMap = new CollisionMap();
this.delta = 0; this.timeScale = GetFastValue(config, 'timeScale', 1);
this.timeScale = 1;
// Impacts maximum time step is 20 fps. // Impacts maximum time step is 20 fps.
this.maxStep = 0.05; this.maxStep = GetFastValue(config, 'maxStep', 0.05);
this.enabled = true; this.enabled = true;
this.drawDebug = false; this.drawDebug = GetFastValue(config, 'debug', false);
this.debugGraphic; this.debugGraphic;
this.debugColors = { this.defaults = {
body: 0xff00ff, bodyDebugColor: GetFastValue(config, 'debugBodyColor', 0xff00ff),
velocity: 0x00ff00 velocityDebugColor: GetFastValue(config, 'debugVelocityColor', 0x00ff00),
maxVelocityX: GetFastValue(config, 'maxVelocityX', 100),
maxVelocityY: GetFastValue(config, 'maxVelocityY', 100),
minBounceVelocity: GetFastValue(config, 'minBounceVelocity', 40),
gravityFactor: GetFastValue(config, 'gravityFactor', 1),
bounciness: GetFastValue(config, 'bounciness', 0)
}; };
this.delta = 0;
this._lastId = 0; this._lastId = 0;
if (showDebug) if (this.drawDebug)
{ {
this.createDebugGraphic(); this.createDebugGraphic();
} }

View file

@ -29,7 +29,7 @@ var PhysicsManager = new Class({
if (config.system === 'impact') if (config.system === 'impact')
{ {
this.world = new Physics.Impact.World(this.scene, config.gravity, config.cellSize, config.debug); this.world = new Physics.Impact.World(this.scene, config);
this.add = new Physics.Impact.Factory(this.world); this.add = new Physics.Impact.Factory(this.world);
} }
}, },