diff --git a/lib/Chuck/Collision/Detector.js b/lib/Chuck/Collision/Detector.js new file mode 100644 index 0000000..d6e211b --- /dev/null +++ b/lib/Chuck/Collision/Detector.js @@ -0,0 +1,38 @@ +define(["Box2D/Box2D"], function(Box2D) { + + function Detector(me) { + this.me = me; + + this.listener = new Box2D.Dynamics.b2ContactListener(); + this.listener.chuckDetector = this; + this.listener.BeginContact = this.BeginContact; + this.listener.PostSolve = this.PostSolve; + this.listener.EndContact = this.EndContact; + } + + Detector.prototype.getListener = function() { + return this.listener; + } + + Detector.prototype.handleStand = function(point, isColliding) { + if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') { + this.me.onFootSensorDetection(isColliding); + } + } + + /** Extension **/ + + Detector.prototype.BeginContact = function(point) { + this.chuckDetector.handleStand(point, true); + } + + Detector.prototype.PostSolve = function(point, impulse) { + this.chuckDetector.handleStand(point, true); + } + + Detector.prototype.EndContact = function(point) { + this.chuckDetector.handleStand(point, false); + } + + return Detector; +}); \ No newline at end of file diff --git a/lib/Chuck/Physics/Engine.js b/lib/Chuck/Physics/Engine.js index 4bab228..8dc0166 100644 --- a/lib/Chuck/Physics/Engine.js +++ b/lib/Chuck/Physics/Engine.js @@ -1,12 +1,12 @@ -define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ +define(["Chuck/Settings", "Box2D/Box2D", "Chuck/Collision/Detector"], function(Settings, Box2D, CollisionDetector){ function Engine () { - this._world; + this.world; this.init(); } Engine.prototype.init = function() { - this._world = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), Settings.BOX2D_ALLOW_SLEEP); + this.world = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), Settings.BOX2D_ALLOW_SLEEP); if(Settings.IS_BROWSER_ENVIRONMENT) { this.setupDebugDraw(); @@ -14,14 +14,13 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ } Engine.prototype.getWorld = function() { - return this._world; + return this.world; } Engine.prototype.setCollisionDetector = function(me) { - /* - var cd = new Chuck.Collision.Detector(me); - var listener = cd.getListener(); - this._world.SetContactListener(listener);*/ + + var detector = new CollisionDetector(me); + this.world.SetContactListener(detector.getListener()); } Engine.prototype.setupDebugDraw = function() { @@ -46,18 +45,18 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ //| Box2D.Dynamics.b2DebugDraw.e_pairBit ); - this._world.SetDebugDraw(debugDraw); - this._world.SetWarmStarting(true); + this.world.SetDebugDraw(debugDraw); + this.world.SetWarmStarting(true); } Engine.prototype.createBody = function(bodyDef) { - return this._world.CreateBody(bodyDef); + return this.world.CreateBody(bodyDef); } Engine.prototype.update = function() { - this._world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS); - this._world.ClearForces(); - this._world.DrawDebugData(); + this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS); + this.world.ClearForces(); + this.world.DrawDebugData(); } return Engine; diff --git a/lib/Chuck/Processor.js b/lib/Chuck/Processor.js index 565150c..aeec0c4 100644 --- a/lib/Chuck/Processor.js +++ b/lib/Chuck/Processor.js @@ -28,7 +28,7 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box Processor.prototype.init = function() { this._physicsEngine = new PhysicsEngine(); - this._makeBox(); + //this._makeBox(); if(Settings.IS_BROWSER_ENVIRONMENT) { @@ -39,9 +39,11 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box //this._camera = Camera.getInstance() //this._camera.follow(this._me); //this._repository = Repository.getInstance(); - //this._physicsEngine.setCollisionDetector(this._me); + this._physicsEngine.setCollisionDetector(this._me); } + + //new Chuck.Loader.Level(this._physicsEngine);u //new Items();