minor improvements (scroll down on msg)
[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, id) {
8 this.x = x;
9 this.y = y;
10 this.id = id;
11
12 mud.rooms[this.x][this.y].join();
13
14 this.move = function(direction) {
15 var rooms = mud.rooms;
16 switch (direction) {
17 case 'left':
18 case 'west':
19 if (rooms[this.x-1] && rooms[this.x-1][this.y]
20 && !rooms[this.x-1][this.y].state) {
21 // $.get(endpoint, {
22 // 'cmd' : 'move',
23 // 'name' : 'majuscule',
24 // 'direction' : 'west'
25 // }, function() {
26 rooms[this.x][this.y].clear();
27 rooms[this.x-1][this.y].join();
28 this.x--;
29 // });
30 }
31 break;
32 case 'up':
33 case 'north':
34 if (rooms[this.x][this.y-1]
35 && !rooms[this.x][this.y-1].state) {
36 rooms[this.x][this.y].clear();
37 rooms[this.x][this.y-1].join();
38 this.y--;
39 }
40 break;
41 case 'right':
42 case 'east':
43 if (rooms[this.x+1] && rooms[this.x+1][this.y]
44 && !rooms[this.x+1][this.y].state) {
45 rooms[this.x][this.y].clear();
46 rooms[this.x+1][this.y].join();
47 this.x++;
48 }
49 break;
50 case 'down':
51 case 'south':
52 if (rooms[this.x][this.y+1]
53 && !rooms[this.x][this.y+1].state) {
54 rooms[this.x][this.y].clear();
55 rooms[this.x][this.y+1].join();
56 this.y++;
57 }
58 break;
59 }
60 }
61 }
62
63 function room(x, y, width, height) {
64 this.x = x;
65 this.y = y;
66 this.width = width;
67 this.height = height;
68 this.state = 0;
69 this.fill = function() {
70 c.fillStyle = "rgb(0,0,0)";
71 c.fillRect(this.x, this.y, this.height, this.width);
72 this.state = 1;
73 }
74 this.clear = function() {
75 c.fillStyle = "rgb(255,255,255)";
76 c.fillRect(this.x, this.y, this.height, this.width);
77 this.state = 0;
78 }
79 this.join = function() {
80 c.fillStyle = "rgb(0,255,0)";
81 c.fillRect(this.x, this.y, this.height, this.width);
82 }
83 }
84
85 function universe() {
86
87 var self = this;
88 this.rooms = [];
89
90 this.build = function(seed) {
91 this.rows = seed.length;
92 this.columns = seed[0].length;
93 this.roomWidth = canvas.width / this.rows;
94 this.roomHeight = canvas.height / this.columns;
95
96 for (var i = 0; i < this.columns; i++) {
97 this.rooms[i] = [];
98 var x = i * this.roomWidth;
99 var y = 0;
100 for (var ii = 0; ii < this.rows; ii++) {
101 this.rooms[i][ii] = new room(x, y, this.roomWidth, this.roomHeight);
102 if (seed[i][ii]) this.rooms[i][ii].fill();
103 y += this.roomHeight;
104 }
105 }
106 }
107
108 this.join = function() {
109 $.getJSON(endpoint, { 'cmd' : 'join' }, function(json) {
110 self.player = new player(json.x, json.y, json.id);
111 setInterval(self.poll, 1000);
112 });
113 }
114
115 var commands = {
116 tell : function(msg) {
117 console.log(msg);
118 var parts = msg.match(/^(\w+)\s(.*)/);
119 if (!parts[1]) return;
120 var dest = parts[1];
121 msg = parts[2];
122 // $.ajax({
123 // url: endpoint,
124 // data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg },
125 // success: function() {
126 writeToLog('You told ' + dest + ': ', 'tell', msg);
127 // },
128 // });
129 },
130 yell : function(msg) {
131 $.ajax({
132 url: endpoint,
133 data: { 'cmd' : 'yell', 'msg' : msg },
134 success: function() {
135 writeToLog('You yelled: ', 'yell', msg);
136 },
137 });
138 },
139 say : function(msg) {
140 // $.ajax({
141 // url: endpoint,
142 // data: { 'cmd' : 'yell', 'msg' : msg },
143 // success: function() {
144 writeToLog('You said: ', 'say', msg);
145 // },
146 // });
147 },
148 move : function(direction) {
149 this.player.move(direction);
150 },
151 }
152 function writeToLog(action, style, msg) {
153 $('#log').append(
154 $('<div>').addClass('logline').append(
155 $('<span>').addClass(style).text(action),
156 $('<span>').addClass('msg').text(msg)
157 )
158 );
159 $("#log").animate({ scrollTop: $('#log')[0].scrollHeight}, 1000);
160 }
161 $('#submit').click(function() {
162 var text = $('#chat').val();
163 var parts = text.match(/^(\w+)\s(.*)/);
164 if (parts) {
165 var cmd = parts[1];
166 text = parts[2];
167 if (commands[cmd] != undefined) {
168 commands[cmd](text);
169 $('#chat').val('');
170 }
171 }
172 });
173 $('#chat').keydown(function(e) {
174 if (e.which == '13') {
175 $('#submit').click();
176 } else if (e.which >= 37 && e.which <= 40)
177 e.stopPropagation();
178 });
179 $('#join').click(function() {
180 mud.join();
181 $(this).fadeOut('slow');
182 });
183
184 $(document).keydown(function(e) {
185 if (typeof self.player == 'undefined') return;
186 switch (e.which) {
187 case 37:
188 self.player.move('left');
189 break;
190 case 38:
191 self.player.move('up');
192 break;
193 case 39:
194 self.player.move('right');
195 break;
196 case 40:
197 self.player.move('down');
198 break;
199 default:
200 return;
201 }
202 e.preventDefault();
203 });
204
205 $.getJSON(endpoint, { 'cmd' : 'start' }, function(json) {
206 self.build(json)
207 })
208 .fail(function(jqxhr, textStatus, error) {
209 console.log(jqxhr, jqxhr.responseText, textStatus, error);
210 });
211
212 this.poll = function() {
213 $.getJSON(endpoint, { 'cmd' : 'poll' }, function(messages) {
214 for (var i in messages) {
215 var msg = messages[i];
216 if (msg.id == self.player.id) continue;
217 writeToLog(msg.name + ':', msg.type, msg.message);
218 $("#log").animate({ scrollTop: $('#log')[0].scrollHeight}, 1000);
219 }
220 });
221 }
222 }
223
224 mud = new universe;
225
226 });