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
;
14 if (rooms
[this.x
-1] && rooms
[this.x
-1][this.y
]
15 && !rooms
[this.x
-1][this.y
].state
) {
16 rooms
[this.x
][this.y
].clear();
17 rooms
[this.x
-1][this.y
].join();
22 if (rooms
[this.x
][this.y
-1]
23 && !rooms
[this.x
][this.y
-1].state
) {
24 rooms
[this.x
][this.y
].clear();
25 rooms
[this.x
][this.y
-1].join();
30 if (rooms
[this.x
+1] && rooms
[this.x
+1][this.y
+1]
31 && !rooms
[this.x
+1][this.y
].state
) {
32 rooms
[this.x
][this.y
].clear();
33 rooms
[this.x
+1][this.y
].join();
38 if (rooms
[this.x
][this.y
+1]
39 && !rooms
[this.x
][this.y
+1].state
) {
40 rooms
[this.x
][this.y
].clear();
41 rooms
[this.x
][this.y
+1].join();
49 function room(x
, y
, h
, l
) {
55 this.fill = function() {
56 c
.fillStyle
= "rgb(0,0,0)";
57 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
60 this.clear = function() {
61 console
.log('clearing');
62 c
.fillStyle
= "rgb(255,255,255)";
63 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
66 this.join = function() {
67 c
.fillStyle
= "rgb(0,255,0)";
68 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
78 this.roomWidth
= canvas
.width
/ this.rows
;
79 this.roomHeight
= canvas
.height
/ this.columns
;
81 for (var i
= 0; i
< this.columns
; i
++) {
83 var x
= i
* this.roomWidth
;
85 for (var ii
= 0; ii
< this.columns
; ii
++) {
86 column
.push(new room(x
, y
, this.roomWidth
, this.roomHeight
));
88 Math
.random() > .5 ? column
[ii
].clear() : column
[ii
].fill();
90 this.rooms
.push(column
);
93 this.populate = function(seed
) {
94 for (var i
= 0; i
< this.rows
; i
++) {
95 for (var ii
= 0; ii
< this.columns
; ii
++) {
96 seed
[i
][ii
] ? this.rooms
[i
][ii
].clear()
97 : this.rooms
[i
][ii
].fill();
102 this.join = function() {
104 var x
= Math
.floor(Math
.random()*(this.columns
));
105 var y
= Math
.floor(Math
.random()*(this.rows
));
106 } while (!this.rooms
[x
][y
].state
);
107 this.rooms
[x
][y
].join();
108 return player
= new player(x
,y
);
111 $(document
).keydown(function(e
) {
112 if (typeof player
== undefined) return;
121 player
.move('right');
133 var mud
= new universe
;
134 var player
= mud
.join();