1 $(document
).ready(function(){
3 var canvas
= $('#world')[0];
4 var c
= canvas
.getContext('2d');
6 function player(x
, y
) {
10 this.move = function(direction
) {
11 var rooms
= mud
.rooms
;
15 if (rooms
[this.x
-1] && rooms
[this.x
-1][this.y
]
16 && !rooms
[this.x
-1][this.y
].state
) {
17 rooms
[this.x
][this.y
].clear();
18 rooms
[this.x
-1][this.y
].join();
24 if (rooms
[this.x
][this.y
-1]
25 && !rooms
[this.x
][this.y
-1].state
) {
26 rooms
[this.x
][this.y
].clear();
27 rooms
[this.x
][this.y
-1].join();
33 if (rooms
[this.x
+1] && rooms
[this.x
+1][this.y
+1]
34 && !rooms
[this.x
+1][this.y
].state
) {
35 rooms
[this.x
][this.y
].clear();
36 rooms
[this.x
+1][this.y
].join();
42 if (rooms
[this.x
][this.y
+1]
43 && !rooms
[this.x
][this.y
+1].state
) {
44 rooms
[this.x
][this.y
].clear();
45 rooms
[this.x
][this.y
+1].join();
53 function room(x
, y
, h
, l
) {
59 this.fill = function() {
60 c
.fillStyle
= "rgb(0,0,0)";
61 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
64 this.clear = function() {
65 c
.fillStyle
= "rgb(255,255,255)";
66 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
69 this.join = function() {
70 c
.fillStyle
= "rgb(0,255,0)";
71 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
81 this.roomWidth
= canvas
.width
/ this.rows
;
82 this.roomHeight
= canvas
.height
/ this.columns
;
84 for (var i
= 0; i
< this.columns
; i
++) {
86 var x
= i
* this.roomWidth
;
88 for (var ii
= 0; ii
< this.columns
; ii
++) {
89 column
.push(new room(x
, y
, this.roomWidth
, this.roomHeight
));
91 Math
.random() > .5 ? column
[ii
].clear() : column
[ii
].fill();
93 this.rooms
.push(column
);
96 this.populate = function(seed
) {
97 for (var i
= 0; i
< this.rows
; i
++) {
98 for (var ii
= 0; ii
< this.columns
; ii
++) {
99 seed
[i
][ii
] ? this.rooms
[i
][ii
].clear()
100 : this.rooms
[i
][ii
].fill();
105 this.join = function() {
107 var x
= Math
.floor(Math
.random()*(this.columns
));
108 var y
= Math
.floor(Math
.random()*(this.rows
));
109 } while (!this.rooms
[x
][y
].state
);
110 this.rooms
[x
][y
].join();
111 return player
= new player(x
,y
);
114 $(document
).keydown(function(e
) {
115 if (typeof player
== undefined) return;
124 player
.move('right');
136 tell : function() {},
137 yell : function() {},
138 move : function(direction
) {
139 player
.move(direction
);
142 $('#submit').click(function() {
143 var text
= $('#chat').val();
144 var parts
= text
.match(/^(\w+)\s(.*)/);
147 if (commands
[cmd
] != undefined) commands
[cmd
](text
);
149 $('#chat').keypress(function(e
) {
150 if (e
.which
== '13') {
151 $('#submit').click();
158 var mud
= new universe
;
159 var player
= mud
.join();