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
) {
69 this.fill = function() {
70 c
.fillStyle
= "rgb(0,0,0)";
71 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
74 this.clear = function() {
75 c
.fillStyle
= "rgb(255,255,255)";
76 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
79 this.join = function() {
80 c
.fillStyle
= "rgb(0,255,0)";
81 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
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
;
96 for (var i
= 0; i
< this.columns
; i
++) {
98 var x
= i
* this.roomWidth
;
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
;
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);
116 tell : function(msg
) {
118 var parts
= msg
.match(/^(\w+)\s(.*)/);
119 if (!parts
[1]) return;
124 // data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg },
125 // success: function() {
126 writeToLog('You told ' + dest
+ ': ', 'tell', msg
);
130 yell : function(msg
) {
133 data
: { 'cmd' : 'yell', 'msg' : msg
},
134 success: function() {
135 writeToLog('You yelled: ', 'yell', msg
);
139 say : function(msg
) {
142 // data: { 'cmd' : 'yell', 'msg' : msg },
143 // success: function() {
144 writeToLog('You said: ', 'say', msg
);
148 move : function(direction
) {
149 this.player
.move(direction
);
152 function writeToLog(action
, style
, msg
) {
154 $('<div>').addClass('logline').append(
155 $('<span>').addClass(style
).text(action
),
156 $('<span>').addClass('msg').text(msg
)
159 $("#log").animate({ scrollTop
: $('#log')[0].scrollHeight
}, 1000);
161 $('#submit').click(function() {
162 var text
= $('#chat').val();
163 var parts
= text
.match(/^(\w+)\s(.*)/);
167 if (commands
[cmd
] != undefined) {
173 $('#chat').keydown(function(e
) {
174 if (e
.which
== '13') {
175 $('#submit').click();
176 } else if (e
.which
>= 37 && e
.which
<= 40)
179 $('#join').click(function() {
181 $(this).fadeOut('slow');
184 $(document
).keydown(function(e
) {
185 if (typeof self
.player
== 'undefined') return;
188 self
.player
.move('left');
191 self
.player
.move('up');
194 self
.player
.move('right');
197 self
.player
.move('down');
205 $.getJSON(endpoint
, { 'cmd' : 'start' }, function(json
) {
208 .fail(function(jqxhr
, textStatus
, error
) {
209 console
.log(jqxhr
, jqxhr
.responseText
, textStatus
, error
);
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);