From f5eacb6335ff29e00407fc8f6048de99a25a9699 Mon Sep 17 00:00:00 2001 From: Logsol Date: Tue, 30 Jul 2013 18:13:39 +0200 Subject: [PATCH] fixed input controllerism - fixes #28 --- .gitignore | 2 + app/Game/Client/Control/KeyboardController.js | 100 ------------------ app/Game/Client/Control/KeyboardInput.js | 55 ++++------ app/Game/Client/Control/PlayerController.js | 70 ++++++++++++ app/Game/Client/GameController.js | 13 +-- app/Game/Core/Control/InputController.js | 59 ----------- app/Game/Core/Control/PlayerController.js | 40 +++++++ app/Game/Core/Physics/Doll.js | 2 + app/Game/Core/Player.js | 10 +- ...InputController.js => PlayerController.js} | 13 ++- app/Game/Server/GameController.js | 6 +- app/Game/Server/Player.js | 14 +-- app/Game/Server/User.js | 2 +- 13 files changed, 163 insertions(+), 223 deletions(-) delete mode 100755 app/Game/Client/Control/KeyboardController.js create mode 100755 app/Game/Client/Control/PlayerController.js delete mode 100755 app/Game/Core/Control/InputController.js create mode 100755 app/Game/Core/Control/PlayerController.js rename app/Game/Server/Control/{InputController.js => PlayerController.js} (56%) diff --git a/.gitignore b/.gitignore index 3c45938..e4d81d5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules/ *.log .DS_Store +.floo +.sublime-project diff --git a/app/Game/Client/Control/KeyboardController.js b/app/Game/Client/Control/KeyboardController.js deleted file mode 100755 index 6c36bb8..0000000 --- a/app/Game/Client/Control/KeyboardController.js +++ /dev/null @@ -1,100 +0,0 @@ -define([ - "Game/Core/Control/InputController", - "Game/Client/Control/KeyboardInput", - "Game/Core/NotificationCenter" -], - -function (InputController, KeyboardInput, NotificationCenter) { - - function KeyboardController (me, gameController) { - - this.gameController = gameController; - this.inputController = new InputController(me); - - this.keyboardInput = new KeyboardInput(this); - - var keys = { - w:87, - a:65, - s:83, - d:68, - - up: 38, - left: 37, - down: 40, - right: 39 - } - - this.init(keys); - } - - KeyboardController.prototype.init = function (keys) { - - this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop', 'moveLeft'); - this.keyboardInput.registerKey(keys.left, 'moveLeft', 'stop', 'moveLeft'); - - this.keyboardInput.registerKey(keys.d, 'moveRight', 'stop', 'moveRight'); - this.keyboardInput.registerKey(keys.right, 'moveRight', 'stop', 'moveRight'); - - this.keyboardInput.registerKey(keys.w, 'jump', 'jumped', 'jumping'); - this.keyboardInput.registerKey(keys.up, 'jump', 'jumped', 'jumping'); - - this.keyboardInput.registerKey(keys.s, 'duck', 'standUp', 'duck'); - this.keyboardInput.registerKey(keys.down, 'duck', 'standUp', 'duck'); - - this.keyboardInput.registerKey(keys.s, 'activateShift', 'activateShift', 'deactivateShift'); - this.keyboardInput.registerKey(keys.down, 'activateShift', 'activateShift', 'deactivateShift'); - } - - KeyboardController.prototype.moveLeft = function () { - this.inputController.moveLeft(); - NotificationCenter.trigger('sendGameCommand', 'moveLeft'); - } - - KeyboardController.prototype.moveRight = function () { - this.inputController.moveRight(); - NotificationCenter.trigger('sendGameCommand', 'moveRight'); - } - - KeyboardController.prototype.stop = function () { - this.inputController.stop(); - NotificationCenter.trigger('sendGameCommand', 'stop'); - } - - KeyboardController.prototype.jump = function () { - this.inputController.jump(); - NotificationCenter.trigger('sendGameCommand', 'jump'); - } - - KeyboardController.prototype.jumped = function () { - this.inputController.jumped(); - } - - KeyboardController.prototype.jumping = function () { - this.inputController.jumping(); - } - - KeyboardController.prototype.duck = function () { - this.inputController.duck(); - NotificationCenter.trigger('sendGameCommand', 'duck'); - } - - KeyboardController.prototype.standUp = function () { - this.inputController.standUp(); - } - - KeyboardController.prototype.activateShift = function () { - this.inputController.activateShift(); - NotificationCenter.trigger('sendGameCommand', 'activateShift'); - } - - KeyboardController.prototype.deactivateShift = function () { - this.inputController.deactivateShift(); - } - - KeyboardController.prototype.update = function () { - this.keyboardInput.update(); - } - - return KeyboardController; -}); \ No newline at end of file diff --git a/app/Game/Client/Control/KeyboardInput.js b/app/Game/Client/Control/KeyboardInput.js index 9e87bd8..5f2999d 100755 --- a/app/Game/Client/Control/KeyboardInput.js +++ b/app/Game/Client/Control/KeyboardInput.js @@ -1,9 +1,9 @@ define(["Game/Client/Control/Key"], function (Key) { - function KeyboardInput (keyboardController) { + function KeyboardInput (playerController) { this._registry = {}; - this._keyboardController = keyboardController; + this._playerController = playerController; this.init(); } @@ -16,9 +16,9 @@ define(["Game/Client/Control/Key"], function (Key) { KeyboardInput.prototype.registerKey = function (keyCode, onKeyDown, onKeyUp, onKeyFrame) { var key = new Key(); - key.setKeyDownFunction(onKeyDown); - key.setKeyUpFunction(onKeyUp); - key.setKeyFrameFunction(onKeyFrame); + if(onKeyDown) key.setKeyDownFunction(onKeyDown); + if(onKeyUp) key.setKeyUpFunction(onKeyUp); + if(onKeyFrame) key.setKeyFrameFunction(onKeyFrame); this._registry[keyCode] = key; } @@ -28,48 +28,39 @@ define(["Game/Client/Control/Key"], function (Key) { KeyboardInput.prototype._onKeyDown = function (e) { var key = this._getKeyByKeyCode(e.keyCode); - if (key && key.getActive() == false) { - key.setActivityUpdateStatus(true); - key.setActivityUpdateNeeded(true); + + if (key && !key.getActive()) { + var callback = key.getKeyDownFunction(); + if(callback) this._playerController[callback](); + key.setActive(true); } } KeyboardInput.prototype._onKeyUp = function (e) { var key = this._getKeyByKeyCode(e.keyCode); - if (key != null) { - key.setActivityUpdateStatus(false); - key.setActivityUpdateNeeded(true); + if (key && key.getActive()) { + var callback = key.getKeyUpFunction(); + if(callback) this._playerController[callback](); + key.setActive(false); } } + /* + * If KeyFrameFunction was set, it is executed when key is active + */ KeyboardInput.prototype.update = function () { var callback = null; - var self = this; for (var keyCode in this._registry) { - var key = this._registry[keyCode]; + var key = this._getKeyByKeyCode(keyCode); - 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._keyboardController[callback](); - } else { - if (key.getActive()) { - callback = key.getKeyFrameFunction(); - if (callback) { - self._keyboardController[callback](); - } + if (key.getActive()) { + callback = key.getKeyFrameFunction(); + if (callback) { + this._playerController[callback](); } } + callback = null; } } diff --git a/app/Game/Client/Control/PlayerController.js b/app/Game/Client/Control/PlayerController.js new file mode 100755 index 0000000..1f6fe48 --- /dev/null +++ b/app/Game/Client/Control/PlayerController.js @@ -0,0 +1,70 @@ +define([ + "Game/Core/Control/PlayerController", + "Game/Client/Control/KeyboardInput", + "Game/Core/NotificationCenter" +], + +function (Parent, KeyboardInput, NotificationCenter) { + + function PlayerController (me) { + + Parent.call(this, me); + + this.keyboardInput = new KeyboardInput(this); + + var keys = { + w:87, + a:65, + s:83, + d:68, + + up: 38, + left: 37, + down: 40, + right: 39 + } + + this.init(keys); + } + + PlayerController.prototype = Object.create(Parent.prototype); + + PlayerController.prototype.init = function (keys) { + + this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop'); + this.keyboardInput.registerKey(keys.left, 'moveLeft', 'stop'); + + this.keyboardInput.registerKey(keys.d, 'moveRight', 'stop'); + this.keyboardInput.registerKey(keys.right, 'moveRight', 'stop'); + + this.keyboardInput.registerKey(keys.w, 'jump'); + this.keyboardInput.registerKey(keys.up, 'jump'); + } + + PlayerController.prototype.moveLeft = function () { + Parent.prototype.moveLeft.call(this); + NotificationCenter.trigger('sendGameCommand', 'moveLeft'); + } + + PlayerController.prototype.moveRight = function () { + Parent.prototype.moveRight.call(this); + NotificationCenter.trigger('sendGameCommand', 'moveRight'); + } + + PlayerController.prototype.stop = function () { + Parent.prototype.stop.call(this); + NotificationCenter.trigger('sendGameCommand', 'stop'); + } + + PlayerController.prototype.jump = function () { + Parent.prototype.jump.call(this); + NotificationCenter.trigger('sendGameCommand', 'jump'); + } + + PlayerController.prototype.update = function () { + this.keyboardInput.update(); + Parent.prototype.update.call(this); + } + + return PlayerController; +}); \ No newline at end of file diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index ee7cffc..a8558ad 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -2,12 +2,12 @@ define([ "Game/Core/GameController", "Game/Client/Physics/Engine", "Game/Client/View/ViewController", - "Game/Client/Control/KeyboardController", + "Game/Client/Control/PlayerController", "Game/Core/NotificationCenter", "Lib/Utilities/RequestAnimFrame" ], -function (Parent, PhysicsEngine, ViewController, KeyboardController, NotificationCenter, requestAnimFrame) { +function (Parent, PhysicsEngine, ViewController, PlayerController, NotificationCenter, requestAnimFrame) { function GameController () { this.viewController = new ViewController(); @@ -17,7 +17,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio this.physicsEngine.setCollisionDetector(); this.me = null; - this.keyboardController = null; this.update(); } @@ -40,7 +39,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio this.viewController.update(); if(this.me) { - this.keyboardController.update(); this.me.update(); } } @@ -62,11 +60,10 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio } - GameController.prototype.onJoinMe = function (playerId) { //this.onSpawnPlayer(options); this.me = this.players[playerId]; - this.keyboardController = new KeyboardController(this.me, this); + this.me.setPlayerController(new PlayerController(this.me)); } GameController.prototype.onSpawnPlayer = function(options) { @@ -76,9 +73,7 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio var player = this.players[playerId]; player.spawn(x, y); - - - }; + } return GameController; }); diff --git a/app/Game/Core/Control/InputController.js b/app/Game/Core/Control/InputController.js deleted file mode 100755 index 3d2e7a9..0000000 --- a/app/Game/Core/Control/InputController.js +++ /dev/null @@ -1,59 +0,0 @@ -define(function () { - - function InputController (player) { - - this.player = player; - - this._shift; - this._isJumping; - } - - InputController.prototype.moveLeft = function () { - this.player.move(-1); - } - - InputController.prototype.moveRight = function () { - this.player.move(1); - } - - InputController.prototype.stop = function () { - this.player.stop(); - } - - InputController.prototype.jump = function () { - this._isJumping = true; - this.player.jump(); - } - - InputController.prototype.jumped = function () { - this._isJumping = false; - } - - InputController.prototype.jumping = function () { - if (this._isJumping) { - this.player.jumping(); - } - } - - InputController.prototype.duck = function () { - this.player.duck(); - } - - InputController.prototype.standUp = function () { - this.player.standUp(); - } - - InputController.prototype.activateShift = function () { - this._shift = true; - } - - InputController.prototype.deactivateShift = function () { - this._shift = false; - } - - InputController.prototype.update = function () { - - } - - return InputController; -}); \ No newline at end of file diff --git a/app/Game/Core/Control/PlayerController.js b/app/Game/Core/Control/PlayerController.js new file mode 100755 index 0000000..b940648 --- /dev/null +++ b/app/Game/Core/Control/PlayerController.js @@ -0,0 +1,40 @@ +define(function () { + + function PlayerController (player) { + + this.player = player; + + this._shift; + this._isJumping; + this._walkingDirectionStatus = 0; + } + + PlayerController.prototype.moveLeft = function () { + this.player.move(-1); + this._walkingDirectionStatus = -1; + } + + PlayerController.prototype.moveRight = function () { + this.player.move(1); + this._walkingDirectionStatus = 1; + } + + PlayerController.prototype.stop = function () { + this.player.stop(); + this._walkingDirectionStatus = 0; + } + + PlayerController.prototype.jump = function () { + this._isJumping = true; + this.player.jump(); + } + + PlayerController.prototype.update = function () { + //console.log(this._walkingDirectionStatus) + if(this._walkingDirectionStatus != 0) { + this.player.move(this._walkingDirectionStatus); + } + } + + return PlayerController; +}); \ No newline at end of file diff --git a/app/Game/Core/Physics/Doll.js b/app/Game/Core/Physics/Doll.js index fc0db2f..acb04ac 100755 --- a/app/Game/Core/Physics/Doll.js +++ b/app/Game/Core/Physics/Doll.js @@ -112,10 +112,12 @@ function (Box2D, Settings, CollisionDetector) { // to prevent higher jumping running uphill, etc. } + /* Doll.prototype.jumping = function () { var vector = new Box2D.Common.Math.b2Vec2(0, -0.05); this.body.ApplyImpulse(vector, this.body.GetPosition()); } + */ Doll.prototype.destroy = function () { this.body.GetWorld().DestroyBody(this.body); diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index 8545004..02280cd 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -15,7 +15,7 @@ function (Doll, Settings) { this.lookDirection = 1; this.moveDirection = 0; this.isSpawned = false; - + this.playerController = null; } Player.prototype.spawn = function (x, y) { @@ -177,11 +177,19 @@ function (Doll, Settings) { if (!this.doll.getBody().IsAwake() && !this.isStanding()) { this.setStanding(true); } + + if(this.playerController) { + this.playerController.update(); + } } Player.prototype.destroy = function () { this.doll.destroy(); } + Player.prototype.setPlayerController = function(playerController) { + this.playerController = playerController; + } + return Player; }); diff --git a/app/Game/Server/Control/InputController.js b/app/Game/Server/Control/PlayerController.js similarity index 56% rename from app/Game/Server/Control/InputController.js rename to app/Game/Server/Control/PlayerController.js index ba44ca2..2fce9b1 100644 --- a/app/Game/Server/Control/InputController.js +++ b/app/Game/Server/Control/PlayerController.js @@ -1,19 +1,22 @@ define([ - "Game/Core/Control/InputController", + "Game/Core/Control/PlayerController", "Game/Core/NotificationCenter", "Game/Core/Protocol/Parser" ], function(Parent, NotificationCenter, Parser) { - function InputController(player) { + function PlayerController(player) { Parent.call(this, player); } - InputController.prototype = Object.create(Parent.prototype); + PlayerController.prototype = Object.create(Parent.prototype); - InputController.prototype.applyCommand = function(options) { + /* + * retrieves move (and other) commands from client and executes them at the server + */ + PlayerController.prototype.applyCommand = function(options) { var message; if (typeof options == "string") { message = Parser.decode(options); @@ -26,6 +29,6 @@ function(Parent, NotificationCenter, Parser) { } }; - return InputController; + return PlayerController; }); \ No newline at end of file diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 849d2a5..3227363 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -2,13 +2,13 @@ define([ "Game/Core/GameController", "Game/Core/Physics/Engine", "Game/Config/Settings", - "Game/Server/Control/InputController", + "Game/Server/Control/PlayerController", "Lib/Utilities/RequestAnimFrame", "Game/Core/NotificationCenter", "Game/Server/Player" ], -function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter, Player) { +function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player) { function GameController (channel) { Parent.call(this, new PhysicsEngine()); @@ -62,7 +62,7 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No GameController.prototype.createPlayer = function(user) { var player = new Player(user.id, this.physicsEngine); - player.setInputController(new InputController(player)) + player.setPlayerController(new PlayerController(player)) return player; }; diff --git a/app/Game/Server/Player.js b/app/Game/Server/Player.js index 7922336..c5398e5 100644 --- a/app/Game/Server/Player.js +++ b/app/Game/Server/Player.js @@ -3,19 +3,7 @@ define([ ], function(Parent) { - - function Player(id, physicsEngine) { - Parent.call(this, id, physicsEngine); - this.inputController = null; - } - - Player.prototype = Object.create(Parent.prototype); - - Player.prototype.setInputController = function(inputController) { - this.inputController = inputController; - } - - return Player; + return Parent; }); \ No newline at end of file diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index ebcf8f5..c5b3c54 100644 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -38,7 +38,7 @@ function(Parent, NotificationCenter, ProtocolHelper) { // User command callbacks User.prototype.onGameCommand = function(command) { - this.player.inputController.applyCommand(command); + this.player.playerController.applyCommand(command); };