diff --git a/lib/Chuck/Physics/Doll.js b/lib/Chuck/Physics/Doll.js index 9ef3291..e0d6692 100644 --- a/lib/Chuck/Physics/Doll.js +++ b/lib/Chuck/Physics/Doll.js @@ -1,56 +1,57 @@ -define(['Doll'], function(){ - Chuck.Physics.Doll = function(engine){ - this._engine = engine; +define(["Box2D/Box2D", "Chuck/Settings"], function(Box2D, Settings){ + + function Doll (physicsEngine){ + this._physicsEngine = physicsEngine; this._body; this._legs; this._contactPoint; - this.init(this._engine.getWorld()); + this.init(this._physicsEngine.getWorld()); } - Chuck.Physics.Doll.prototype.init = function (world) { + Doll.prototype.init = function (world) { - var bodyDef = new Chuck.b2BodyDef(); - bodyDef.position.x = 220 / Chuck.Settings.RATIO; - bodyDef.position.y = 0 / Chuck.Settings.RATIO; + var bodyDef = new Box2D.Dynamics.b2BodyDef(); + bodyDef.position.x = 220 / Settings.RATIO; + bodyDef.position.y = 0 / Settings.RATIO; bodyDef.fixedRotation = true; - bodyDef.linearDamping = Chuck.Settings.PLAYER_LINEAR_DAMPING; - bodyDef.type = Chuck.b2Body.b2_dynamicBody; + bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; + bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; this._body = world.CreateBody(bodyDef); - var fixtureDef = new Chuck.b2FixtureDef(); - fixtureDef.density = Chuck.Settings.PLAYER_DENSITY; + var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); + fixtureDef.density = Settings.PLAYER_DENSITY; fixtureDef.friction = 0; - fixtureDef.restitution = Chuck.Settings.PLAYER_RESTITUTION; + fixtureDef.restitution = Settings.PLAYER_RESTITUTION; - var headShape = new Chuck.b2CircleShape(); - headShape.SetRadius(5 / Chuck.Settings.RATIO); - headShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -37 / Chuck.Settings.RATIO)); + var headShape = new Box2D.Collision.Shapes.b2CircleShape(); + headShape.SetRadius(5 / Settings.RATIO); + headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); fixtureDef.shape = headShape; fixtureDef.isSensor = false; fixtureDef.userData = 'myHead'; this._body.CreateFixture(fixtureDef); - var bodyShape = new Chuck.b2PolygonShape(); - bodyShape.SetAsOrientedBox(5 / Chuck.Settings.RATIO, 16 / Chuck.Settings.RATIO, new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -21 / Chuck.Settings.RATIO)); + 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._body.CreateFixture(fixtureDef); - var legsShape = new Chuck.b2CircleShape(); - legsShape.SetRadius(5 / Chuck.Settings.RATIO); - legsShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -5 / Chuck.Settings.RATIO)); + var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); + legsShape.SetRadius(5 / Settings.RATIO); + legsShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -5 / Settings.RATIO)); fixtureDef.shape = legsShape; - fixtureDef.friction = Chuck.Settings.PLAYER_FRICTION; + fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.isSensor = false; fixtureDef.userData = 'myLegs'; this._legs = this._body.CreateFixture(fixtureDef); - var feetShape = new Chuck.b2CircleShape(); - feetShape.SetRadius(4 / Chuck.Settings.RATIO); - feetShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, 0 / Chuck.Settings.RATIO)); + var feetShape = new Box2D.Collision.Shapes.b2CircleShape(); + feetShape.SetRadius(4 / Settings.RATIO); + feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO)); fixtureDef.shape = feetShape; fixtureDef.isSensor = true; fixtureDef.userData = 'myFeet'; @@ -59,16 +60,16 @@ define(['Doll'], function(){ this._body.SetActive(false); } - Chuck.Physics.Doll.prototype.spawn = function (x, y) { - this._body.SetPosition(new Chuck.b2Vec2(x / Chuck.Settings.RATIO, y / Chuck.Settings.RATIO)); + Doll.prototype.spawn = function (x, y) { + this._body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO)); this._body.SetActive(true); } - Chuck.Physics.Doll.prototype.getBody = function () { + Doll.prototype.getBody = function () { return this._body; } - Chuck.Physics.Doll.prototype._setFriction = function (friction) { + Doll.prototype._setFriction = function (friction) { if(!friction) friction = -1; if (this._legs.GetFriction() != friction) @@ -77,29 +78,31 @@ define(['Doll'], function(){ } } - Chuck.Physics.Doll.prototype.move = function (direction, speed) { - this._setFriction(Chuck.Settings.PLAYER_MOTION_FRICTION); + Doll.prototype.move = function (direction, speed) { + this._setFriction(Settings.PLAYER_MOTION_FRICTION); this._body.SetAwake(true); - var vector = new Chuck.b2Vec2(speed * direction, this._body.GetLinearVelocity().y); + var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this._body.GetLinearVelocity().y); this._body.SetLinearVelocity(vector); } - Chuck.Physics.Doll.prototype.stop = function () { - this._setFriction(Chuck.Settings.PLAYER_FRICTION); + Doll.prototype.stop = function () { + this._setFriction(Settings.PLAYER_FRICTION); } - Chuck.Physics.Doll.prototype.jump = function () { + Doll.prototype.jump = function () { this._body.SetAwake(true); - var vector = new Chuck.b2Vec2(0, -Chuck.Settings.JUMP_SPEED); + var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED); this._body.ApplyImpulse(vector, this._body.GetPosition()); // maybe change to a constant force instead of applying of force? // to prevent higher jumping running uphill, etc. } - Chuck.Physics.Doll.prototype.jumping = function () { - var vector = new Chuck.b2Vec2(0, -0.1); + Doll.prototype.jumping = function () { + var vector = new Box2D.Common.Math.b2Vec2(0, -0.1); this._body.ApplyImpulse(vector, this._body.GetPosition()); } -} \ No newline at end of file + + return Doll; +}); \ No newline at end of file diff --git a/lib/Chuck/Player.js b/lib/Chuck/Player.js index 8ffc388..43abf55 100644 --- a/lib/Chuck/Player.js +++ b/lib/Chuck/Player.js @@ -1,4 +1,4 @@ -define(['Doll'], function(){ +define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){ function Player (physicsEngine, repository) { this._physicsEngine = physicsEngine; @@ -36,8 +36,7 @@ define(['Doll'], function(){ Player.prototype.setStanding = function(isStanding) { var resetStates = ['jump', 'jumploop']; - if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding) - { + if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding) { this._animate('stand'); } this._standing = isStanding; @@ -52,15 +51,15 @@ define(['Doll'], function(){ switch(true) { case direction == this._lookDirection && this.isStanding(): - this._doll.move(direction, Chuck.Settings.RUN_SPEED); + this._doll.move(direction, Settings.RUN_SPEED); break; case !this.isStanding(): - this._doll.move(direction, Chuck.Settings.FLY_SPEED); + this._doll.move(direction, Settings.FLY_SPEED); break; default: - this._doll.move(direction, Chuck.Settings.WALK_SPEED); + this._doll.move(direction, Settings.WALK_SPEED); break; } @@ -78,8 +77,7 @@ define(['Doll'], function(){ } Player.prototype.jump = function() { - if (this.isStanding()) - { + if (this.isStanding()) { this._doll.jump(); this._animate('jump'); this.setStanding(false); @@ -123,15 +121,15 @@ define(['Doll'], function(){ Player.prototype.look = function(x, y) { /* - var degree = Math.atan2(Chuck.Settings.STAGE_WIDTH / 2 - x, Chuck.Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180); + var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180); var lastLookDirection = this._lookDirection; - if (x < Chuck.Settings.STAGE_WIDTH / 2) { + if (x < Settings.STAGE_WIDTH / 2) { this._mc.scaleX = -1; this._lookDirection = -1; degree = (-45 + degree / 2); this._mc.head.rotation = degree; - } else if (x >= Chuck.Settings.STAGE_WIDTH / 2) { + } else if (x >= Settings.STAGE_WIDTH / 2) { this._mc.scaleX = 1; this._lookDirection = 1; degree = (45 + -degree / 2) - 90; @@ -155,7 +153,7 @@ define(['Doll'], function(){ // called by CollisionDetection Player.prototype.onFootSensorDetection = function(isColliding) { if(isColliding) { - if(this._doll.getBody().GetLinearVelocity().y < -Chuck.Settings.JUMP_SPEED && !this.isStanding()) { + if(this._doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) { return; } this.setStanding(true); @@ -180,4 +178,6 @@ define(['Doll'], function(){ this.setStanding(true); } } -} + + return Player; +}); diff --git a/lib/Chuck/Processor.js b/lib/Chuck/Processor.js index 745365d..e4a11c3 100644 --- a/lib/Chuck/Processor.js +++ b/lib/Chuck/Processor.js @@ -1,4 +1,4 @@ -define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ +define(["Chuck/Physics/Engine", "Chuck/Player", "Box2D/Box2D"], function(PhysicsEngine, Player, Box2D){ function Processor () { this._me; @@ -20,56 +20,48 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ + { + var world = this._physicsEngine.getWorld(); - var world = this._physicsEngine.getWorld(); + var fixDef = new Box2D.Dynamics.b2FixtureDef; + fixDef.density = 1.0; + fixDef.friction = 0.99; + fixDef.restitution = .51; + var bodyDef = new Box2D.Dynamics.b2BodyDef; + + // create ground + bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; + fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; + fixDef.shape.SetAsBox(20, 2); + bodyDef.position.Set(10, 400 / 30 + 1.8); + world.CreateBody(bodyDef).CreateFixture(fixDef); + bodyDef.position.Set(10, -1.8); + world.CreateBody(bodyDef).CreateFixture(fixDef); + fixDef.shape.SetAsBox(2, 14); + bodyDef.position.Set(-1.8, 13); + world.CreateBody(bodyDef).CreateFixture(fixDef); + bodyDef.position.Set(21.8, 13); + world.CreateBody(bodyDef).CreateFixture(fixDef); - var fixDef = new Box2D.Dynamics.b2FixtureDef; - fixDef.density = 1.0; - fixDef.friction = 0.99; - fixDef.restitution = .51; - - var bodyDef = new Box2D.Dynamics.b2BodyDef; - - // create ground - bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; - fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; - fixDef.shape.SetAsBox(20, 2); + // create object + bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; + fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; + fixDef.shape.SetAsBox(0.4, 0.4); + bodyDef.position.x = 10; + bodyDef.position.y = 2; + bodyDef.userData = { + 'bodyId': 1 + '' + }; - bodyDef.position.Set(10, 400 / 30 + 1.8); + world.CreateBody(bodyDef).CreateFixture(fixDef); + this._bodyDef = bodyDef; + } - world.CreateBody(bodyDef).CreateFixture(fixDef); - bodyDef.position.Set(10, -1.8); - - world.CreateBody(bodyDef).CreateFixture(fixDef); - fixDef.shape.SetAsBox(2, 14); - - bodyDef.position.Set(-1.8, 13); - world.CreateBody(bodyDef).CreateFixture(fixDef); - - bodyDef.position.Set(21.8, 13); - world.CreateBody(bodyDef).CreateFixture(fixDef); - - // create object - bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - - fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; - fixDef.shape.SetAsBox(0.4, 0.4); - - bodyDef.position.x = 10; - bodyDef.position.y = 2; - - bodyDef.userData = { - 'bodyId': 1 + '' - }; - - world.CreateBody(bodyDef).CreateFixture(fixDef); - - - this._bodyDef = bodyDef; - //this._me = new Chuck.Player(this._physicsEngine, this._repository); - + this._me = new Player(this._physicsEngine, null); + this._me.spawn(100, 0); + /* //this._camera = Camera.getInstance() //this._repository = Repository.getInstance(); @@ -80,7 +72,7 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ new Chuck.Loader.Level(this._physicsEngine); //new Items(); - this._me.spawn(100, 0); + //this._camera.follow(this._me); */ setInterval(this._update, 1000/60, this);