Skip to content
Browse files

Added health in the form of hearts. Now you must lose all your hearts…

… to die. Added basic AI to enemies - they chase players if within a 10 tile radius. Lastly, changed the enemy movement interval to 1sec from 4secs to make things more interesting :)
  • Loading branch information...
1 parent 3a8e14c commit cbe1c32f357b681947feec29513d573b56cfd318 Derek committed Aug 18, 2012
Showing with 90 additions and 3 deletions.
  1. +26 −2 assets/scripts/engine.js
  2. +16 −0 assets/styles/main.css
  3. BIN assets/tilesets/heart-32x32.png
  4. +4 −0 index.html
  5. +44 −1 server.js
View
28 assets/scripts/engine.js
@@ -45,6 +45,7 @@ window.app = {
app.persistence.startAutoSave();
app.graphics.startAnimation();
app.controls.initialize();
+ app.graphics.drawHearts();
app.chat.message('Help', 'Type /help for some help', 'help');
app.chat.message('Help', 'Use the WASD keys to move around', 'help');
@@ -346,6 +347,7 @@ window.app = {
},
player: {
+ hearts: 5,
picture: 0,
name: '',
god: false,
@@ -401,7 +403,7 @@ window.app = {
if (app.environment.corruption.loaded && app.environment.corruption.data[coords.x][coords.y]) {
if (Math.random() < 1/8) {
- app.player.kill("You were killed by corruption");
+ app.player.hurt("You were killed by corruption");
app.network.send.chat("*Killed by Corruption*");
}
}
@@ -558,6 +560,11 @@ window.app = {
app.graphics.viewport.update();
app.chat.message('Client', message, 'client');
app.persistence.save();
+
+ setTimeout(function() {
+ app.player.hearts = 5;
+ app.graphics.drawHearts();
+ }, 200);
},
// Checks to see if an NPC is adjacent to the player, and if so, kills them
@@ -569,13 +576,22 @@ window.app = {
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
if (npc.x == coords.x+i && npc.y == coords.y+j) {
- app.player.kill("Killed by " + app.graphics.tilesets.descriptors.monsters[npc.id].name);
+ app.player.hurt("Killed by " + app.graphics.tilesets.descriptors.monsters[npc.id].name);
break;
}
}
}
}
},
+
+ hurt: function(killMessage) {
+ app.player.hearts--;
+ if (app.player.hearts <= 0) {
+ app.player.kill(killMessage);
+ return;
+ }
+ app.graphics.drawHearts();
+ }
},
// NPC stuff
@@ -671,6 +687,7 @@ window.app = {
var player = app.players.data[i];
if (player.session == session) {
app.players.data.splice(i, 1);
+ break;
}
}
}
@@ -971,6 +988,13 @@ window.app = {
}
return index;
+ },
+
+ drawHearts: function() {
+ $('#hearts .holder').empty();
+ for (var i = 0; i < app.player.hearts; i++) {
+ $('#hearts .holder').append('<div class="heart"></div>');
+ }
}
},
View
16 assets/styles/main.css
@@ -130,6 +130,22 @@ body {
#nametags .name span.monster {
color: #e87b06;
}
+#hearts {
+ position: absolute;
+ width: 100%;
+ bottom: 55px;
+}
+#hearts .holder {
+ margin: 0 auto;
+ width: 290;
+ border-radius: 6px;
+}
+#hearts .holder .heart {
+ display: inline-block;
+ width: 32px;
+ height: 32px;
+ background-image: url("/assets/tilesets/heart-32x32.png");
+}
#controls {
position: absolute;
width: 100%;
View
BIN assets/tilesets/heart-32x32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 index.html
@@ -38,6 +38,10 @@
<div class="inventory-display" id="inventory-8" title="Cobalt Ore">Cobalt: <span>0</span></div>
</div>
</div>
+ <div id="hearts">
+ <div class="holder">
+ </div>
+ </div>
<div id="controls">
<div class="holder">
<div class="button wood-wall" title="Press 1 to place a wooden wall<br /><strong>Cost: 4 x Wood</strong>"><span>1</span></div>
View
45 server.js
@@ -242,12 +242,18 @@ var game = {
npcmovement: {
handle: null,
- interval: 4 * 1000,
+ interval: 2 * 1000,
payload: function() {
var len = game.npcs.length;
for(var i = 0; i < len; i++) {
var npc = game.npcs[i];
+
+ if (game.tryNPCChase(npc)) {
+ // success -> heading towards a player
+ continue;
+ }
+
var new_direction = Math.floor(Math.random() * 5);
if (new_direction == 0 && npc.x < 199 && game.canNPCWalk(npc.x+1, npc.y)) {
npc.x++;
@@ -299,6 +305,43 @@ var game = {
return true;
},
+ tryNPCChase: function(npc) {
+ var radius = 10;
+ var deltaX = 0;
+ var deltaY = 0;
+ var moved = false;
+ for (var i = 0; i < game.players.length; i++) {
+ deltaX = game.players[i].x - npc.x;
+ deltaY = game.players[i].y - npc.y;
+ moved = false;
+
+ if (deltaX >= 0 && deltaX <= radius && npc.x < 199 && game.canNPCWalk(npc.x+1, npc.y)) {
+ npc.x++;
+ npc.d = 'e';
+ moved = true;
+ } else if (deltaX <= 0 && deltaX >= -radius && npc.x > 0 && game.canNPCWalk(npc.x-1, npc.y)) {
+ npc.x--;
+ npc.d = 'w';
+ moved = true;
+ }
+
+ if (deltaY >= 0 && deltaY <= radius && npc.y < 199 && game.canNPCWalk(npc.x, npc.y+1)) {
+ npc.y++;
+ npc.d = 's';
+ moved = true;
+ } else if (deltaY <= 0 && deltaY >= -radius && npc.y > 0 && game.canNPCWalk(npc.x, npc.y-1)) {
+ npc.y--;
+ npc.d = 'n';
+ moved = true;
+ }
+
+ if (moved) {
+ return true;
+ }
+ }
+ return false;
+ },
+
// Array of known player locations
players: [],

0 comments on commit cbe1c32

Please sign in to comment.
Something went wrong with that request. Please try again.