world.js: Refactor move commands out of keyDown

In order to better handle move commands, refactor them out of the keyDown
event handler. This will enable using other input methods to trigger a move
command, such as mouse clicks or touch events.
This commit is contained in:
Andrew Ardill
2013-07-04 14:49:21 +10:00
parent 9327358008
commit d0a171e95b
+45 -23
View File
@@ -33,6 +33,10 @@ var World = {
BASE_HIT_CHANCE: 0.8,
MEAT_HEAL: 10,
FIGHT_DELAY: 3, // At least three moves between fights
NORTH: [ 0, -1],
SOUTH: [ 0, 1],
WEST: [-1, 0],
EAST: [ 1, 0],
Weapons: {
'fists': {
@@ -253,46 +257,64 @@ var World = {
return div;
},
keyDown: function(event) {
var moved = true;
moveNorth: function() {
Engine.log('North');
if(World.curPos[1] > 0) World.move(World.NORTH);
},
moveSouth: function() {
Engine.log('South');
if(World.curPos[1] < World.RADIUS * 2) World.move(World.SOUTH);
},
moveWest: function() {
Engine.log('West');
if(World.curPos[0] > 0) World.move(World.WEST);
},
moveEast: function() {
Engine.log('East');
if(World.curPos[0] < World.RADIUS * 2) World.move(World.EAST);
},
move: function(direction) {
var oldTile = World.state.map[World.curPos[0]][World.curPos[1]];
World.curPos[0] += direction[0];
World.curPos[1] += direction[1];
World.narrateMove(oldTile, World.state.map[World.curPos[0]][World.curPos[1]]);
World.lightMap(World.curPos[0], World.curPos[1], World.state.mask);
World.drawMap();
World.doSpace();
if(World.checkDanger()) {
if(World.danger) {
Notifications.notify(World, 'dangerous to be this far from the village without proper protection')
} else {
Notifications.notify(World, 'safer here');
}
}
},
keyDown: function(event) {
switch(event.which) {
case 38: // Up
case 87:
Engine.log('up');
if(World.curPos[1] > 0) World.curPos[1]--;
World.moveNorth();
break;
case 40: // Down
case 83:
Engine.log('down');
if(World.curPos[1] < World.RADIUS * 2) World.curPos[1]++;
World.moveSouth();
break;
case 37: // Left
case 65:
Engine.log('left');
if(World.curPos[0] > 0) World.curPos[0]--;
World.moveWest();
break;
case 39: // Right
case 68:
Engine.log('right');
if(World.curPos[0] < World.RADIUS * 2) World.curPos[0]++;
World.moveEast();
break;
default:
moved = false;
break;
}
if(moved) {
World.narrateMove(oldTile, World.state.map[World.curPos[0]][World.curPos[1]]);
World.lightMap(World.curPos[0], World.curPos[1], World.state.mask);
World.drawMap();
World.doSpace();
if(World.checkDanger()) {
if(World.danger) {
Notifications.notify(World, 'dangerous to be this far from the village without proper protection')
} else {
Notifications.notify(World, 'safer here');
}
}
}
},