Skip to content

Commit

Permalink
Added turning just like the original skifree
Browse files Browse the repository at this point in the history
  • Loading branch information
basicallydan committed Mar 3, 2014
1 parent d4d0391 commit a7d0975
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 59 deletions.
6 changes: 0 additions & 6 deletions js/lib/game.js
Expand Up @@ -70,13 +70,7 @@ var EventedLoop = require('eventedloop');
var mouseMapPosition = dContext.canvasPositionToMapPosition([mouseX, mouseY]);

if (!player.isJumping) {
if (intervalNum === 0) {
console.log('Skier targets: ' + player.movingToward[0] + ', ' + player.movingToward[1]);
}
player.setMapPositionTarget(mouseMapPosition[0], mouseMapPosition[1]);
if (intervalNum === 0) {
console.log('Skier targets: ' + player.movingToward[0] + ', ' + player.movingToward[1]);
}
}

intervalNum++;
Expand Down
165 changes: 140 additions & 25 deletions js/lib/skier.js
Expand Up @@ -12,6 +12,15 @@ if (typeof navigator !== 'undefined') {

(function(global) {
function Skier(data) {
var discreteDirections = {
'west': 270,
'wsWest': 240,
'sWest': 195,
'south': 180,
'sEast': 165,
'esEast': 120,
'east': 90
};
var that = new Sprite(data);
var sup = {
draw: that.superior('draw'),
Expand Down Expand Up @@ -90,29 +99,61 @@ if (typeof navigator !== 'undefined') {
that.setMapPosition(undefined, undefined, 1);
}

function getDirection() {
var xDiff = that.movingToward[0] - that.mapPosition[0];
var yDiff = that.movingToward[1] - that.mapPosition[1];
if (yDiff <= 0) {
if (xDiff > 0) {
function getDiscreteDirection() {
if (that.direction) {
if (that.direction <= 90) {
return 'east';
} else {
} else if (that.direction > 90 && that.direction < 150) {
return 'esEast';
} else if (that.direction >= 150 && that.direction < 180) {
return 'sEast';
} else if (that.direction === 180) {
return 'south';
} else if (that.direction > 180 && that.direction <= 210) {
return 'sWest';
} else if (that.direction > 210 && that.direction < 270) {
return 'wsWest';
} else if (that.direction >= 270) {
return 'west';
} else {
return 'south';
}
} else {
var xDiff = that.movingToward[0] - that.mapPosition[0];
var yDiff = that.movingToward[1] - that.mapPosition[1];
if (yDiff <= 0) {
if (xDiff > 0) {
return 'east';
} else {
return 'west';
}
}
}

if (directions.esEast(xDiff)) {
return 'esEast';
} else if (directions.sEast(xDiff)) {
return 'sEast';
} else if (directions.wsWest(xDiff)) {
return 'wsWest';
} else if (directions.sWest(xDiff)) {
return 'sWest';
if (directions.esEast(xDiff)) {
return 'esEast';
} else if (directions.sEast(xDiff)) {
return 'sEast';
} else if (directions.wsWest(xDiff)) {
return 'wsWest';
} else if (directions.sWest(xDiff)) {
return 'sWest';
}
}
return 'south';
}

function setDiscreteDirection(d) {
if (discreteDirections[d]) {
that.setDirection(discreteDirections[d]);
}

if (d === 'west' || d === 'east') {
that.isMoving = false;
} else {
that.isMoving = true;
}
}

function getBeingEatenSprite() {
return 'blank';
}
Expand All @@ -132,6 +173,78 @@ if (typeof navigator !== 'undefined') {
}
}

that.stop = function () {
if (that.direction > 180) {
setDiscreteDirection('west');
} else {
setDiscreteDirection('east');
}
};

that.turnEast = function () {
var discreteDirection = getDiscreteDirection();

switch (discreteDirection) {
case 'west':
setDiscreteDirection('wsWest');
break;
case 'wsWest':
setDiscreteDirection('sWest');
break;
case 'sWest':
setDiscreteDirection('south');
break;
case 'south':
setDiscreteDirection('sEast');
break;
case 'sEast':
setDiscreteDirection('esEast');
break;
case 'esEast':
setDiscreteDirection('east');
break;
default:
setDiscreteDirection('south');
break;
}
};

that.turnWest = function () {
var discreteDirection = getDiscreteDirection();

switch (discreteDirection) {
case 'east':
setDiscreteDirection('esEast');
break;
case 'esEast':
setDiscreteDirection('sEast');
break;
case 'sEast':
setDiscreteDirection('south');
break;
case 'south':
setDiscreteDirection('sWest');
break;
case 'sWest':
setDiscreteDirection('wsWest');
break;
case 'wsWest':
setDiscreteDirection('west');
break;
default:
setDiscreteDirection('south');
break;
}
};

that.stepWest = function () {
that.mapPosition[0] -= that.speed * 2;
};

that.stepEast = function () {
that.mapPosition[0] += that.speed * 2;
};

that.setMapPositionTarget = function (x, y) {
if (that.hasBeenHit) return;

Expand All @@ -144,6 +257,12 @@ if (typeof navigator !== 'undefined') {
// that.resetDirection();
};

that.startMovingIfPossible = function () {
if (!that.hasBeenHit && !that.isBeingEaten) {
that.isMoving = true;
}
};

that.setTurnEaseCycles = function (c) {
turnEaseCycles = c;
};
Expand Down Expand Up @@ -187,7 +306,7 @@ if (typeof navigator !== 'undefined') {
return 'hit';
}

return getDirection();
return getDiscreteDirection();
};

return sup.draw(dContext, spritePartToUse());
Expand Down Expand Up @@ -253,14 +372,14 @@ if (typeof navigator !== 'undefined') {
}

that.getSpeedX = function () {
if (getDirection() === 'esEast' || getDirection() === 'wsWest') {
if (getDiscreteDirection() === 'esEast' || getDiscreteDirection() === 'wsWest') {
speedXFactor = 0.5;
speedX = easeSpeedToTargetUsingFactor(speedX, that.getSpeed() * speedXFactor, speedXFactor);

return speedX;
}

if (getDirection() === 'sEast' || getDirection() === 'sWest') {
if (getDiscreteDirection() === 'sEast' || getDiscreteDirection() === 'sWest') {
speedXFactor = 0.33;
speedX = easeSpeedToTargetUsingFactor(speedX, that.getSpeed() * speedXFactor, speedXFactor);

Expand All @@ -285,21 +404,21 @@ if (typeof navigator !== 'undefined') {
return speedY;
}

if (getDirection() === 'esEast' || getDirection() === 'wsWest') {
if (getDiscreteDirection() === 'esEast' || getDiscreteDirection() === 'wsWest') {
speedYFactor = 0.6;
speedY = easeSpeedToTargetUsingFactor(speedY, that.getSpeed() * 0.6, 0.6);

return speedY;
}

if (getDirection() === 'sEast' || getDirection() === 'sWest') {
if (getDiscreteDirection() === 'sEast' || getDiscreteDirection() === 'sWest') {
speedYFactor = 0.85;
speedY = easeSpeedToTargetUsingFactor(speedY, that.getSpeed() * 0.85, 0.85);

return speedY;
}

if (getDirection() === 'east' || getDirection() === 'west') {
if (getDiscreteDirection() === 'east' || getDiscreteDirection() === 'west') {
speedYFactor = 1;
speedY = 0;

Expand Down Expand Up @@ -363,10 +482,6 @@ if (typeof navigator !== 'undefined') {
return that;
}

Skier.prototype.walkLeft = function () {

};

global.skier = Skier;
})(this);

Expand Down
20 changes: 6 additions & 14 deletions js/lib/sprite.js
Expand Up @@ -5,7 +5,7 @@
var zIndexesOccupied = [ 0 ];
var that = this;
var trackedSpriteToMoveToward;
var direction;
that.direction = undefined;
that.mapPosition = [0, 0, 0];
that.id = GUID();
that.canvasX = 0;
Expand Down Expand Up @@ -64,9 +64,9 @@
var currentX = that.mapPosition[0];
var currentY = that.mapPosition[1];

if (typeof direction !== 'undefined') {
// For this we need to modify the direction so it relates to the horizontal
var d = direction - 90;
if (typeof that.direction !== 'undefined') {
// For this we need to modify the that.direction so it relates to the horizontal
var d = that.direction - 90;
if (d < 0) d = 360 + d;
currentX += roundHalf(that.speed * Math.cos(d * (Math.PI / 180)));
currentY += roundHalf(that.speed * Math.sin(d * (Math.PI / 180)));
Expand Down Expand Up @@ -277,20 +277,12 @@
if (angle >= 360) {
angle = 360 - angle;
}
direction = angle;
that.direction = angle;
that.movingToward = undefined;
};

this.startMoving = function () {
that.isMoving = true;
};

this.stopMoving = function () {
that.isMoving = false;
};

this.resetDirection = function () {
direction = undefined;
that.direction = undefined;
};

this.setMapPositionTargetWithConviction = function (cx, cy) {
Expand Down
36 changes: 22 additions & 14 deletions js/main.js
Expand Up @@ -192,36 +192,42 @@ function startNeverEndingGame (images) {
game.addUIElement(infoBox);

$(mainCanvas)
// .mousemove(function (e) {
// game.setMouseX(e.pageX);
// game.setMouseY(e.pageY);
// player.resetDirection();
// })
.mousemove(function (e) {
game.setMouseX(e.pageX);
game.setMouseY(e.pageY);
player.resetDirection();
player.startMovingIfPossible();
})
.bind('click', function (e) {
game.setMouseX(e.pageX);
game.setMouseY(e.pageY);
player.resetDirection();
player.startMovingIfPossible();
})
.focus(); // So we can listen to events immediately

Mousetrap.bind('f', player.speedBoost);
Mousetrap.bind('t', player.attemptTrick);
Mousetrap.bind(['w', 'up'], function () {
game.setMouseX(0);
game.setMouseY(0);
player.stopMoving();
player.stop();
});
Mousetrap.bind(['a', 'left'], function () {
player.setDirection(225);
player.startMoving();
if (player.direction === 270) {
player.stepWest();
} else {
player.turnWest();
}
});
Mousetrap.bind(['s', 'down'], function () {
player.setDirection(180);
player.startMoving();
player.startMovingIfPossible();
});
Mousetrap.bind(['d', 'right'], function () {
player.setDirection(135);
player.startMoving();
if (player.direction === 90) {
player.stepEast();
} else {
player.turnEast();
}
});
Mousetrap.bind('m', spawnMonster);
Mousetrap.bind('b', spawnBoarder);
Expand All @@ -240,7 +246,9 @@ function startNeverEndingGame (images) {
player.speedBoost();
});

player.stopMoving();
player.isMoving = false;
player.setDirection(270);

game.start();
}

Expand Down

0 comments on commit a7d0975

Please sign in to comment.