Merge pull request #7 from Cogito/moveInterface

Improve movement interface to allow click events
This commit is contained in:
Michael Townsend
2013-07-04 06:55:50 -07:00
+67 -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,84 @@ 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); click: function(event) {
World.drawMap(); var map = $('#map'),
World.doSpace(); // measure clicks relative to the centre of the current location
if(World.checkDanger()) { centreX = map.offset().left + map.width() * World.curPos[0] / (World.RADIUS * 2),
if(World.danger) { centreY = map.offset().top + map.height() * World.curPos[1] / (World.RADIUS * 2),
Notifications.notify(World, 'dangerous to be this far from the village without proper protection') clickX = event.pageX - centreX,
} else { clickY = event.pageY - centreY;
Notifications.notify(World, 'safer here'); if (clickX > clickY && clickX < -clickY) {
} World.moveNorth();
} }
if (clickX < clickY && clickX > -clickY) {
World.moveSouth();
}
if (clickX < clickY && clickX < -clickY) {
World.moveWest();
}
if (clickX > clickY && clickX > -clickY) {
World.moveEast();
} }
}, },
@@ -641,6 +683,8 @@ var World = {
var map = $('#map'); var map = $('#map');
if(map.length == 0) { if(map.length == 0) {
map = new $('<div>').attr('id', 'map').appendTo('#worldOuter'); map = new $('<div>').attr('id', 'map').appendTo('#worldOuter');
// register click handler
map.click(World.click);
} }
var mapString = ""; var mapString = "";
for(var j = 0; j <= World.RADIUS * 2; j++) { for(var j = 0; j <= World.RADIUS * 2; j++) {