1 $(document
).ready(function(){
3 var canvas
= $('#universe')[0];
4 var c
= canvas
.getContext('2d');
5 var endpoint
= '/mud/mud.php';
7 function player(x
, y
, id
) {
12 mud
.rooms
[this.x
][this.y
].join();
14 this.move = function(direction
) {
15 var rooms
= mud
.rooms
;
19 if (rooms
[this.x
-1] && rooms
[this.x
-1][this.y
]
20 && !rooms
[this.x
-1][this.y
].state
) {
23 // 'name' : 'majuscule',
24 // 'direction' : 'west'
26 rooms
[this.x
][this.y
].clear();
27 rooms
[this.x
-1][this.y
].join();
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();
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();
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();
63 function room(x
, y
, width
, height
) {
70 this.fill = function() {
71 c
.fillStyle
= "rgb(0,0,0)";
72 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
75 this.clear = function(other
, player
) {
78 if (!this.population
) {
79 c
.fillStyle
= "rgb(255,255,255)";
80 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
83 this.join = function(other
, player
) {
84 c
.fillStyle
= !other
|| (player
&& mud
&& mud
.player
85 && mud
.player
.x
== player
.x
&& mud
.player
.y
== player
.y
)
86 ? "rgb(0,255,0)" : "rgb(0,0,255)";
87 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
98 this.build = function(seed
) {
99 this.rows
= seed
.length
;
100 this.columns
= seed
[0].length
;
101 this.roomWidth
= canvas
.width
/ this.rows
;
102 this.roomHeight
= canvas
.height
/ this.columns
;
104 for (var i
= 0; i
< this.columns
; i
++) {
106 var x
= i
* this.roomWidth
;
108 for (var ii
= 0; ii
< this.rows
; ii
++) {
109 this.rooms
[i
][ii
] = new room(x
, y
, this.roomWidth
, this.roomHeight
);
110 if (seed
[i
][ii
]) this.rooms
[i
][ii
].fill();
111 y
+= this.roomHeight
;
116 this.join = function(name
) {
117 $.getJSON(endpoint
, { 'cmd' : 'join', 'name' : name
}, function(json
) {
118 self
.player
= new player(json
.x
, json
.y
, json
.id
);
120 setInterval(self
.poll
, 1000);
121 self
.populate(json
.poll
.players
);
126 tell : function(msg
) {
128 var parts
= msg
.match(/^(\w+)\s(.*)/);
129 if (!parts
[1]) return;
134 // data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg },
135 // success: function() {
136 writeToLog('You told ' + dest
+ ': ', 'tell', msg
);
140 yell : function(msg
) {
143 data
: { 'cmd' : 'yell', 'msg' : msg
},
144 success: function() {
145 writeToLog('You yelled: ', 'yell', msg
);
149 say : function(msg
) {
152 // data: { 'cmd' : 'yell', 'msg' : msg },
153 // success: function() {
154 writeToLog('You said: ', 'say', msg
);
158 move : function(direction
) {
159 this.player
.move(direction
);
162 function writeToLog(action
, style
, msg
) {
164 $('<div>').addClass('logline').append(
165 $('<span>').addClass(style
).text(action
),
166 $('<span>').addClass('msg').text(msg
)
169 $("#log").animate({ scrollTop
: $('#log')[0].scrollHeight
}, 1000);
171 $('#submit').click(function() {
172 var text
= $('#chat').val();
173 var parts
= text
.match(/^(\w+)\s(.*)/);
177 if (commands
[cmd
] != undefined) {
183 $('#chat').keydown(function(e
) {
184 if (e
.which
== '13') {
185 typeof self
.player
== 'undefined' ?
186 $('#join').click() : $('#submit').click();
187 } else if (e
.which
>= 37 && e
.which
<= 40)
190 $('#join').click(function() {
191 var chat
= $('#chat');
192 if (chat
.val() == '') {
193 chat
.css('border-color', 'red');
196 mud
.join(chat
.val());
197 chat
.css('border-color', 'black').val('');
198 $(this).fadeOut('slow', function() {
199 $('#log, #submit').fadeIn('slow')
200 .css('display', 'inline-block');
204 $(document
).keydown(function(e
) {
205 if (typeof self
.player
== 'undefined') return;
208 self
.player
.move('left');
211 self
.player
.move('up');
214 self
.player
.move('right');
217 self
.player
.move('down');
225 $.getJSON(endpoint
, { 'cmd' : 'start' }, function(json
) {
227 }).fail(function(jqxhr
, textStatus
, error
) {
228 console
.log(jqxhr
, jqxhr
.responseText
, textStatus
, error
);
231 this.populate = function(players
) {
232 for (var i
in this.players
)
233 mud
.rooms
[this.players
[i
].x
][this.players
[i
].y
].clear();
234 this.players
= players
;
235 for (var i
in this.players
)
236 mud
.rooms
[this.players
[i
].x
][this.players
[i
].y
].join(1, this.players
[i
]);
239 this.poll = function() {
240 $.getJSON(endpoint
, { 'cmd' : 'poll' }, function(json
) {
241 for (var i
in json
.messages
) {
242 var msg
= messages
[i
];
243 if (msg
.id
== self
.player
.id
) continue;
244 writeToLog(msg
.name
+ ':', msg
.type
, msg
.message
);
245 $("#log").animate({ scrollTop
: $('#log')[0].scrollHeight
}, 1000);
247 self
.populate(json
.players
);