Skip to content

Commit

Permalink
Fixed bug with jumping & made speed directional
Browse files Browse the repository at this point in the history
  • Loading branch information
basicallydan committed Feb 17, 2013
1 parent 158f797 commit c91d080
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
15 changes: 9 additions & 6 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ function startGame (images) {

mainCanvas.width = mainCanvas.width;

var mouseCanvasPosition = dContext.canvasPositionToMapPosition([mouseX, mouseY]);

if (!skier.isJumping) {
var mouseCanvasPosition = dContext.canvasPositionToMapPosition([mouseX, mouseY]);
skier.setMapPositionTarget(mouseCanvasPosition[0], mouseCanvasPosition[1]);
} else {
skier.setMapPositionTarget(undefined, mouseCanvasPosition[1]);
}

skier.draw(dContext);
Expand Down Expand Up @@ -203,7 +206,7 @@ function startGame (images) {
'High Score: ' + highScore,
'Created by Dan Hough (@basicallydan)',
'Current Speed: ' + skier.getSpeed(),
'Skier Position: ' + skier.mapPosition[0] + ', ' + skier.mapPosition[1]
'Skier Position: ' + skier.mapPosition[0].toFixed(1) + ', ' + skier.mapPosition[1].toFixed(1)
]);

infoBox.draw(dContext);
Expand All @@ -229,22 +232,22 @@ function startGame (images) {
skier.speedBoost();
}
//W Key
if (e.Keycode === 87 || e.keyCode === 119) {
if (e.Keycode === 87 || e.keyCode === 119 || e.keyCode === 38) {
mouseX = 0;
mouseY = 0;
}
// A Key
if (e.keyCode === 65 || e.keyCode === 97) {
if (e.keyCode === 65 || e.keyCode === 97 || e.keyCode === 37) {
mouseX = 0;
mouseY = mainCanvas.height;
}
// S Key
if (e.keyCode === 83 || e.keyCode === 115) {
if (e.keyCode === 83 || e.keyCode === 115 || e.keyCode === 40) {
mouseX = mainCanvas.width / 2;
mouseY = mainCanvas.height;
}
// D Key
if (e.keyCode === 68 || e.keyCode === 100) {
if (e.keyCode === 68 || e.keyCode === 100 || e.keyCode === 39) {
mouseX = mainCanvas.width;
mouseY = mainCanvas.height;
}
Expand Down
26 changes: 26 additions & 0 deletions js/skier.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var Sprite = require('./Sprite');
var sup = {
draw: that.superior('draw'),
cycle: that.superior('cycle'),
getSpeedX: that.superior('getSpeedX'),
getSpeedY: that.superior('getSpeedY'),
hits: that.superior('hits')
};
var directions = {
Expand Down Expand Up @@ -152,6 +154,30 @@ var Sprite = require('./Sprite');
}
};

that.getSpeedX = function () {
if (getDirection() === 'esEast' || getDirection() === 'wsWest') {
return that.getSpeed() * 0.5;
}

if (getDirection() === 'sEast' || getDirection() === 'sWest') {
return that.getSpeed() * 0.33;
}

return 0;
};

that.getSpeedY = function () {
if (getDirection() === 'esEast' || getDirection() === 'wsWest') {
return that.getSpeed() * 0.6;
}

if (getDirection() === 'sEast' || getDirection() === 'sWest') {
return that.getSpeed() * 0.85;
}

return that.getSpeed();
};

that.hasHitObstacle = function (obs) {
that.isMoving = false;
that.hasBeenHit = true;
Expand Down
18 changes: 12 additions & 6 deletions js/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
that.canvasZ = 0;
that.height = 0;
that.speed = 0;
that.speedX = that.speed;
that.speedY = that.speed;
that.data = data || { parts : {} };
that.movingToward = [ 0, 0 ];
that.metresDownTheMountain = 0;
Expand Down Expand Up @@ -63,17 +61,17 @@

if (typeof that.movingToward[0] !== 'undefined') {
if (currentX > that.movingToward[0]) {
currentX -= Math.min(that.speedX, Math.abs(currentX - that.movingToward[0]));
currentX -= Math.min(that.getSpeedX(), Math.abs(currentX - that.movingToward[0]));
} else if (currentX < that.movingToward[0]) {
currentX += Math.min(that.speedX, Math.abs(currentX - that.movingToward[0]));
currentX += Math.min(that.getSpeedX(), Math.abs(currentX - that.movingToward[0]));
}
}

if (typeof that.movingToward[1] !== 'undefined') {
if (currentY > that.movingToward[1]) {
currentY -= Math.min(that.speedY, Math.abs(currentY - that.movingToward[1]));
currentY -= Math.min(that.getSpeedY(), Math.abs(currentY - that.movingToward[1]));
} else if (currentY < that.movingToward[1]) {
currentY += Math.min(that.speedY, Math.abs(currentY - that.movingToward[1]));
currentY += Math.min(that.getSpeedY(), Math.abs(currentY - that.movingToward[1]));
}
}

Expand Down Expand Up @@ -190,6 +188,14 @@
return that.speed;
};

that.getSpeedX = function () {
return that.speed;
};

that.getSpeedY = function () {
return that.speed;
};

this.setHeight = function setHeight (h) {
that.height = h;
};
Expand Down

0 comments on commit c91d080

Please sign in to comment.