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, BASE_HIT_CHANCE: 0.8,
MEAT_HEAL: 10, MEAT_HEAL: 10,
FIGHT_DELAY: 3, // At least three moves between fights FIGHT_DELAY: 3, // At least three moves between fights
NORTH: [ 0, -1],
SOUTH: [ 0, 1],
WEST: [-1, 0],
EAST: [ 1, 0],
Weapons: { Weapons: {
'fists': { 'fists': {
@@ -253,46 +257,64 @@ var World = {
return div; return div;
}, },
keyDown: function(event) { moveNorth: function() {
var moved = true; 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]]; 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) { switch(event.which) {
case 38: // Up case 38: // Up
case 87: case 87:
Engine.log('up'); World.moveNorth();
if(World.curPos[1] > 0) World.curPos[1]--;
break; break;
case 40: // Down case 40: // Down
case 83: case 83:
Engine.log('down'); World.moveSouth();
if(World.curPos[1] < World.RADIUS * 2) World.curPos[1]++;
break; break;
case 37: // Left case 37: // Left
case 65: case 65:
Engine.log('left'); World.moveWest();
if(World.curPos[0] > 0) World.curPos[0]--;
break; break;
case 39: // Right case 39: // Right
case 68: case 68:
Engine.log('right'); World.moveEast();
if(World.curPos[0] < World.RADIUS * 2) World.curPos[0]++;
break; break;
default: default:
moved = false;
break; 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');
}
}
} }
}, },