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
+50 -28
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,35 +257,30 @@ 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]];
switch(event.which) { World.curPos[0] += direction[0];
case 38: // Up World.curPos[1] += direction[1];
case 87:
Engine.log('up');
if(World.curPos[1] > 0) World.curPos[1]--;
break;
case 40: // Down
case 83:
Engine.log('down');
if(World.curPos[1] < World.RADIUS * 2) World.curPos[1]++;
break;
case 37: // Left
case 65:
Engine.log('left');
if(World.curPos[0] > 0) World.curPos[0]--;
break;
case 39: // Right
case 68:
Engine.log('right');
if(World.curPos[0] < World.RADIUS * 2) World.curPos[0]++;
break;
default:
moved = false;
break;
}
if(moved) {
World.narrateMove(oldTile, World.state.map[World.curPos[0]][World.curPos[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.lightMap(World.curPos[0], World.curPos[1], World.state.mask);
World.drawMap(); World.drawMap();
@@ -293,6 +292,29 @@ var World = {
Notifications.notify(World, 'safer here'); Notifications.notify(World, 'safer here');
} }
} }
},
keyDown: function(event) {
switch(event.which) {
case 38: // Up
case 87:
World.moveNorth();
break;
case 40: // Down
case 83:
World.moveSouth();
break;
case 37: // Left
case 65:
World.moveWest();
break;
case 39: // Right
case 68:
World.moveEast();
break;
default:
break;
}
} }
}, },