mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
fixed input controllerism - fixes #28
This commit is contained in:
parent
a59a258ad6
commit
f5eacb6335
13 changed files with 163 additions and 223 deletions
|
|
@ -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;
|
||||
});
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
70
app/Game/Client/Control/PlayerController.js
Executable file
70
app/Game/Client/Control/PlayerController.js
Executable 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;
|
||||
});
|
||||
|
|
@ -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;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue