diff --git a/index.html b/index.html index 7686069..cff8016 100755 --- a/index.html +++ b/index.html @@ -11,7 +11,10 @@ + + + diff --git a/lib/Chuck/Collision/Detector.js b/lib/Chuck/Collision/Detector.js new file mode 100755 index 0000000..b3f8f86 --- /dev/null +++ b/lib/Chuck/Collision/Detector.js @@ -0,0 +1,33 @@ +Chuck.Collision.Detector = function(me) { //extends b2ContactListener { + this._me = me; + + this._listener = new Chuck.b2ContactListener(); + this._listener._chuckDetector = this; + this._listener.BeginContact = this.BeginContact; + this._listener.PostSolve = this.PostSolve; + this._listener.EndContact = this.EndContact; +} + +Chuck.Collision.Detector.prototype.getListener = function() { + return this._listener; +} + +Chuck.Collision.Detector.prototype.handleStand = function(point, isColliding) { + if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') { + this._me.onFootSensorDetection(isColliding); + } +} + +/** Extension **/ + +Chuck.Collision.Detector.prototype.BeginContact = function(point) { + this._chuckDetector.handleStand(point, true); +} + +Chuck.Collision.Detector.prototype.PostSolve = function(point, impulse) { + this._chuckDetector.handleStand(point, true); +} + +Chuck.Collision.Detector.prototype.EndContact = function(point) { + this._chuckDetector.handleStand(point, false); +} diff --git a/lib/Chuck/Config/Setup.js b/lib/Chuck/Config/Setup.js index fd3aadc..2c86b08 100755 --- a/lib/Chuck/Config/Setup.js +++ b/lib/Chuck/Config/Setup.js @@ -2,6 +2,7 @@ var Chuck = { Physics: {}, Loader: {}, Control: {}, + Collision: {}, b2Vec2 : Box2D.Common.Math.b2Vec2, b2AABB : Box2D.Collision.b2AABB, @@ -14,7 +15,8 @@ var Chuck = { b2PolygonShape : Box2D.Collision.Shapes.b2PolygonShape, b2CircleShape : Box2D.Collision.Shapes.b2CircleShape, b2DebugDraw : Box2D.Dynamics.b2DebugDraw, - b2MouseJointDef : Box2D.Dynamics.Joints.b2MouseJointDef + b2MouseJointDef : Box2D.Dynamics.Joints.b2MouseJointDef, + b2ContactListener : Box2D.Dynamics.b2ContactListener }; Chuck.Settings = { @@ -49,7 +51,7 @@ Chuck.Settings = { PLAYER_DENSITY : 0.96, PLAYER_FRICTION : 5, - PLAYER_MOTION_FRICTION : 0, + PLAYER_MOTION_FRICTION : 0.1, PLAYER_RESTITUTION : 0.0, PLAYER_LINEAR_DAMPING : .5, diff --git a/lib/Chuck/Control/InputControlUnit.js b/lib/Chuck/Control/InputControlUnit.js index fc38213..242b38e 100755 --- a/lib/Chuck/Control/InputControlUnit.js +++ b/lib/Chuck/Control/InputControlUnit.js @@ -1,36 +1,27 @@ -Chuck.Control.InputControlUnit = function() { - var KEY_LEFT; - var KEY_RIGHT; - var KEY_UP; - var KEY_DOWN; +Chuck.Control.InputControlUnit = function(ki, me) { + this._ki = ki; + this._me = me; - var _instance; - var _ki; - var _me; - var _shift; - var _isJumping; + this._shift; + this._isJumping; + + this.KEY_LEFT = 65; + this.KEY_RIGHT = 68; + this.KEY_UP = 87; + this.KEY_DOWN = 83; this.init(); } -Chuck.Control.InputControlUnit.prototype.InputControlUnit = function() { - this._me = Processor.getInstance().getMe(); - this._ki = KeyboardInput.getInstance(); - this._ki.registerKey(KEY_LEFT, this.moveLeft, this.stop, this.moveLeft); - this._ki.registerKey(KEY_RIGHT, this.moveRight, this.stop, this.moveRight); - this._ki.registerKey(KEY_UP, this.jump, this.jumped, this.jumping); - this._ki.registerKey(KEY_DOWN, this.duck, this.standUp, this.duck); - this._ki.registerKey(KEY_DOWN, this.activateShift, this.activateShift, this.deactivateShift); - - this._ki.registerKey(37, this.wasd); - this._ki.registerKey(38, this.wasd); - this._ki.registerKey(39, this.wasd); - this._ki.registerKey(40, this.wasd); -} - - -Chuck.Control.InputControlUnit.prototype.wasd = function() { - trace('wasd benutzen alter...'); +Chuck.Control.InputControlUnit.prototype.init = function() { + + this._ki.setInputControlUnit(this); + + this._ki.registerKey(this.KEY_LEFT, 'moveLeft', 'stop', 'moveLeft'); + this._ki.registerKey(this.KEY_RIGHT, 'moveRight', 'stop', 'moveRight'); + this._ki.registerKey(this.KEY_UP, 'jump', 'jumped', 'jumping'); + this._ki.registerKey(this.KEY_DOWN, 'duck', 'standUp', 'duck'); + this._ki.registerKey(this.KEY_DOWN, 'activateShift', 'activateShift', 'deactivateShift'); } Chuck.Control.InputControlUnit.prototype.moveLeft = function() { diff --git a/lib/Chuck/Control/Key.js b/lib/Chuck/Control/Key.js new file mode 100755 index 0000000..84dfbbf --- /dev/null +++ b/lib/Chuck/Control/Key.js @@ -0,0 +1,56 @@ +Chuck.Control.Key = function() { + this._active = false; + this._activityUpdateStatus = false; + this._activityUpdateNeeded = false; + this._keyDown = null; + this._keyUp = null; + this._keyFrame = null; + +} +Chuck.Control.Key.prototype.setActivityUpdateStatus = function(active) { + this._activityUpdateStatus = active; +} + +Chuck.Control.Key.prototype.getActivityUpdateStatus = function() { + return this._activityUpdateStatus; +} + +Chuck.Control.Key.prototype.setActivityUpdateNeeded = function(need) { + this._activityUpdateNeeded = need; +} + +Chuck.Control.Key.prototype.getActivityUpdateNeeded = function() { + return this._activityUpdateNeeded; +} + +Chuck.Control.Key.prototype.setActive = function(active) { + this._active = active; +} + +Chuck.Control.Key.prototype.getActive = function() { + return this._active; +} + +Chuck.Control.Key.prototype.setKeyDownFunction = function(f) { + this._keyDown = f; +} + +Chuck.Control.Key.prototype.getKeyDownFunction = function() { + return this._keyDown; +} + +Chuck.Control.Key.prototype.setKeyUpFunction = function(f) { + this._keyUp = f; +} + +Chuck.Control.Key.prototype.getKeyUpFunction = function() { + return this._keyUp; +} + +Chuck.Control.Key.prototype.setKeyFrameFunction = function(f) { + this._keyFrame = f; +} + +Chuck.Control.Key.prototype.getKeyFrameFunction = function() { + return this._keyFrame; +} diff --git a/lib/Chuck/Control/KeyboardInput.js b/lib/Chuck/Control/KeyboardInput.js new file mode 100755 index 0000000..985aef4 --- /dev/null +++ b/lib/Chuck/Control/KeyboardInput.js @@ -0,0 +1,72 @@ +Chuck.Control.KeyboardInput = function() { + this._registry = {}; + this._inputControlUnit = null; + + this.init(); +} + +Chuck.Control.KeyboardInput.prototype.init = function() { + $(window).keydown($.proxy(this._onKeyDown, this)); + $(window).keyup($.proxy(this._onKeyUp, this)); +} + +Chuck.Control.KeyboardInput.prototype.setInputControlUnit = function(inputControlUnit) { + this._inputControlUnit = inputControlUnit; +} + +Chuck.Control.KeyboardInput.prototype.registerKey = function(keyCode, onKeyDown, onKeyUp, onKeyFrame) { + var key = new Chuck.Control.Key(); + key.setKeyDownFunction(onKeyDown); + key.setKeyUpFunction(onKeyUp); + key.setKeyFrameFunction(onKeyFrame); + this._registry[keyCode] = key; +} + +Chuck.Control.KeyboardInput.prototype._getKeyByKeyCode = function(keyCode) { + return this._registry[keyCode]; +} + +Chuck.Control.KeyboardInput.prototype._onKeyDown = function(e) { + var key = this._getKeyByKeyCode(e.keyCode); + if (key && key.getActive() == false) { + key.setActivityUpdateStatus(true); + key.setActivityUpdateNeeded(true); + } +} + +Chuck.Control.KeyboardInput.prototype._onKeyUp = function(e) { + var key = this._getKeyByKeyCode(e.keyCode); + if (key != null) { + key.setActivityUpdateStatus(false); + key.setActivityUpdateNeeded(true); + } +} + +Chuck.Control.KeyboardInput.prototype.update = function() { + var callback = null; + var self = this; + $.each(this._registry, function(keyCode, key) { + if (key.getActivityUpdateNeeded()) { + if (key.getActivityUpdateStatus() == true) { + callback = key.getKeyDownFunction(); + key.setActive(true); + } else { + callback = key.getKeyUpFunction(); + key.setActive(false); + } + key.setActivityUpdateNeeded(false); + } + + if (callback) { + self._inputControlUnit[callback](); + } else { + if (key.getActive()) { + callback = key.getKeyFrameFunction(); + if (callback) { + self._inputControlUnit[callback](); + } + } + } + callback = null; + }); +} \ No newline at end of file diff --git a/lib/Chuck/Main.js b/lib/Chuck/Main.js index 3161838..fcec559 100755 --- a/lib/Chuck/Main.js +++ b/lib/Chuck/Main.js @@ -1,7 +1,5 @@ Chuck.Main = function () { - - // Settings were loaded automatically - + // Settings were loaded automatically var processor = new Chuck.Processor(); processor.init(); -}; +}; \ No newline at end of file diff --git a/lib/Chuck/Physics/Engine.js b/lib/Chuck/Physics/Engine.js index 5cc06fa..40ee5fa 100755 --- a/lib/Chuck/Physics/Engine.js +++ b/lib/Chuck/Physics/Engine.js @@ -1,6 +1,5 @@ Chuck.Physics.Engine = function () { this._world; - this.init(); } @@ -17,9 +16,10 @@ Chuck.Physics.Engine.prototype.getWorld = function() { return this._world; } -Chuck.Physics.Engine.prototype.setCollisionDetector = function() { - var cd = new CollisionDetector(); - this._world.SetContactListener(cd); +Chuck.Physics.Engine.prototype.setCollisionDetector = function(me) { + var cd = new Chuck.Collision.Detector(me); + var listener = cd.getListener(); + this._world.SetContactListener(listener); } Chuck.Physics.Engine.prototype.setupDebugDraw = function() { diff --git a/lib/Chuck/Player.js b/lib/Chuck/Player.js index 658136f..007ed85 100755 --- a/lib/Chuck/Player.js +++ b/lib/Chuck/Player.js @@ -1,7 +1,7 @@ Chuck.Player = function(engine, repository) { this._engine = engine; this._repository = repository; - this._standing; + this._standing = false; this._doll; this._mc; this._currentAnimationState = 'stand'; @@ -47,18 +47,18 @@ Chuck.Player.prototype.isStanding = function() { Chuck.Player.prototype.move = function(direction) { this._moveDirection = direction; - + switch(true) { case direction == this._lookDirection && this.isStanding(): - this._doll.move(direction, Settings.RUN_SPEED); + this._doll.move(direction, Chuck.Settings.RUN_SPEED); break; case !this.isStanding(): - this._doll.move(direction, Settings.FLY_SPEED); + this._doll.move(direction, Chuck.Settings.FLY_SPEED); break; default: - this._doll.move(direction, Settings.WALK_SPEED); + this._doll.move(direction, Chuck.Settings.WALK_SPEED); break; } @@ -107,7 +107,7 @@ Chuck.Player.prototype._animate = function(type) { return; } - this._mc.gotoAndPlay(type); + //this._mc.gotoAndPlay(type); this._currentAnimationState = type; } @@ -120,15 +120,15 @@ Chuck.Player.prototype._calculateWalkAnimation = function() { } Chuck.Player.prototype.look = function(x, y) { - var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180); + 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 < Settings.STAGE_WIDTH / 2) { + 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 >= Settings.STAGE_WIDTH / 2) { + } else if (x >= Chuck.Settings.STAGE_WIDTH / 2) { this._mc.scaleX = 1; this._lookDirection = 1; degree = (45 + -degree / 2) - 90; @@ -152,7 +152,7 @@ Chuck.Player.prototype._isWalking = function() { // called by CollisionDetection Chuck.Player.prototype.onFootSensorDetection = function(isColliding) { if(isColliding) { - if(this._doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) { + if(this._doll.getBody().GetLinearVelocity().y < -Chuck.Settings.JUMP_SPEED && !this.isStanding()) { return; } this.setStanding(true); @@ -167,7 +167,7 @@ Chuck.Player.prototype.onFootSensorDetection = function(isColliding) { } Chuck.Player.prototype.update = function() { - this._mc.head.y = this._mc.head_posmask.y; + //this._mc.head.y = this._mc.head_posmask.y; if (this._doll.getBody().GetLinearVelocity().x == 0 && this._isWalking()) { this.stop(); diff --git a/lib/Chuck/Processor.js b/lib/Chuck/Processor.js index df8d6c0..9f470e3 100755 --- a/lib/Chuck/Processor.js +++ b/lib/Chuck/Processor.js @@ -11,19 +11,15 @@ Chuck.Processor = function() { } Chuck.Processor.prototype.init = function() { + this._engine = new Chuck.Physics.Engine(); - - this._me = new Chuck.Player(this._engine, this._repository); - - //this._camera = Camera.getInstance() + this._me = new Chuck.Player(this._engine, this._repository); + //this._camera = Camera.getInstance() //this._repository = Repository.getInstance(); + this._engine.setCollisionDetector(this._me); + this._keyboardInput = new Chuck.Control.KeyboardInput(); + this._inputControlUnit = new Chuck.Control.InputControlUnit(this._keyboardInput, this._me); - //this._engine.setCollisionDetector(); - - //this._inputControlUnit = InputControlUnit.getInstance(); - //this._keyboardInput = KeyboardInput.getInstance(); - - new Chuck.Loader.Level(this._engine); //new Items(); @@ -50,10 +46,9 @@ Chuck.Processor.prototype._update = function(self) { self._engine.update(); // Order is important - /* - self._repository.update(); + + //self._repository.update(); self._keyboardInput.update(); self._me.update(); - self._camera.update(); - */ + //self._camera.update(); } \ No newline at end of file