mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
control features implemented
This commit is contained in:
parent
8d0ff1a929
commit
c2b0baaea2
10 changed files with 213 additions and 63 deletions
|
|
@ -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() {
|
||||
|
|
|
|||
56
lib/Chuck/Control/Key.js
Executable file
56
lib/Chuck/Control/Key.js
Executable file
|
|
@ -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;
|
||||
}
|
||||
72
lib/Chuck/Control/KeyboardInput.js
Executable file
72
lib/Chuck/Control/KeyboardInput.js
Executable file
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue