fixed input controllerism - fixes #28

This commit is contained in:
Logsol 2013-07-30 18:13:39 +02:00
parent a59a258ad6
commit f5eacb6335
13 changed files with 163 additions and 223 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
node_modules/ node_modules/
*.log *.log
.DS_Store .DS_Store
.floo
.sublime-project

View file

@ -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;
});

View file

@ -1,9 +1,9 @@
define(["Game/Client/Control/Key"], function (Key) { define(["Game/Client/Control/Key"], function (Key) {
function KeyboardInput (keyboardController) { function KeyboardInput (playerController) {
this._registry = {}; this._registry = {};
this._keyboardController = keyboardController; this._playerController = playerController;
this.init(); this.init();
} }
@ -16,9 +16,9 @@ define(["Game/Client/Control/Key"], function (Key) {
KeyboardInput.prototype.registerKey = function (keyCode, onKeyDown, onKeyUp, onKeyFrame) { KeyboardInput.prototype.registerKey = function (keyCode, onKeyDown, onKeyUp, onKeyFrame) {
var key = new Key(); var key = new Key();
key.setKeyDownFunction(onKeyDown); if(onKeyDown) key.setKeyDownFunction(onKeyDown);
key.setKeyUpFunction(onKeyUp); if(onKeyUp) key.setKeyUpFunction(onKeyUp);
key.setKeyFrameFunction(onKeyFrame); if(onKeyFrame) key.setKeyFrameFunction(onKeyFrame);
this._registry[keyCode] = key; this._registry[keyCode] = key;
} }
@ -28,48 +28,39 @@ define(["Game/Client/Control/Key"], function (Key) {
KeyboardInput.prototype._onKeyDown = function (e) { KeyboardInput.prototype._onKeyDown = function (e) {
var key = this._getKeyByKeyCode(e.keyCode); var key = this._getKeyByKeyCode(e.keyCode);
if (key && key.getActive() == false) {
key.setActivityUpdateStatus(true); if (key && !key.getActive()) {
key.setActivityUpdateNeeded(true); var callback = key.getKeyDownFunction();
if(callback) this._playerController[callback]();
key.setActive(true);
} }
} }
KeyboardInput.prototype._onKeyUp = function (e) { KeyboardInput.prototype._onKeyUp = function (e) {
var key = this._getKeyByKeyCode(e.keyCode); var key = this._getKeyByKeyCode(e.keyCode);
if (key != null) { if (key && key.getActive()) {
key.setActivityUpdateStatus(false); var callback = key.getKeyUpFunction();
key.setActivityUpdateNeeded(true); if(callback) this._playerController[callback]();
}
}
KeyboardInput.prototype.update = function () {
var callback = null;
var self = this;
for (var keyCode in this._registry) {
var key = this._registry[keyCode];
if (key.getActivityUpdateNeeded()) {
if (key.getActivityUpdateStatus() == true) {
callback = key.getKeyDownFunction();
key.setActive(true);
} else {
callback = key.getKeyUpFunction();
key.setActive(false); key.setActive(false);
} }
key.setActivityUpdateNeeded(false);
} }
if (callback) { /*
self._keyboardController[callback](); * If KeyFrameFunction was set, it is executed when key is active
} else { */
KeyboardInput.prototype.update = function () {
var callback = null;
for (var keyCode in this._registry) {
var key = this._getKeyByKeyCode(keyCode);
if (key.getActive()) { if (key.getActive()) {
callback = key.getKeyFrameFunction(); callback = key.getKeyFrameFunction();
if (callback) { if (callback) {
self._keyboardController[callback](); this._playerController[callback]();
}
} }
} }
callback = null; callback = null;
} }
} }

View file

@ -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;
});

View file

@ -2,12 +2,12 @@ define([
"Game/Core/GameController", "Game/Core/GameController",
"Game/Client/Physics/Engine", "Game/Client/Physics/Engine",
"Game/Client/View/ViewController", "Game/Client/View/ViewController",
"Game/Client/Control/KeyboardController", "Game/Client/Control/PlayerController",
"Game/Core/NotificationCenter", "Game/Core/NotificationCenter",
"Lib/Utilities/RequestAnimFrame" "Lib/Utilities/RequestAnimFrame"
], ],
function (Parent, PhysicsEngine, ViewController, KeyboardController, NotificationCenter, requestAnimFrame) { function (Parent, PhysicsEngine, ViewController, PlayerController, NotificationCenter, requestAnimFrame) {
function GameController () { function GameController () {
this.viewController = new ViewController(); this.viewController = new ViewController();
@ -17,7 +17,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
this.physicsEngine.setCollisionDetector(); this.physicsEngine.setCollisionDetector();
this.me = null; this.me = null;
this.keyboardController = null;
this.update(); this.update();
} }
@ -40,7 +39,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
this.viewController.update(); this.viewController.update();
if(this.me) { if(this.me) {
this.keyboardController.update();
this.me.update(); this.me.update();
} }
} }
@ -62,11 +60,10 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
} }
GameController.prototype.onJoinMe = function (playerId) { GameController.prototype.onJoinMe = function (playerId) {
//this.onSpawnPlayer(options); //this.onSpawnPlayer(options);
this.me = this.players[playerId]; this.me = this.players[playerId];
this.keyboardController = new KeyboardController(this.me, this); this.me.setPlayerController(new PlayerController(this.me));
} }
GameController.prototype.onSpawnPlayer = function(options) { GameController.prototype.onSpawnPlayer = function(options) {
@ -76,9 +73,7 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
var player = this.players[playerId]; var player = this.players[playerId];
player.spawn(x, y); player.spawn(x, y);
}
};
return GameController; return GameController;
}); });

