From ba99c329aa55549cefeeec517fbda7480c304afb Mon Sep 17 00:00:00 2001 From: Derek Smith Date: Fri, 17 Aug 2012 16:32:59 -0700 Subject: [PATCH 1/7] Changed the way movement is handled Movement is handled by polling to avoid the default delay that the browser/OS' use before repeating held-down keys. --- assets/scripts/engine.js | 47 ++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/assets/scripts/engine.js b/assets/scripts/engine.js index 78c637e..ad2a826 100644 --- a/assets/scripts/engine.js +++ b/assets/scripts/engine.js @@ -56,28 +56,47 @@ window.app = { }, initializeKeybindings: function() { - $(document).keypress(function(e) { - if ($(e.target).is(":input")) { - return; - } - - if (e.which == 119) { // w + + var keysPressed = {}; + + $(document).keydown(function(e) { + keysPressed[e.which] = true; + }); + + $(document).keyup(function(e) { + keysPressed[e.which] = false; + }); + + var checkKeys = function() { + if (keysPressed['119']) { // w app.player.move('n'); - } else if (e.which == 97) { // a + } else if (keysPressed['97']) { // a app.player.move('w'); - } else if (e.which == 115) { // s + } else if (keysPressed['115']) { // s app.player.move('s'); - } else if (e.which == 100) { // d + } else if (keysPressed['100']) { // d app.player.move('e'); - } else if (e.which == 87) { // W + } else if (keysPressed['87']) { // W app.player.setDirection('n'); - } else if (e.which == 65) { // A + } else if (keysPressed['65']) { // A app.player.setDirection('w'); - } else if (e.which == 83) { // S + } else if (keysPressed['83']) { // S app.player.setDirection('s'); - } else if (e.which == 68) { // D + } else if (keysPressed['68']) { // D app.player.setDirection('e'); - } else if (e.which == 116) { // T + } + + setTimeout(checkKeys, 200); + }; + + checkKeys(); + + $(document).keypress(function(e) { + if ($(e.target).is(":input")) { + return; + } + + if (e.which == 116) { // T e.preventDefault(); // keeps us from getting a t in the box $('#message-input').focus(); } else if (e.which == 47) { // / From 3316bf31301112f0bc7b984bf38abcf612e5c019 Mon Sep 17 00:00:00 2001 From: Derek Date: Fri, 17 Aug 2012 18:01:12 -0700 Subject: [PATCH 2/7] Updated the movement code after testing it. --- assets/scripts/engine.js | 59 ++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/assets/scripts/engine.js b/assets/scripts/engine.js index ad2a826..b48b89f 100644 --- a/assets/scripts/engine.js +++ b/assets/scripts/engine.js @@ -55,38 +55,55 @@ window.app = { $('#controls .button').tipsy({fade: false, gravity: 's', html: true}); }, + keysPressed: {}, + initializeKeybindings: function() { - var keysPressed = {}; - $(document).keydown(function(e) { - keysPressed[e.which] = true; + if ($(e.target).is(":input")) { + return; + } + + app.keysPressed[e.which] = true; }); $(document).keyup(function(e) { - keysPressed[e.which] = false; + if ($(e.target).is(":input")) { + return; + } + + app.keysPressed[e.which] = false; }); var checkKeys = function() { - if (keysPressed['119']) { // w - app.player.move('n'); - } else if (keysPressed['97']) { // a - app.player.move('w'); - } else if (keysPressed['115']) { // s - app.player.move('s'); - } else if (keysPressed['100']) { // d - app.player.move('e'); - } else if (keysPressed['87']) { // W - app.player.setDirection('n'); - } else if (keysPressed['65']) { // A - app.player.setDirection('w'); - } else if (keysPressed['83']) { // S - app.player.setDirection('s'); - } else if (keysPressed['68']) { // D - app.player.setDirection('e'); + + if (app.keysPressed['87']) { // w + if (app.keysPressed['16']) { // shift + app.player.setDirection('n'); + } else { + app.player.move('n'); + } + } else if (app.keysPressed['65']) { // a + if (app.keysPressed['16']) { // shift + app.player.setDirection('w'); + } else { + app.player.move('w'); + } + } else if (app.keysPressed['83']) { // s + if (app.keysPressed['16']) { // shift + app.player.setDirection('s'); + } else { + app.player.move('s'); + } + } else if (app.keysPressed['68']) { // d + if (app.keysPressed['16']) { // shift + app.player.setDirection('e'); + } else { + app.player.move('e'); + } } - setTimeout(checkKeys, 200); + setTimeout(checkKeys, 100); }; checkKeys(); From 78327d5d769b670eaf42226966ad3f6fa075bfb2 Mon Sep 17 00:00:00 2001 From: Derek Date: Fri, 17 Aug 2012 18:07:14 -0700 Subject: [PATCH 3/7] Moved keysPressed inside initializeKeybindings --- assets/scripts/engine.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/assets/scripts/engine.js b/assets/scripts/engine.js index b48b89f..fe8aea7 100644 --- a/assets/scripts/engine.js +++ b/assets/scripts/engine.js @@ -55,16 +55,16 @@ window.app = { $('#controls .button').tipsy({fade: false, gravity: 's', html: true}); }, - keysPressed: {}, - initializeKeybindings: function() { + + var keysPressed = {}; $(document).keydown(function(e) { if ($(e.target).is(":input")) { return; } - app.keysPressed[e.which] = true; + keysPressed[e.which] = true; }); $(document).keyup(function(e) { @@ -72,31 +72,31 @@ window.app = { return; } - app.keysPressed[e.which] = false; + keysPressed[e.which] = false; }); var checkKeys = function() { - if (app.keysPressed['87']) { // w - if (app.keysPressed['16']) { // shift + if (keysPressed['87']) { // w + if (keysPressed['16']) { // shift app.player.setDirection('n'); } else { app.player.move('n'); } - } else if (app.keysPressed['65']) { // a - if (app.keysPressed['16']) { // shift + } else if (keysPressed['65']) { // a + if (keysPressed['16']) { // shift app.player.setDirection('w'); } else { app.player.move('w'); } - } else if (app.keysPressed['83']) { // s - if (app.keysPressed['16']) { // shift + } else if (keysPressed['83']) { // s + if (keysPressed['16']) { // shift app.player.setDirection('s'); } else { app.player.move('s'); } - } else if (app.keysPressed['68']) { // d - if (app.keysPressed['16']) { // shift + } else if (keysPressed['68']) { // d + if (keysPressed['16']) { // shift app.player.setDirection('e'); } else { app.player.move('e'); From 3a8e14c5f3e424f41c68dd86b590649625f08e41 Mon Sep 17 00:00:00 2001 From: Derek Date: Fri, 17 Aug 2012 22:28:26 -0700 Subject: [PATCH 4/7] Added another way to place items and dig --- assets/scripts/engine.js | 56 +++++++++++++++++++++++++++++++++++++++- assets/styles/main.css | 5 ++-- server.js | 3 +-- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/assets/scripts/engine.js b/assets/scripts/engine.js index fe8aea7..8417ceb 100644 --- a/assets/scripts/engine.js +++ b/assets/scripts/engine.js @@ -44,15 +44,58 @@ window.app = { app.initializeKeybindings(); app.persistence.startAutoSave(); app.graphics.startAnimation(); + app.controls.initialize(); app.chat.message('Help', 'Type /help for some help', 'help'); app.chat.message('Help', 'Use the WASD keys to move around', 'help'); - + setTimeout(function() { app.network.send.move(app.player.coordinates, app.player.direction); app.network.send.character(app.player.name, app.player.picture); }, 500); $('#controls .button').tipsy({fade: false, gravity: 's', html: true}); + + app.player.inventory.data = [1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]; + }, + + controls: { + selected: 0, + initialize: function() { + app.controls.set(0); + }, + data: [ + $('#controls .wood-wall'), + $('#controls .wood-floor'), + $('#controls .stone-wall'), + $('#controls .stone-floor'), + $('#controls .door'), + $('#controls .glass'), + $('#controls .collect') + ], + set: function(index) { + if (index < 0) { + index = app.controls.data.length - 1; + } else if (index >= app.controls.data.length) { + index = 0; + } + + app.controls.data[app.controls.selected].css('border-color', '#333'); + app.controls.selected = index; + app.controls.data[app.controls.selected].css('border-color', '#0F0'); + }, + next: function() { + app.controls.set(app.controls.selected + 1) + }, + previous: function() { + app.controls.set(app.controls.selected - 1) + }, + action: function() { + if (app.controls.selected == 6) { // collect + app.player.mineFacingTile(); + } else { + app.player.placeItem(app.controls.selected + 9); + } + } }, initializeKeybindings: function() { @@ -103,6 +146,13 @@ window.app = { } } + if (keysPressed['37']) { // left arrow + app.controls.previous(); + } + else if (keysPressed['39']) { // right arrow + app.controls.next(); + } + setTimeout(checkKeys, 100); }; @@ -113,6 +163,10 @@ window.app = { return; } + if (e.which == 32) { // space + app.controls.action(); + } + if (e.which == 116) { // T e.preventDefault(); // keeps us from getting a t in the box $('#message-input').focus(); diff --git a/assets/styles/main.css b/assets/styles/main.css index 68f4924..3a3261e 100644 --- a/assets/styles/main.css +++ b/assets/styles/main.css @@ -137,12 +137,12 @@ body { } #controls .holder { margin: 0 auto; - width: 264px; + width: 290px; background: rgba(0, 0, 0, 0.75); border-top-left-radius: 6px; border-top-right-radius: 6px; height: 32px; - padding-top: 8px; + padding: 8px 0 10px 0; } #controls .holder .button { display: inline-block; @@ -150,6 +150,7 @@ body { height: 32px; text-align: left; text-shadow: 0 2px 2px #000; + border: 2px solid #333; } #controls .wood-wall { background-image: url("/assets/tilesets/terrain-32x32.png"); diff --git a/server.js b/server.js index f6560b7..c5a7099 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,5 @@ // Use clean code 'use strict'; - // requires var app = require('express').createServer(); var io = require('socket.io').listen(app, { log: false}); @@ -14,7 +13,7 @@ var colors = require('colors'); // Web Server Configuration var server_port = 80; // most OS's will require sudo to listen on 80 -var server_address = '127.0.0.1'; +var server_address = '192.168.0.191'; //'127.0.0.1'; // MongoDB Configuration var mongo_host = '127.0.0.1'; From cbe1c32f357b681947feec29513d573b56cfd318 Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 18 Aug 2012 01:57:15 -0700 Subject: [PATCH 5/7] 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 :) --- assets/scripts/engine.js | 28 ++++++++++++++++++-- assets/styles/main.css | 16 ++++++++++++ assets/tilesets/heart-32x32.png | Bin 0 -> 1589 bytes index.html | 4 +++ server.js | 45 +++++++++++++++++++++++++++++++- 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 assets/tilesets/heart-32x32.png diff --git a/assets/scripts/engine.js b/assets/scripts/engine.js index 8417ceb..2a276cd 100644 --- a/assets/scripts/engine.js +++ b/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('
'); + } } }, diff --git a/assets/styles/main.css b/assets/styles/main.css index 3a3261e..ba65841 100644 --- a/assets/styles/main.css +++ b/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%; diff --git a/assets/tilesets/heart-32x32.png b/assets/tilesets/heart-32x32.png new file mode 100644 index 0000000000000000000000000000000000000000..fa7092c581ae38fb6adf0faf4de05084967ef1b0 GIT binary patch literal 1589 zcmV-52Fm$~P)8Zup z+yroqqp87fT)MP+Y4Z*!w_3;d7P1+LdpzKGT!eHob?yo0&0V;XFLi+X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)n8200000NkvXXu0mjfFo^q| literal 0 HcmV?d00001 diff --git a/index.html b/index.html index 84f1d1c..6291cbb 100644 --- a/index.html +++ b/index.html @@ -38,6 +38,10 @@
Inventory
Cobalt: 0
+
+
+
+
1
diff --git a/server.js b/server.js index c5a7099..5dda9c5 100644 --- a/server.js +++ b/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: [], From 0f0e0d35c0ea37ae6698f67d8e7a923930120b2b Mon Sep 17 00:00:00 2001 From: Derek Smith Date: Sun, 19 Aug 2012 19:45:04 -0700 Subject: [PATCH 6/7] Changed node's server address back to 127.0.0.1 --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 5dda9c5..c0c2925 100644 --- a/server.js +++ b/server.js @@ -13,7 +13,7 @@ var colors = require('colors'); // Web Server Configuration var server_port = 80; // most OS's will require sudo to listen on 80 -var server_address = '192.168.0.191'; //'127.0.0.1'; +var server_address = '127.0.0.1'; // MongoDB Configuration var mongo_host = '127.0.0.1'; From 6584a5629aadf3b7b773562febbcadcc205a2ef1 Mon Sep 17 00:00:00 2001 From: Derek Smith Date: Sun, 19 Aug 2012 19:58:48 -0700 Subject: [PATCH 7/7] Re-factored the heart drawing code Re-factored the heart drawing code to cache the jQuery object. --- assets/scripts/engine.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/assets/scripts/engine.js b/assets/scripts/engine.js index 2a276cd..be666e1 100644 --- a/assets/scripts/engine.js +++ b/assets/scripts/engine.js @@ -563,7 +563,7 @@ window.app = { setTimeout(function() { app.player.hearts = 5; - app.graphics.drawHearts(); + app.graphics.hearts.draw(); }, 200); }, @@ -590,7 +590,7 @@ window.app = { app.player.kill(killMessage); return; } - app.graphics.drawHearts(); + app.graphics.hearts.draw(); } }, @@ -990,10 +990,14 @@ window.app = { return index; }, - drawHearts: function() { - $('#hearts .holder').empty(); - for (var i = 0; i < app.player.hearts; i++) { - $('#hearts .holder').append('
'); + hearts: { + $holder = $('#hearts .holder'), + + draw: function() { + app.graphics.hearts.$holder.empty(); + for (var i = 0; i < app.player.hearts; i++) { + app.graphics.hearts.$holder.append('
'); + } } } },