From 5a6581393e776180364c8b6bed17bf7b733d9f70 Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 1 Jul 2012 15:16:33 +0200 Subject: [PATCH] repaired debug draw, added more classes from game draft --- lib/Chuck/Physics/Doll.js | 105 +++++++++++++++++++++ lib/Chuck/Physics/Engine.js | 42 ++++----- lib/Chuck/Player.js | 183 ++++++++++++++++++++++++++++++++++++ lib/Chuck/Processor.js | 53 +++++------ lib/Chuck/Settings.js | 2 +- server.js | 40 ++++---- 6 files changed, 354 insertions(+), 71 deletions(-) create mode 100644 lib/Chuck/Physics/Doll.js create mode 100644 lib/Chuck/Player.js diff --git a/lib/Chuck/Physics/Doll.js b/lib/Chuck/Physics/Doll.js new file mode 100644 index 0000000..9ef3291 --- /dev/null +++ b/lib/Chuck/Physics/Doll.js @@ -0,0 +1,105 @@ +define(['Doll'], function(){ + Chuck.Physics.Doll = function(engine){ + this._engine = engine; + this._body; + this._legs; + this._contactPoint; + + this.init(this._engine.getWorld()); + } + + Chuck.Physics.Doll.prototype.init = function (world) { + + var bodyDef = new Chuck.b2BodyDef(); + bodyDef.position.x = 220 / Chuck.Settings.RATIO; + bodyDef.position.y = 0 / Chuck.Settings.RATIO; + bodyDef.fixedRotation = true; + bodyDef.linearDamping = Chuck.Settings.PLAYER_LINEAR_DAMPING; + bodyDef.type = Chuck.b2Body.b2_dynamicBody; + + this._body = world.CreateBody(bodyDef); + + var fixtureDef = new Chuck.b2FixtureDef(); + fixtureDef.density = Chuck.Settings.PLAYER_DENSITY; + fixtureDef.friction = 0; + fixtureDef.restitution = Chuck.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)); + 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)); + 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)); + fixtureDef.shape = legsShape; + fixtureDef.friction = Chuck.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)); + fixtureDef.shape = feetShape; + fixtureDef.isSensor = true; + fixtureDef.userData = 'myFeet'; + this._body.CreateFixture(fixtureDef); + + 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)); + this._body.SetActive(true); + } + + Chuck.Physics.Doll.prototype.getBody = function () { + return this._body; + } + + Chuck.Physics.Doll.prototype._setFriction = function (friction) { + if(!friction) friction = -1; + + if (this._legs.GetFriction() != friction) + { + this._legs.SetFriction(friction); + } + } + + Chuck.Physics.Doll.prototype.move = function (direction, speed) { + this._setFriction(Chuck.Settings.PLAYER_MOTION_FRICTION); + this._body.SetAwake(true); + var vector = new Chuck.b2Vec2(speed * direction, this._body.GetLinearVelocity().y); + this._body.SetLinearVelocity(vector); + } + + Chuck.Physics.Doll.prototype.stop = function () { + this._setFriction(Chuck.Settings.PLAYER_FRICTION); + } + + Chuck.Physics.Doll.prototype.jump = function () { + this._body.SetAwake(true); + + var vector = new Chuck.b2Vec2(0, -Chuck.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); + this._body.ApplyImpulse(vector, this._body.GetPosition()); + } +} \ No newline at end of file diff --git a/lib/Chuck/Physics/Engine.js b/lib/Chuck/Physics/Engine.js index e67ab38..0455636 100644 --- a/lib/Chuck/Physics/Engine.js +++ b/lib/Chuck/Physics/Engine.js @@ -6,7 +6,7 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ } 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(); @@ -25,32 +25,30 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ } Engine.prototype.setupDebugDraw = function() { - var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE; - console.log(debugSprite); + //var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE; + var debugSprite = document.getElementById("canvas").getContext("2d"); // set debug draw - var dbgDraw = new Box2D.Dynamics.b2DebugDraw(); + var debugDraw = new Box2D.Dynamics.b2DebugDraw(); - dbgDraw.SetSprite(debugSprite); - dbgDraw.SetDrawScale(Settings.RATIO); - dbgDraw.SetAlpha(0.5); - dbgDraw.SetFillAlpha(0.1); - dbgDraw.SetLineThickness(0); + debugDraw.SetSprite(debugSprite); + debugDraw.SetDrawScale(Settings.RATIO); + debugDraw.SetDrawScale(30.0); + debugDraw.SetFillAlpha(0.5); + debugDraw.SetLineThickness(1.0); - dbgDraw.SetFlags(null - | dbgDraw.e_shapeBit - //| b2DebugDraw.e_jointBit - //| b2DebugDraw.e_coreShapeBit - //| b2DebugDraw.e_aabbBit - //| b2DebugDraw.e_centerOfMassBit - //| b2DebugDraw.e_obbBit - //| b2DebugDraw.e_pairBit - ); - - this._world.SetDebugDraw(dbgDraw); + debugDraw.SetFlags(null + | Box2D.Dynamics.b2DebugDraw.e_shapeBit + | Box2D.Dynamics.b2DebugDraw.e_jointBit + //| Box2D.Dynamics.b2DebugDraw.e_coreShapeBit + //| Box2D.Dynamics.b2DebugDraw.e_aabbBit + //| Box2D.Dynamics.b2DebugDraw.e_centerOfMassBit + //| Box2D.Dynamics.b2DebugDraw.e_obbBit + //| Box2D.Dynamics.b2DebugDraw.e_pairBit + ); + this._world.SetDebugDraw(debugDraw); this._world.SetWarmStarting(true); - console.log('Debug Draw was set up'); } Engine.prototype.createBody = function(bodyDef) { @@ -58,7 +56,7 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ } Engine.prototype.update = function() { - this._world.Step(Settings.Box2D_TIME_STEP, Settings.Box2D_VELOCITY_ITERATIONS, Settings.Box2D_POSITION_ITERATIONS); + this._world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS); this._world.ClearForces(); this._world.DrawDebugData(); } diff --git a/lib/Chuck/Player.js b/lib/Chuck/Player.js new file mode 100644 index 0000000..8ffc388 --- /dev/null +++ b/lib/Chuck/Player.js @@ -0,0 +1,183 @@ +define(['Doll'], function(){ + + function Player (physicsEngine, repository) { + this._physicsEngine = physicsEngine; + this._repository = repository; + this._standing = false; + this._doll; + this._mc; + this._currentAnimationState = 'stand'; + this._lookDirection = 1; + this._moveDirection = 0; + + this.init(); + } + + Player.prototype.init = function() { + this._doll = new Doll(this._physicsEngine); + //this._mc = EmbedHandler.load(EmbedHandler.CHUCK); + //this._mc.stop(); + //var mclp = new MovieClipLabelParser(); + //mclp.parse(this._mc); + } + + Player.prototype.spawn = function(x, y) { + //this._repository.createModel(this._mc, this._doll.getBody()); + this._doll.spawn(x, y); + } + + Player.prototype.getDoll = function() { + return this._doll; + } + + Player.prototype.getBody = function() { + return this._doll.getBody(); + } + + Player.prototype.setStanding = function(isStanding) { + var resetStates = ['jump', 'jumploop']; + if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding) + { + this._animate('stand'); + } + this._standing = isStanding; + } + + Player.prototype.isStanding = function() { + return this._standing; + } + + Player.prototype.move = function(direction) { + this._moveDirection = direction; + + switch(true) { + case direction == this._lookDirection && this.isStanding(): + this._doll.move(direction, Chuck.Settings.RUN_SPEED); + break; + + case !this.isStanding(): + this._doll.move(direction, Chuck.Settings.FLY_SPEED); + break; + + default: + this._doll.move(direction, Chuck.Settings.WALK_SPEED); + break; + } + + if (this.isStanding()) { + this._animate(this._calculateWalkAnimation()); + } + } + + Player.prototype.stop = function() { + this._moveDirection = 0; + this._doll.stop(); + if (this._isWalking() || this._standing) { + this._animate('stand'); + } + } + + Player.prototype.jump = function() { + if (this.isStanding()) + { + this._doll.jump(); + this._animate('jump'); + this.setStanding(false); + } + } + + Player.prototype.jumping = function() { + if (!this.isStanding()) { + this._doll.jumping(); + } + } + + Player.prototype.duck = function() { + if (this._standing && !this._isWalking()) { + this._animate('duck'); + } + } + + Player.prototype.standUp = function() { + if (this._standing) { + this._animate('standup'); + } + } + + Player.prototype._animate = function(type) { + if (type == this._currentAnimationState) { + return; + } + + //this._mc.gotoAndPlay(type); + + this._currentAnimationState = type; + } + + Player.prototype._calculateWalkAnimation = function() { + if (this._moveDirection == this._lookDirection) { + return 'run'; + } + return 'walkback'; + } + + 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 lastLookDirection = this._lookDirection; + + if (x < Chuck.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) { + this._mc.scaleX = 1; + this._lookDirection = 1; + degree = (45 + -degree / 2) - 90; + this._mc.head.rotation = degree; + } + + if (this._lookDirection != lastLookDirection && this._isWalking()) { + this._animate(this._calculateWalkAnimation()); + }*/ + } + + Player.prototype._isWalking = function() { + var states = ['walk', 'walkback', 'run']; + + if (states.indexOf(this._currentAnimationState) >= 0) { + return true; + } + return false; + } + + // called by CollisionDetection + Player.prototype.onFootSensorDetection = function(isColliding) { + if(isColliding) { + if(this._doll.getBody().GetLinearVelocity().y < -Chuck.Settings.JUMP_SPEED && !this.isStanding()) { + return; + } + this.setStanding(true); + } else { + // TODO This needs some more thought to it. + // maybe take a look at collision groups for collision detection, + // to group all tiles together + + //this.setStanding(false); + //this._animate('jumploop'); + } + } + + Player.prototype.update = function() { + //this._mc.head.y = this._mc.head_posmask.y; + + if (this._doll.getBody().GetLinearVelocity().x == 0 && this._isWalking()) { + this.stop(); + } + + if (!this._doll.getBody().IsAwake()) { + this.setStanding(true); + } + } +} diff --git a/lib/Chuck/Processor.js b/lib/Chuck/Processor.js index b77824f..745365d 100644 --- a/lib/Chuck/Processor.js +++ b/lib/Chuck/Processor.js @@ -8,6 +8,8 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ this._inputControlUnit; this._keyboardInput; + this._bodyDef; + this.init(); }; @@ -15,25 +17,9 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ this._physicsEngine = new PhysicsEngine(); -/* - var fixDef = new Box2D.Dynamics.b2FixtureDef; - fixDef.density = 1.0; - fixDef.friction = 0.99; - fixDef.restitution = .51; - - var bodyDef = new Box2D.Dynamics.b2BodyDef; - bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; - fixDef.shape.SetAsBox(0.4, 0.4); - - this._physicsEngine.createBody(bodyDef).CreateFixture(fixDef); - bodyDef.position.x = 2; - bodyDef.position.y = 3; - - console.log('bodying');*/ - ///------ + var world = this._physicsEngine.getWorld(); @@ -63,25 +49,28 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ bodyDef.position.Set(21.8, 13); world.CreateBody(bodyDef).CreateFixture(fixDef); - // create some objects + // create object bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - for(var i = 0; i < 5; i++) { - fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; - fixDef.shape.SetAsBox(0.4, 0.4); + fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; + fixDef.shape.SetAsBox(0.4, 0.4); - bodyDef.position.x = ((i + 1) * 2) % 8; - bodyDef.position.y = 3; + bodyDef.position.x = 10; + bodyDef.position.y = 2; - bodyDef.userData = { - 'bodyId': i + '' - }; + bodyDef.userData = { + 'bodyId': 1 + '' + }; - world.CreateBody(bodyDef).CreateFixture(fixDef); - } + world.CreateBody(bodyDef).CreateFixture(fixDef); + + + this._bodyDef = bodyDef; + + + //this._me = new Chuck.Player(this._physicsEngine, this._repository); /* - this._me = new Chuck.Player(this._physicsEngine, this._repository); //this._camera = Camera.getInstance() //this._repository = Repository.getInstance(); this._physicsEngine.setCollisionDetector(this._me); @@ -108,11 +97,17 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ Processor.prototype._update = function(self) { self._physicsEngine.update(); + + //console.log(self._physicsEngine.getWorld().GetBodyList().GetPosition()); + //self._repository.update(); //self._keyboardInput.update(); //self._me.update(); //self._camera.update(); } + + + return Processor; }); diff --git a/lib/Chuck/Settings.js b/lib/Chuck/Settings.js index 7b15e18..b1226d6 100644 --- a/lib/Chuck/Settings.js +++ b/lib/Chuck/Settings.js @@ -5,7 +5,7 @@ define(function() { STAGE_HEIGHT : 400, // BOX2D INITIALATORS - RATIO : 35, + RATIO : 30, BOX2D_WORLD_AABB_SIZE : 3000, BOX2D_ALLOW_SLEEP : true, BOX2D_GRAVITY : 16, diff --git a/server.js b/server.js index 5b99b69..5ad1ddf 100644 --- a/server.js +++ b/server.js @@ -19,28 +19,30 @@ requirejs(["Chuck/Chuck"], function(Chuck) { var fileServer = new nodeStatic.Server('./'); var server = http.createServer( function(req, res){ - switch(req.url) { - case '/': - fileServer.serveFile('./index.html', 200, {}, req, res); - break; + req.addListener('end', function () { + switch(req.url) { + case '/': + fileServer.serveFile('./index.html', 200, {}, req, res); + break; - case '/client.js': - fileServer.serveFile('./client.js', 200, {}, req, res); - break; + case '/client.js': + fileServer.serveFile('./client.js', 200, {}, req, res); + break; - case '/require.js': - fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res); - break; + case '/require.js': + fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res); + break; - default: - if(req.url.match(/^\/lib/)) { - fileServer.serve(req, res); - } else { - res.writeHead(404, {'Content-Type': 'text/html'}); - res.end('

404 not ... found

'); - } - break; - } + default: + if(req.url.match(/^\/lib/)) { + fileServer.serve(req, res); + } else { + res.writeHead(404, {'Content-Type': 'text/html'}); + res.end('

404 not ... found

'); + } + break; + } + }); } ); server.listen(1234);