View file

@ -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;
});

View file

@ -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;
});

View file

@ -112,10 +112,12 @@ function (Box2D, Settings, CollisionDetector) {
// to prevent higher jumping running uphill, etc. // to prevent higher jumping running uphill, etc.
} }
/*
Doll.prototype.jumping = function () { Doll.prototype.jumping = function () {
var vector = new Box2D.Common.Math.b2Vec2(0, -0.05); var vector = new Box2D.Common.Math.b2Vec2(0, -0.05);
this.body.ApplyImpulse(vector, this.body.GetPosition()); this.body.ApplyImpulse(vector, this.body.GetPosition());
} }
*/
Doll.prototype.destroy = function () { Doll.prototype.destroy = function () {
this.body.GetWorld().DestroyBody(this.body); this.body.GetWorld().DestroyBody(this.body);

View file

@ -15,7 +15,7 @@ function (Doll, Settings) {
this.lookDirection = 1; this.lookDirection = 1;
this.moveDirection = 0; this.moveDirection = 0;
this.isSpawned = false; this.isSpawned = false;
this.playerController = null;
} }
Player.prototype.spawn = function (x, y) { Player.prototype.spawn = function (x, y) {
@ -177,11 +177,19 @@ function (Doll, Settings) {
if (!this.doll.getBody().IsAwake() && !this.isStanding()) { if (!this.doll.getBody().IsAwake() && !this.isStanding()) {
this.setStanding(true); this.setStanding(true);
} }
if(this.playerController) {
this.playerController.update();
}
} }
Player.prototype.destroy = function () { Player.prototype.destroy = function () {
this.doll.destroy(); this.doll.destroy();
} }
Player.prototype.setPlayerController = function(playerController) {
this.playerController = playerController;
}
return Player; return Player;
}); });

View file

@ -1,19 +1,22 @@
define([ define([
"Game/Core/Control/InputController", "Game/Core/Control/PlayerController",
"Game/Core/NotificationCenter", "Game/Core/NotificationCenter",
"Game/Core/Protocol/Parser" "Game/Core/Protocol/Parser"
], ],
function(Parent, NotificationCenter, Parser) { function(Parent, NotificationCenter, Parser) {
function InputController(player) { function PlayerController(player) {
Parent.call(this, 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; var message;
if (typeof options == "string") { if (typeof options == "string") {
message = Parser.decode(options); message = Parser.decode(options);
@ -26,6 +29,6 @@ function(Parent, NotificationCenter, Parser) {
} }
}; };
return InputController; return PlayerController;
}); });

View file

@ -2,13 +2,13 @@ define([
"Game/Core/GameController", "Game/Core/GameController",
"Game/Core/Physics/Engine", "Game/Core/Physics/Engine",
"Game/Config/Settings", "Game/Config/Settings",
"Game/Server/Control/InputController", "Game/Server/Control/PlayerController",
"Lib/Utilities/RequestAnimFrame", "Lib/Utilities/RequestAnimFrame",
"Game/Core/NotificationCenter", "Game/Core/NotificationCenter",
"Game/Server/Player" "Game/Server/Player"
], ],
function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter, Player) { function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player) {
function GameController (channel) { function GameController (channel) {
Parent.call(this, new PhysicsEngine()); Parent.call(this, new PhysicsEngine());
@ -62,7 +62,7 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No
GameController.prototype.createPlayer = function(user) { GameController.prototype.createPlayer = function(user) {
var player = new Player(user.id, this.physicsEngine); var player = new Player(user.id, this.physicsEngine);
player.setInputController(new InputController(player)) player.setPlayerController(new PlayerController(player))
return player; return player;
}; };

View file

@ -4,18 +4,6 @@ define([
function(Parent) { function(Parent) {
function Player(id, physicsEngine) { return Parent;
Parent.call(this, id, physicsEngine);
this.inputController = null;
}
Player.prototype = Object.create(Parent.prototype);
Player.prototype.setInputController = function(inputController) {
this.inputController = inputController;
}
return Player;
}); });

View file

@ -38,7 +38,7 @@ function(Parent, NotificationCenter, ProtocolHelper) {
// User command callbacks // User command callbacks
User.prototype.onGameCommand = function(command) { User.prototype.onGameCommand = function(command) {
this.player.inputController.applyCommand(command); this.player.playerController.applyCommand(command);
}; };