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

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

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