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
) {
13 mud
.rooms
[this.x
][this.y
].join();
15 this.move = function(direction
) {
16 var rooms
= mud
.rooms
;
18 function move(direction
, oldroom
, newroom
) {
21 'direction' : direction
25 if (json
&& json
.description
)
26 writeToLog('', 'env', json
.description
);
33 if (rooms
[this.x
-1] && rooms
[this.x
-1][this.y
]
34 && !rooms
[this.x
-1][this.y
].state
) {
35 move('west', rooms
[self
.x
][self
.y
], rooms
[self
.x
-1][self
.y
])
41 if (rooms
[this.x
][this.y
-1]
42 && !rooms
[this.x
][this.y
-1].state
) {
43 move('north', rooms
[this.x
][this.y
], rooms
[this.x
][this.y
-1]);
49 if (rooms
[this.x
+1] && rooms
[this.x
+1][this.y
]
50 && !rooms
[this.x
+1][this.y
].state
) {
51 move('east', rooms
[this.x
][this.y
], rooms
[this.x
+1][this.y
]);
57 if (rooms
[this.x
][this.y
+1]
58 && !rooms
[this.x
][this.y
+1].state
) {
59 move('south', rooms
[this.x
][this.y
], rooms
[this.x
][this.y
+1]);
67 function room(x
, y
, width
, height
) {
74 this.fill = function() {
75 c
.fillStyle
= "rgb(0,0,0)";
76 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
79 this.clear = function(other
, player
) {
82 if (!this.population
) {
83 c
.fillStyle
= "rgb(255,255,255)";
84 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
87 this.join = function(other
, player
) {
88 c
.fillStyle
= !other
|| (player
&& mud
&& mud
.player
89 && mud
.player
.x
== player
.x
&& mud
.player
.y
== player
.y
)
90 ? "rgb(0,255,0)" : "rgb(0,0,255)";
91 c
.fillRect(this.x
, this.y
, this.height
, this.width
);
102 this.build = function(seed
) {
103 this.rows
= seed
.length
;
104 this.columns
= seed
[0].length
;
105 this.roomWidth
= canvas
.width
/ this.rows
;
106 this.roomHeight
= canvas
.height
/ this.columns
;
108 for (var i
= 0; i
< this.columns
; i
++) {
110 var x
= i
* this.roomWidth
;
112 for (var ii
= 0; ii
< this.rows
; ii
++) {
113 this.rooms
[i
][ii
] = new room(x
, y
, this.roomWidth
, this.roomHeight
);
114 if (seed
[i
][ii
]) this.rooms
[i
][ii
].fill();
115 y
+= this.roomHeight
;
120 this.join = function(name
) {
121 $.getJSON(endpoint
, { 'cmd' : 'join', 'name' : name
}, function(json
) {
122 self
.player
= new player(json
.x
, json
.y
, json
.id
);
123 setInterval(self
.poll
, 1000);
124 self
.populate(json
.poll
.players
);
129 tell : function(msg
) {
130 var parts
= msg
.match(/^(\w+)\s(.*)/);
131 if (!parts
[1]) return;
136 data
: { 'cmd' : 'tell', 'dest' : dest
, 'msg' : msg
},
137 success: function() {
138 self
.writeToLog('You told ' + dest
+ ': ', 'tell', msg
);
142 yell : function(msg
) {
145 data
: { 'cmd' : 'yell', 'msg' : msg
},
146 success: function() {
147 self
.writeToLog('You yelled: ', 'yell', msg
);
151 say : function(msg
) {
154 data
: { 'cmd' : 'say', 'msg' : msg
},
155 success: function() {
156 self
.writeToLog('You said: ', 'say', msg
);
160 move : function(direction
) {
161 this.player
.move(direction
);
164 this.writeToLog = function(action
, style
, msg
) {
166 $('<div>').addClass('logline').append(
167 $('<span>').addClass(style
).text(action
),
168 $('<span>').addClass('msg').text(msg
)
171 $("#log").animate({ scrollTop
: $('#log')[0].scrollHeight
}, 1000);
173 $('#submit').click(function() {
174 var text
= $('#chat').val();
175 var parts
= text
.match(/^(\w+)\s(.*)/);
179 if (commands
[cmd
] != undefined) {
185 $('#chat').keydown(function(e
) {
186 if (e
.which
== '13') {
187 typeof self
.player
== 'undefined' ?
188 $('#join').click() : $('#submit').click();
189 } else if (e
.which
>= 37 && e
.which
<= 40)
192 $('#join').click(function() {
193 var chat
= $('#chat');
194 if (chat
.val() == '') {
195 chat
.css('border-color', 'red');
198 mud
.join(chat
.val());
199 chat
.css('border-color', 'black').val('');
200 $(this).fadeOut('slow', function() {
201 $('#log, #submit').fadeIn('slow')
202 .css('display', 'inline-block');
206 $(document
).keydown(function(e
) {
207 if (typeof self
.player
== 'undefined') return;
210 self
.player
.move('left');
213 self
.player
.move('up');
216 self
.player
.move('right');
219 self
.player
.move('down');
227 $.getJSON(endpoint
, { 'cmd' : 'start' }, function(json
) {
229 }).fail(function(jqxhr
, textStatus
, error
) {
230 console
.log(jqxhr
, jqxhr
.responseText
, textStatus
, error
);
233 this.populate = function(players
) {
234 for (var i
in this.players
)
235 if (this.players
[i
].id
!= this.player
.id
)
236 mud
.rooms
[this.players
[i
].x
][this.players
[i
].y
].clear();
237 this.players
= players
;
238 for (var i
in this.players
)
239 if (this.players
[i
].id
!= this.player
.id
)
240 mud
.rooms
[this.players
[i
].x
][this.players
[i
].y
].join(1, this.players
[i
]);
243 this.poll = function() {
244 $.getJSON(endpoint
, { 'cmd' : 'poll' }, function(json
) {
245 for (var i
in json
.messages
) {
246 var msg
= json
.messages
[i
];
247 if (msg
.id
== self
.player
.id
) continue;
248 self
.writeToLog(msg
.name
+ ':', msg
.type
, msg
.message
);
249 $("#log").animate({ scrollTop
: $('#log')[0].scrollHeight
}, 1000);
251 self
.populate(json
.players
);