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
) {
11 this.move = function(direction
) {
12 var rooms
= mud
.rooms
;
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();
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();
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();
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();
54 function room(x
, y
, h
, l
) {
60 this.fill = function() {
61 c
.fillStyle
= "rgb(0,0,0)";
62 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
65 this.clear = function() {
66 c
.fillStyle
= "rgb(255,255,255)";
67 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
70 this.join = function() {
71 c
.fillStyle
= "rgb(0,255,0)";
72 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
83 this.roomWidth
= canvas
.width
/ this.rows
;
84 this.roomHeight
= canvas
.height
/ this.columns
;
86 for (var i
= 0; i
< this.columns
; i
++) {
88 var x
= i
* this.roomWidth
;
90 for (var ii
= 0; ii
< this.columns
; ii
++) {
91 column
.push(new room(x
, y
, this.roomWidth
, this.roomHeight
));
93 Math
.random() > .2 ? column
[ii
].clear() : column
[ii
].fill();
95 this.rooms
.push(column
);
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();
107 this.join = function() {
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
);
116 $(document
).keydown(function(e
) {
117 if (typeof player
== undefined) return;
126 player
.move('right');
138 tell : function(msg
) {
139 var parts
= msg
.match(/^(\w+)\s(.*)/);
144 data
: { 'cmd' : 'tell', 'dest' : dest
, 'msg' : msg
},
145 success: function() {
146 writeToLog('You told ' + dest
+ ': ', 'tell', msg
);
150 yell : function(msg
) {
151 console
.log('yell!');
154 data
: { 'cmd' : 'yell', 'msg' : msg
},
155 success: function() {
156 writeToLog('You yelled: ', 'yell', msg
);
160 move : function(direction
) {
161 player
.move(direction
);
164 function writeToLog(action
, style
, msg
) {
166 $('<div>').addClass('logline').append(
167 $('<span>').addClass(style
).text(action
),
168 $('<span>').addClass('msg').text(msg
)
172 $('#submit').click(function() {
173 var text
= $('#chat').val();
174 var parts
= text
.match(/^(\w+)\s(.*)/);
177 if (commands
[cmd
] != undefined) {
182 $('#chat').keypress(function(e
) {
183 if (e
.which
== '13') {
184 $('#submit').click();
190 var mud
= new universe
;
191 var player
= mud
.join();