2d3aad1150405140fa2bc3c58edb45e281828754
[dylansserver.git] / mud / mud.js
1 $(document).ready(function(){
2
3 var canvas = $('#world')[0];
4 var c = canvas.getContext('2d');
5
6 function player(x, y) {
7 this.x = x;
8 this.y = y;
9
10 this.move = function(direction) {
11 var rooms = mud.rooms;
12 switch (direction) {
13 case 'left':
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();
18 this.x++;
19 }
20 break;
21 case 'up':
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();
26 this.y++;
27 }
28 break;
29 case 'right':
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();
34 this.x++;
35 }
36 break;
37 case 'down':
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();
42 this.y++;
43 }
44 break;
45 }
46 }
47 }
48
49 function room(x, y, h, l) {
50 this.x = x;
51 this.y = y;
52 this.h = h;
53 this.l = l;
54 this.state = 0;
55 this.fill = function() {
56 c.fillStyle = "rgb(0,0,0)";
57 c.fillRect(this.x, this.y, this.h, this.l);
58 this.state = 1;
59 }
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);
64 this.state = 0;
65 }
66 this.join = function() {
67 c.fillStyle = "rgb(0,255,0)";
68 c.fillRect(this.x, this.y, this.h, this.l);
69 }
70 }
71
72 function universe() {
73 this.rooms = [];
74
75 this.rows = 30;
76 this.columns = 30;
77
78 this.roomWidth = canvas.width / this.rows;
79 this.roomHeight = canvas.height / this.columns;
80
81 for (var i = 0; i < this.columns; i++) {
82 var column = [];
83 var x = i * this.roomWidth;
84 var y = 0;
85 for (var ii = 0; ii < this.columns; ii++) {
86 column.push(new room(x, y, this.roomWidth, this.roomHeight));
87 y += this.roomWidth;
88 Math.random() > .5 ? column[ii].clear() : column[ii].fill();
89 }
90 this.rooms.push(column);
91 }
92
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();
98 }
99 }
100 }
101
102 this.join = function() {
103 do {
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);
109 }
110
111 $(document).keydown(function(e) {
112 if (typeof player == undefined) return;
113 switch (e.which) {
114 case 37:
115 player.move('left');
116 break;
117 case 38:
118 player.move('up');
119 break;
120 case 39:
121 player.move('right');
122 break;
123 case 40:
124 player.move('down');
125 break;
126 default:
127 return;
128 }
129 e.preventDefault();
130 });
131 }
132
133 var mud = new universe;
134 var player = mud.join();
135
136 });