bind chat commands
[mudd.git] / 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 case 'west':
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();
19 this.x--;
20 }
21 break;
22 case 'up':
23 case 'north':
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();
28 this.y--;
29 }
30 break;
31 case 'right':
32 case 'east':
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();
37 this.x++;
38 }
39 break;
40 case 'down':
41 case 'south':
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();
46 this.y++;
47 }
48 break;
49 }
50 }
51 }
52
53 function room(x, y, h, l) {
54 this.x = x;
55 this.y = y;
56 this.h = h;
57 this.l = l;
58 this.state = 0;
59 this.fill = function() {
60 c.fillStyle = "rgb(0,0,0)";
61 c.fillRect(this.x, this.y, this.h, this.l);
62 this.state = 1;
63 }
64 this.clear = function() {
65 c.fillStyle = "rgb(255,255,255)";
66 c.fillRect(this.x, this.y, this.h, this.l);
67 this.state = 0;
68 }
69 this.join = function() {
70 c.fillStyle = "rgb(0,255,0)";
71 c.fillRect(this.x, this.y, this.h, this.l);
72 }
73 }
74
75 function universe() {
76 this.rooms = [];
77
78 this.rows = 30;
79 this.columns = 30;
80
81 this.roomWidth = canvas.width / this.rows;
82 this.roomHeight = canvas.height / this.columns;
83
84 for (var i = 0; i < this.columns; i++) {
85 var column = [];
86 var x = i * this.roomWidth;
87 var y = 0;
88 for (var ii = 0; ii < this.columns; ii++) {
89 column.push(new room(x, y, this.roomWidth, this.roomHeight));
90 y += this.roomWidth;
91 Math.random() > .5 ? column[ii].clear() : column[ii].fill();
92 }
93 this.rooms.push(column);
94 }
95
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();
101 }
102 }
103 }
104
105 this.join = function() {
106 do {
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);
112 }
113
114 $(document).keydown(function(e) {
115 if (typeof player == undefined) return;
116 switch (e.which) {
117 case 37:
118 player.move('left');
119 break;
120 case 38:
121 player.move('up');
122 break;
123 case 39:
124 player.move('right');
125 break;
126 case 40:
127 player.move('down');
128 break;
129 default:
130 return;
131 }
132 e.preventDefault();
133 });
134
135 var commands = {
136 tell : function() {},
137 yell : function() {},
138 move : function(direction) {
139 player.move(direction);
140 },
141 }
142 $('#submit').click(function() {
143 var text = $('#chat').val();
144 var parts = text.match(/^(\w+)\s(.*)/);
145 var cmd = parts[1];
146 text = parts[2];
147 if (commands[cmd] != undefined) commands[cmd](text);
148 });
149 $('#chat').keypress(function(e) {
150 if (e.which == '13') {
151 $('#submit').click();
152 $('#chat').val('');
153 }
154 });
155
156 }
157
158 var mud = new universe;
159 var player = mud.join();
160
161 });