X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=mud.js;h=1f55d01b352cecbf899a8def326e6f0531337cce;hb=9b66980c9363a6e8b5b1bdf7487f0adec84b1eec;hp=eb1b55c5c7f2db5d4b533d399342fb364248cbc6;hpb=620baa26c102f5a97830593c55d67ab27dbe596a;p=mudd.git diff --git a/mud.js b/mud.js index eb1b55c..1f55d01 100644 --- a/mud.js +++ b/mud.js @@ -1,7 +1,8 @@ $(document).ready(function(){ - var canvas = $('#world')[0]; + var canvas = $('#universe')[0]; var c = canvas.getContext('2d'); + var endpoint = '/mud/mud.php'; function player(x, y) { this.x = x; @@ -11,6 +12,7 @@ $(document).ready(function(){ var rooms = mud.rooms; switch (direction) { case 'left': + case 'west': if (rooms[this.x-1] && rooms[this.x-1][this.y] && !rooms[this.x-1][this.y].state) { rooms[this.x][this.y].clear(); @@ -19,6 +21,7 @@ $(document).ready(function(){ } break; case 'up': + case 'north': if (rooms[this.x][this.y-1] && !rooms[this.x][this.y-1].state) { rooms[this.x][this.y].clear(); @@ -27,6 +30,7 @@ $(document).ready(function(){ } break; case 'right': + case 'east': if (rooms[this.x+1] && rooms[this.x+1][this.y+1] && !rooms[this.x+1][this.y].state) { rooms[this.x][this.y].clear(); @@ -35,6 +39,7 @@ $(document).ready(function(){ } break; case 'down': + case 'south': if (rooms[this.x][this.y+1] && !rooms[this.x][this.y+1].state) { rooms[this.x][this.y].clear(); @@ -58,7 +63,6 @@ $(document).ready(function(){ this.state = 1; } this.clear = function() { - console.log('clearing'); c.fillStyle = "rgb(255,255,255)"; c.fillRect(this.x, this.y, this.h, this.l); this.state = 0; @@ -70,6 +74,7 @@ $(document).ready(function(){ } function universe() { + this.rooms = []; this.rows = 30; @@ -85,7 +90,7 @@ $(document).ready(function(){ for (var ii = 0; ii < this.columns; ii++) { column.push(new room(x, y, this.roomWidth, this.roomHeight)); y += this.roomWidth; - Math.random() > .5 ? column[ii].clear() : column[ii].fill(); + Math.random() > .2 ? column[ii].clear() : column[ii].fill(); } this.rooms.push(column); } @@ -128,6 +133,58 @@ $(document).ready(function(){ } e.preventDefault(); }); + + var commands = { + tell : function(msg) { + var parts = msg.match(/^(\w+)\s(.*)/); + var dest = parts[1]; + msg = parts[2]; + $.ajax({ + url: endpoint, + data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg }, + success: function() { + writeToLog('You told ' + dest + ': ', 'tell', msg); + }, + }); + }, + yell : function(msg) { + console.log('yell!'); + $.ajax({ + url: endpoint, + data: { 'cmd' : 'yell', 'msg' : msg }, + success: function() { + writeToLog('You yelled: ', 'yell', msg); + }, + }); + }, + move : function(direction) { + player.move(direction); + }, + } + function writeToLog(action, style, msg) { + $('#log').append( + $('
').addClass('logline').append( + $('').addClass(style).text(action), + $('').addClass('msg').text(msg) + ) + ) + } + $('#submit').click(function() { + var text = $('#chat').val(); + var parts = text.match(/^(\w+)\s(.*)/); + var cmd = parts[1]; + text = parts[2]; + if (commands[cmd] != undefined) { + commands[cmd](text); + $('#chat').val(''); + } + }); + $('#chat').keypress(function(e) { + if (e.which == '13') { + $('#submit').click(); + } + }); + } var mud = new universe;