Skip to content

Commit

Permalink
multi-directional kitty move... and restart x position too
Browse files Browse the repository at this point in the history
  • Loading branch information
courtyenn committed May 27, 2016
1 parent f22278c commit 7fd6f23
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h1 class="txt-center">PAUSED</h1>
<h3 class="txt-center">Press 'O' To Continue</h3>
</div>
<audio loop="true"></audio>
<canvas id="game" class="invisible"></canvas>
<canvas id="game" class="invisible">Your browser doesn't support canvas. :(</canvas>
<button id="mute">MUTE</button>
</div>
</div>
Expand Down
13 changes: 8 additions & 5 deletions public/js/keyHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ var KeyHandler = new function(){
var action = config.action;
var condition = config.conditionBeforeFiringNextAction;
var allowMultipleFire = config.allowMultipleFire || false;
var releaseAction = config.releaseAction || null;
if(this.keyMap[key] !== null){
this.keyMap[key] = {
keyDownAction: action,
pressed: false,
wasPressed: false,
allowMultipleFire: allowMultipleFire
allowMultipleFire: allowMultipleFire,
releaseAction: releaseAction
};
if(condition){
this.keyMap[key].condition = condition;
Expand All @@ -25,23 +27,24 @@ var KeyHandler = new function(){
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;
}
}
this.keyMap[key].wasPressed = false;
this.keyMap[key].pressed = true;
};

this.handleKeyRelease = function(key){
var key = String.fromCharCode(event.keyCode).toLowerCase();
if(this.keyMap[key] && this.keyMap[key].pressed){
this.keyMap[key].wasPressed = true;
this.keyMap[key].pressed = false;
if(this.keyMap[key].releaseAction){
this.keyMap[key].releaseAction();
}
}
}

Expand Down
33 changes: 25 additions & 8 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var kitty_jump = (function(){
var time = 0;
var player = {
startY: CANVAS_HEIGHT-100,
startX: 30,
x: 30,
y: CANVAS_HEIGHT-100,
yStep: 1,
Expand All @@ -49,13 +50,15 @@ var kitty_jump = (function(){
isJumping: false,
isFalling: false,
canJump: true,
moveDistance: 0,
draw: function(){
if(this.isJumping){
this.jump();
}
else if(this.isFalling) {
this.fall();
}
this.move();
context.drawImage(player.image, this.x, this.y);
},
shoot: function() {
Expand Down Expand Up @@ -114,12 +117,17 @@ var kitty_jump = (function(){
}
}
},
move: function(key){
move: function(){
if(currentGameState !== 300){
this.x += this.moveDistance;
}
},
setMove: function(key){
if(key === 'a'){
this.x -= 15;
this.moveDistance = -15;
}
else if(key === 'd'){
this.x += 15;
this.moveDistance = 15;
}
}
};
Expand Down Expand Up @@ -377,7 +385,6 @@ var kitty_jump = (function(){

I.explode = function() {
this.active = false;
// Extra Credit: Add an explosion graphic
};

return I;
Expand Down Expand Up @@ -553,18 +560,27 @@ var kitty_jump = (function(){
KeyHandler.keyMap = keyCodes;
var jumpConfig = {
key: 'w',
action: player.jumpOn.bind(player),
action: function(){
player.isJumping = true;
},
conditionBeforeFiringNextAction: checkIfPlayerShouldJump
};
var moveConfig = {
key: 'a',
action: player.move.bind(player, 'a'),
allowMultipleFire: true
action: function(){
player.moveDistance = -15;
},
allowMultipleFire: true,
releaseAction: function(){
player.moveDistance = 0;
}
};
KeyHandler.setKeyDownAction(jumpConfig);
KeyHandler.setKeyDownAction(moveConfig);
moveConfig.key = 'd';
moveConfig.action = player.move.bind(player, 'd');
moveConfig.action = function(){
player.moveDistance = 15;
};
KeyHandler.setKeyDownAction(moveConfig);

}
Expand Down Expand Up @@ -607,6 +623,7 @@ function loop(){
function init(){
player.lives.count = 9;
player.y = player.startY;
player.x = player.startX;
player.canJump = true;
player.isJumping = false;
player.isFalling = false;
Expand Down

0 comments on commit 7fd6f23

Please sign in to comment.