started client chat fns
[mudd.git] / mud.js
1 $(document).ready(function(){
2
3 var canvas = $('#universe')[0];
4 var c = canvas.getContext('2d');
5 var endpoint = '/mud/mud.php';
6
7 function player(x, y) {
8 this.x = x;
9 this.y = y;
10
11 this.move = function(direction) {
12 var rooms = mud.rooms;
13 switch (direction) {
14 case 'left':
15 case 'west':
16 if (rooms[this.x-1] && rooms[this.x-1][this.y]
17 && !rooms[this.x-1][this.y].state) {
18 rooms[this.x][this.y].clear();
19 rooms[this.x-1][this.y].join();
20 this.x--;
21 }
22 break;
23 case 'up':
24 case 'north':
25 if (rooms[this.x][this.y-1]
26 && !rooms[this.x][this.y-1].state) {
27 rooms[this.x][this.y].clear();
28 rooms[this.x][this.y-1].join();
29 this.y--;
30 }
31 break;
32 case 'right':
33 case 'east':
34 if (rooms[this.x+1] && rooms[this.x+1][this.y+1]
35 && !rooms[this.x+1][this.y].state) {
36 rooms[this.x][this.y].clear();
37 rooms[this.x+1][this.y].join();
38 this.x++;
39 }
40 break;
41 case 'down':
42 case 'south':
43 if (rooms[this.x][this.y+1]
44 && !rooms[this.x][this.y+1].state) {
45 rooms[this.x][this.y].clear();
46 rooms[this.x][this.y+1].join();
47 this.y++;
48 }
49 break;
50 }
51 }
52 }
53
54 function room(x, y, h, l) {
55 this.x = x;
56 this.y = y;
57 this.h = h;
58 this.l = l;
59 this.state = 0;
60 this.fill = function() {
61 c.fillStyle = "rgb(0,0,0)";
62 c.fillRect(this.x, this.y, this.h, this.l);
63 this.state = 1;
64 }
65 this.clear = function() {
66 c.fillStyle = "rgb(255,255,255)";
67 c.fillRect(this.x, this.y, this.h, this.l);
68 this.state = 0;
69 }
70 this.join = function() {
71 c.fillStyle = "rgb(0,255,0)";
72 c.fillRect(this.x, this.y, this.h, this.l);
73 }
74 }
75
76 function universe() {
77
78 this.rooms = [];
79
80 this.rows = 30;
81 this.columns = 30;
82
83 this.roomWidth = canvas.width / this.rows;
84 this.roomHeight = canvas.height / this.columns;
85
86 for (var i = 0; i < this.columns; i++) {
87 var column = [];
88 var x = i * this.roomWidth;
89 var y = 0;
90 for (var ii = 0; ii < this.columns; ii++) {
91 column.push(new room(x, y, this.roomWidth, this.roomHeight));
92 y += this.roomWidth;
93 Math.random() > .2 ? column[ii].clear() : column[ii].fill();
94 }
95 this.rooms.push(column);
96 }
97
98 this.populate = function(seed) {
99 for (var i = 0; i < this.rows; i++) {
100 for (var ii = 0; ii < this.columns; ii++) {
101 seed[i][ii] ? this.rooms[i][ii].clear()
102 : this.rooms[i][ii].fill();
103 }
104 }
105 }
106
107 this.join = function() {
108 do {
109 var x = Math.floor(Math.random()*(this.columns));
110 var y = Math.floor(Math.random()*(this.rows));
111 } while (!this.rooms[x][y].state);
112 this.rooms[x][y].join();
113 return player = new player(x,y);
114 }
115
116 $(document).keydown(function(e) {
117 if (typeof player == undefined) return;
118 switch (e.which) {
119 case 37:
120 player.move('left');
121 break;
122 case 38:
123 player.move('up');
124 break;
125 case 39:
126 player.move('right');
127 break;
128 case 40:
129 player.move('down');
130 break;
131 default:
132 return;
133 }
134 e.preventDefault();
135 });
136
137 var commands = {
138 tell : function(msg) {
139 var parts = msg.match(/^(\w+)\s(.*)/);
140 var dest = parts[1];
141 msg = parts[2];
142 $.ajax({
143 url: endpoint,
144 data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg },
145 success: function() {
146 writeToLog('You told ' + dest + ': ', 'tell', msg);
147 },
148 });
149 },
150 yell : function(msg) {
151 console.log('yell!');
152 $.ajax({
153 url: endpoint,
154 data: { 'cmd' : 'yell', 'msg' : msg },
155 success: function() {
156 writeToLog('You yelled: ', 'yell', msg);
157 },
158 });
159 },
160 move : function(direction) {
161 player.move(direction);
162 },
163 }
164 function writeToLog(action, style, msg) {
165 $('#log').append(
166 $('<div>').addClass('logline').append(
167 $('<span>').addClass(style).text(action),
168 $('<span>').addClass('msg').text(msg)
169 )
170 )
171 }
172 $('#submit').click(function() {
173 var text = $('#chat').val();
174 var parts = text.match(/^(\w+)\s(.*)/);
175 var cmd = parts[1];
176 text = parts[2];
177 if (commands[cmd] != undefined) {
178 commands[cmd](text);
179 $('#chat').val('');
180 }
181 });
182 $('#chat').keypress(function(e) {
183 if (e.which == '13') {
184 $('#submit').click();
185 }
186 });
187
188 }
189
190 var mud = new universe;
191 var player = mud.join();
192
193 });