Skip to content

Commit

Permalink
need to incorporate keyhandler into the game loop
Browse files Browse the repository at this point in the history
  • Loading branch information
courtyenn committed May 27, 2016
1 parent 594534e commit f22278c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
27 changes: 16 additions & 11 deletions public/js/keyHandler.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
var KeyHandler = new function(){
this.keyMap = {};
this.actionInProgress = false;
this.setKeyDownAction = function(key, action, conditionBeforeFiringNextAction){
this.setKeyDownAction = function(config){
var key = config.key;
var action = config.action;
var condition = config.conditionBeforeFiringNextAction;
var allowMultipleFire = config.allowMultipleFire || false;
if(this.keyMap[key] !== null){
this.keyMap[key] = {
keyDownAction: action,
pressed: false,
wasPressed: false
wasPressed: false,
allowMultipleFire: allowMultipleFire
};
if(conditionBeforeFiringNextAction){
this.keyMap[key].condition = conditionBeforeFiringNextAction;
if(condition){
this.keyMap[key].condition = condition;
}
}
};

this.handleKeyDown = function(event){
var key = String.fromCharCode(event.keyCode).toLowerCase();
if(this.keyMap[key] && this.keyMap[key].keyDownAction){
if(!this.keyMap[key].pressed){
if(this.keyMap[key].condition()){
this.keyMap[key].keyDownAction();
this.keyMap[key].wasPressed = false;
this.keyMap[key].pressed = true;
}
else if(typeof this.keyMap[key].condition === 'undefined'){
if(!this.keyMap[key].pressed && this.keyMap[key].allowMultipleFire === false){
if(this.keyMap[key].condition && this.keyMap[key].condition()){
this.keyMap[key].keyDownAction();
this.keyMap[key].wasPressed = false;
this.keyMap[key].pressed = true;
}
}
else if(this.keyMap[key].allowMultipleFire === true){
this.keyMap[key].keyDownAction();
this.keyMap[key].wasPressed = false;
this.keyMap[key].pressed = false;
}
}
};

Expand Down
47 changes: 34 additions & 13 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,28 @@ var kitty_jump = (function(){
},
fall: function(){
if(currentGameState !== 300){
this.velocityY = gravity * this.time;
this.y += (this.velocityY);
if(this.y >= (this.startY) ){
this.isFalling = false;
this.isJumping = false;
this.canJump = true;
this.time = 0;
this.y = this.startY;
this.velocityY = gravity * this.time;
this.y += (this.velocityY);
if(this.y >= (this.startY) ){
this.isFalling = false;
this.isJumping = false;
this.canJump = true;
this.time = 0;
this.y = this.startY;
}
else {
this.time = this.time + .22;
}
}
else {

this.time = this.time + .22;
},
move: function(key){
if(key === 'a'){
this.x -= 15;
}
else if(key === 'd'){
this.x += 15;
}
}
}
};

gamestates = {
Expand Down Expand Up @@ -544,7 +551,21 @@ var kitty_jump = (function(){
gameDead.volume = .1;

KeyHandler.keyMap = keyCodes;
KeyHandler.setKeyDownAction('w', player.jumpOn.bind(player), checkIfPlayerShouldJump);
var jumpConfig = {
key: 'w',
action: player.jumpOn.bind(player),
conditionBeforeFiringNextAction: checkIfPlayerShouldJump
};
var moveConfig = {
key: 'a',
action: player.move.bind(player, 'a'),
allowMultipleFire: true
};
KeyHandler.setKeyDownAction(jumpConfig);
KeyHandler.setKeyDownAction(moveConfig);
moveConfig.key = 'd';
moveConfig.action = player.move.bind(player, 'd');
KeyHandler.setKeyDownAction(moveConfig);

}
function checkIfPlayerShouldJump(){
Expand Down

0 comments on commit f22278c

Please sign in to comment.