X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=mud.js;h=45373cc7a7085e1bc85109bec9f64a7717230cc6;hb=ac86d15fc911ea899a123b1fc3027bf12aa6809e;hp=1017af9a9096a1cefa55704cde98e805b6f7840a;hpb=93ee0b987b1558436cb0333640d32c87299097ca;p=mudd.git diff --git a/mud.js b/mud.js index 1017af9..45373cc 100644 --- a/mud.js +++ b/mud.js @@ -1,11 +1,15 @@ $(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) { + function player(x, y, id) { this.x = x; this.y = y; + this.id = id; + + mud.rooms[this.x][this.y].join(); this.move = function(direction) { var rooms = mud.rooms; @@ -14,9 +18,15 @@ $(document).ready(function(){ 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(); - rooms[this.x-1][this.y].join(); - this.x--; +// $.get(endpoint, { +// 'cmd' : 'move', +// 'name' : 'majuscule', +// 'direction' : 'west' +// }, function() { + rooms[this.x][this.y].clear(); + rooms[this.x-1][this.y].join(); + this.x--; +// }); } break; case 'up': @@ -30,7 +40,7 @@ $(document).ready(function(){ break; case 'right': case 'east': - if (rooms[this.x+1] && rooms[this.x+1][this.y+1] + if (rooms[this.x+1] && rooms[this.x+1][this.y] && !rooms[this.x+1][this.y].state) { rooms[this.x][this.y].clear(); rooms[this.x+1][this.y].join(); @@ -50,81 +60,140 @@ $(document).ready(function(){ } } - function room(x, y, h, l) { + function room(x, y, width, height) { this.x = x; this.y = y; - this.h = h; - this.l = l; + this.width = width; + this.height = height; this.state = 0; this.fill = function() { c.fillStyle = "rgb(0,0,0)"; - c.fillRect(this.x, this.y, this.h, this.l); + c.fillRect(this.x, this.y, this.height, this.width); this.state = 1; } this.clear = function() { c.fillStyle = "rgb(255,255,255)"; - c.fillRect(this.x, this.y, this.h, this.l); + c.fillRect(this.x, this.y, this.height, this.width); this.state = 0; } this.join = function() { c.fillStyle = "rgb(0,255,0)"; - c.fillRect(this.x, this.y, this.h, this.l); + c.fillRect(this.x, this.y, this.height, this.width); } } function universe() { - this.rooms = []; - this.rows = 30; - this.columns = 30; - - this.roomWidth = canvas.width / this.rows; - this.roomHeight = canvas.height / this.columns; + var self = this; + this.rooms = []; - for (var i = 0; i < this.columns; i++) { - var column = []; - var x = i * this.roomWidth; - var y = 0; - 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(); - } - this.rooms.push(column); - } + this.build = function(seed) { + this.rows = seed.length; + this.columns = seed[0].length; + this.roomWidth = canvas.width / this.rows; + this.roomHeight = canvas.height / this.columns; - this.populate = function(seed) { - for (var i = 0; i < this.rows; i++) { - for (var ii = 0; ii < this.columns; ii++) { - seed[i][ii] ? this.rooms[i][ii].clear() - : this.rooms[i][ii].fill(); + for (var i = 0; i < this.columns; i++) { + this.rooms[i] = []; + var x = i * this.roomWidth; + var y = 0; + for (var ii = 0; ii < this.rows; ii++) { + this.rooms[i][ii] = new room(x, y, this.roomWidth, this.roomHeight); + if (seed[i][ii]) this.rooms[i][ii].fill(); + y += this.roomHeight; } } } this.join = function() { - do { - var x = Math.floor(Math.random()*(this.columns)); - var y = Math.floor(Math.random()*(this.rows)); - } while (!this.rooms[x][y].state); - this.rooms[x][y].join(); - return player = new player(x,y); + $.getJSON(endpoint, { 'cmd' : 'join' }, function(json) { + self.player = new player(json.x, json.y, json.id); + setInterval(self.poll, 1000); + }); } + var commands = { + tell : function(msg) { + console.log(msg); + var parts = msg.match(/^(\w+)\s(.*)/); + if (!parts[1]) return; + 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) { + $.ajax({ + url: endpoint, + data: { 'cmd' : 'yell', 'msg' : msg }, + success: function() { + writeToLog('You yelled: ', 'yell', msg); + }, + }); + }, + say : function(msg) { +// $.ajax({ +// url: endpoint, +// data: { 'cmd' : 'yell', 'msg' : msg }, +// success: function() { + writeToLog('You said: ', 'say', msg); +// }, +// }); + }, + move : function(direction) { + this.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(.*)/); + if (parts) { + var cmd = parts[1]; + text = parts[2]; + if (commands[cmd] != undefined) { + commands[cmd](text); + $('#chat').val(''); + } + } + }); + $('#chat').keydown(function(e) { + if (e.which == '13') { + $('#submit').click(); + } else if (e.which >= 37 && e.which <= 40) + e.stopPropagation(); + }); + $('#join').click(function() { + mud.join(); + $(this).fadeOut('slow'); + }); + $(document).keydown(function(e) { - if (typeof player == undefined) return; + if (typeof self.player == 'undefined') return; switch (e.which) { case 37: - player.move('left'); + self.player.move('left'); break; case 38: - player.move('up'); + self.player.move('up'); break; case 39: - player.move('right'); + self.player.move('right'); break; case 40: - player.move('down'); + self.player.move('down'); break; default: return; @@ -132,30 +201,24 @@ $(document).ready(function(){ e.preventDefault(); }); - var commands = { - tell : function() {}, - yell : function() {}, - move : function(direction) { - player.move(direction); - }, - } - $('#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').keypress(function(e) { - if (e.which == '13') { - $('#submit').click(); - $('#chat').val(''); - } + $.getJSON(endpoint, { 'cmd' : 'start' }, function(json) { + self.build(json) + }) + .fail(function(jqxhr, textStatus, error) { + console.log(jqxhr, jqxhr.responseText, textStatus, error); }); + this.poll = function() { + $.getJSON(endpoint, { 'cmd' : 'poll' }, function(messages) { + for (var i in messages) { + var msg = messages[i]; + if (msg.id == self.player.id) continue; + writeToLog(msg.name + ':', msg.type, msg.message); + } + }); + } } - var mud = new universe; - var player = mud.join(); + mud = new universe; });