From 3c1e7008e5235dd85481253cbfbe6da67061a41f Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 15 Jul 2012 22:32:44 +0200 Subject: [PATCH] added Constants module. fixes #4 --- lib/Chuck/Collision/Detector.js | 6 ++++-- lib/Chuck/Constants.js | 16 +++++++++++++++- lib/Chuck/Loader/Level.js | 4 ++-- lib/Chuck/Physics/Doll.js | 16 ++++++++-------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/Chuck/Collision/Detector.js b/lib/Chuck/Collision/Detector.js index f9a18fb..42378ce 100644 --- a/lib/Chuck/Collision/Detector.js +++ b/lib/Chuck/Collision/Detector.js @@ -1,4 +1,4 @@ -define(["Vendor/Box2D"], function(Box2D) { +define(["Vendor/Box2D", "Chuck/Constants"], function(Box2D, Constants) { function Detector(me) { this.me = me; @@ -15,7 +15,9 @@ define(["Vendor/Box2D"], function(Box2D) { } Detector.prototype.handleStand = function(point, isColliding) { - if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') { + if (point.GetFixtureA().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR + || point.GetFixtureB().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR) { + this.me.onFootSensorDetection(isColliding); } } diff --git a/lib/Chuck/Constants.js b/lib/Chuck/Constants.js index 435d77f..4e41b00 100644 --- a/lib/Chuck/Constants.js +++ b/lib/Chuck/Constants.js @@ -1 +1,15 @@ -Constants.js \ No newline at end of file +define(function() { + + var Constants = { + COLLISION_IDENTIFIER_PLAYER: 'player', + COLLISION_IDENTIFIER_PLAYER_HEAD: 'head', + COLLISION_IDENTIFIER_PLAYER_CHEST: 'chest', + COLLISION_IDENTIFIER_PLAYER_LEGS: 'legs', + COLLISION_IDENTIFIER_PLAYER_FOOT_SENSOR: 'footsensor', + + COLLISION_IDENTIFIER_TILE: 'tile' + } + + return Constants; + +}); \ No newline at end of file diff --git a/lib/Chuck/Loader/Level.js b/lib/Chuck/Loader/Level.js index acc7f0e..a45137d 100644 --- a/lib/Chuck/Loader/Level.js +++ b/lib/Chuck/Loader/Level.js @@ -1,4 +1,4 @@ -define(["Chuck/Settings", "Vendor/Box2D"], function(Settings, Box2D) { +define(["Chuck/Settings", "Chuck/Constants", "Vendor/Box2D"], function(Settings, Constants, Box2D) { // Public function Level(path, engine) { @@ -50,7 +50,7 @@ define(["Chuck/Settings", "Vendor/Box2D"], function(Settings, Box2D) { fixtureDef.friction = Settings.TILE_FRICTION; fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.isSensor = false; - fixtureDef.userData = 'tile'; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_TILE; this.engine.createBody(bodyDef).CreateFixture(fixtureDef); } diff --git a/lib/Chuck/Physics/Doll.js b/lib/Chuck/Physics/Doll.js index a9455ff..26f2277 100644 --- a/lib/Chuck/Physics/Doll.js +++ b/lib/Chuck/Physics/Doll.js @@ -1,4 +1,4 @@ -define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ +define(["Vendor/Box2D", "Chuck/Constants", "Chuck/Settings"], function(Box2D, Constants, Settings){ function Doll (physicsEngine, id){ this.id = id; @@ -18,7 +18,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ bodyDef.fixedRotation = true; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - bodyDef.userData = 'player-' + this.id; + bodyDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER + '-' + this.id; this.body = world.CreateBody(bodyDef); @@ -32,14 +32,14 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); fixtureDef.shape = headShape; fixtureDef.isSensor = false; - fixtureDef.userData = 'myHead-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_HEAD; this.body.CreateFixture(fixtureDef); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape(); bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO)); fixtureDef.shape = bodyShape; fixtureDef.isSensor = false; - fixtureDef.userData = 'myBody-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_CHEST; this.body.CreateFixture(fixtureDef); var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); @@ -48,7 +48,8 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ fixtureDef.shape = legsShape; fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.isSensor = false; - fixtureDef.userData = 'myLegs-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_LEGS; + this.legs = this.body.CreateFixture(fixtureDef); var feetShape = new Box2D.Collision.Shapes.b2CircleShape(); @@ -56,7 +57,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO)); fixtureDef.shape = feetShape; fixtureDef.isSensor = true; - fixtureDef.userData = 'myFeet-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_FOOTSENSOR; this.body.CreateFixture(fixtureDef); this.body.SetActive(false); @@ -74,8 +75,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ Doll.prototype.setFriction = function (friction) { if(!friction) friction = -1; - if (this.legs.GetFriction() != friction) - { + if (this.legs.GetFriction() != friction) { this.legs.SetFriction(friction); } }