-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
276 lines (230 loc) · 6.76 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
if (!gotHello) {
helloData = JSON.parse(e.data);
console.log(helloData['pix']['player_1']);
GameEngine.setSprite(helloData['pix']['player_1']);
gotHello = true;
} else {
goRun = true;
data = JSON.parse(e.data);
map = data; // REM LATER
}
*/
// The basics
var fx = !(window.mozInnerScreenX == null);
var GRID_WIDTH = 960;
var GRID_HEIGHT = 480;
var BLOCK_SIZE = 24;
data = [];
map = [];
isRunning = true;
gotHello = false;
goRun = false;
offset = 0.0;
user_offset = 0;
helloData = [];
// WebSockets
var sock = null;
var port = 1338;
var wsuri = "ws://moosegame.jump:"+ port;
function connect() {
sock = new WebSocket(wsuri);
sock.onopen = function() {
GameEngine.init();
}
sock.onclose = function(e) {
window.setInterval("connect", 1);
sock = new WebSocket(wsuri);
}
sock.onmessage = function(e) {
try
{
/* I DONT */
data.push(e);
}
catch(error)
{
}
GameEngine.loop();
}
};
function send(msg) {
sock.send(msg);
};
var GameEngine = {
//Variables
//methods
init : function(){
this.activeCanvas = document.getElementById('zero');
this.inactiveCanvas = document.getElementById('one');
this.activeCanvas.setAttribute('width',GRID_WIDTH);
this.activeCanvas.setAttribute('height',GRID_HEIGHT);
this.inactiveCanvas.setAttribute('width',GRID_WIDTH);
this.inactiveCanvas.setAttribute('height',GRID_HEIGHT);
//this.sprite = new Image();
//this.sprite.src = "px/sprite.png";
evarglice.bind('keydown',[document.body],GameEngine.keyDown);
evarglice.bind('keyup',[document.body],GameEngine.keyUp);
evarglice.bind('mousedown',[document.body],GameEngine.mouseDown);
this.keyIsPressed = [];
this.keyIsPressed[32] = 0;
this.keyIsPressed[37] = 0;
this.keyIsPressed[38] = 0;
this.keyIsPressed[39] = 0;
this.keyIsPressed[40] = 0;
for (var i = 0; i < 9; i++) {
this.keyIsPressed[48+i] = 0;
}
// window.setInterval(this.loop,10);
return true;
},
/* should be several , add sprite, remove and so on*/
setSprite: function (sprites) {
this.sprite = new Image();
this.sprite.src = sprites[0].data;
},
mouseDown : function(data) {
},
mouseUp: function(data) {
},
keyDown: function(data){
if(data.keyCode == 32 || data.keyCode == 37 || data.keyCode == 38 || data.keyCode == 39 || data.keyCode == 40)
{
if(!GameEngine.keyIsPressed[data.keyCode])
{
keyPress = {type: 'pressed',keyCode:data.keyCode};
//send keypress to server
send(JSON.stringify(keyPress));
GameEngine.keyIsPressed[data.keyCode] = 1;
}
if (fx){return false; }
event.preventDefault();
}
},
keyUp: function(data){
if(data.keyCode == 32 || data.keyCode == 37 || data.keyCode == 38 || data.keyCode == 39 || data.keyCode == 40){
keyPress = {type: 'released',keyCode:data.keyCode};
send(JSON.stringify(keyPress));
GameEngine.keyIsPressed[data.keyCode] = 0;
if (fx){return false; }
event.preventDefault();
return false;
}
},
flip : function(){
tmp = this.activeCanvas;
this.activeCanvas = this.inactiveCanvas;
this.inactiveCanvas = tmp;
this.inactiveCanvas.className='gamecanvas';
this.activeCanvas.className='gamecanvas active';
return true;
},
loop: function()
{
if (isRunning){
GameEngine.update();
GameEngine.draw();
}
},
load_pix: function(pix) {
/* named obj get pix*/
this.pix = this.pix.concat(pix);
},
update: function() {
while (data.length != 0) {
d = data.shift();
recvdata = JSON.parse(d.data);
for (var key in recvdata){
value = recvdata[key];
switch (key) {
case 'msg': break;
case 'sprites': GameEngine.setSprite(value); break;
case "players":
map.players = value;
//console.log( value );
break;
case "map": map = value; break;
case 'world_width': map.world_width = value; break;
case 'world_height': map.world_height = value; break;
case 'world_tiles' : map.world_tiles = value; break;
case 'load_music':
loadMusicFile( value );
break;
//case 'music': GameEngine.load_audio(value); break;
case 'camera_offset': offset = value; break;
case 'user_offset': user_offset = value; break;
//case "pix": GameEngine.load_pix(e); break;
case 'sound_effects':
loadSoundList( value );
break;
default: console.log("undefined data from be");console.log(key, value); break;
}
}
}
},
draw: function(){
this.ctx = this.inactiveCanvas.getContext("2d");
this.ctx.clearRect(0,0,GRID_WIDTH,GRID_HEIGHT);
if(typeof map == 'object')
{
//Draw the world
for(x = 0; x < map.world_width; x++)
{
for(y = 0; y < map.world_height; y++)
{
if(map.world_tiles[x][y])
this.ctx.drawImage(this.sprite,9*BLOCK_SIZE-4,0,BLOCK_SIZE,BLOCK_SIZE, x * BLOCK_SIZE, (y - offset) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
level = 2;
//Draw the players
if (map.players != undefined) {
color = 0;
for(c = 0; c < map.players.length; c++)
{
// nada
//moose left horn
if(map.players[c].direction)
{
this.ctx.drawImage(this.sprite, 55, 2 * BLOCK_SIZE,
BLOCK_SIZE+10, BLOCK_SIZE+5, map.players[c].pos_x *BLOCK_SIZE + (map.players[c].direction *(12)),
(map.players[c].pos_y) * BLOCK_SIZE-18, BLOCK_SIZE +10 , BLOCK_SIZE+5);
}
else
{
this.ctx.drawImage(this.sprite, 1*BLOCK_SIZE, 2 * BLOCK_SIZE,
BLOCK_SIZE+10, BLOCK_SIZE+5, map.players[c].pos_x *BLOCK_SIZE-20,
(map.players[c].pos_y) * BLOCK_SIZE-18, BLOCK_SIZE+10 , BLOCK_SIZE +5);
}
//moose
this.ctx.drawImage(this.sprite, color * BLOCK_SIZE, map.players[c].direction * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE, map.players[c].pos_x * BLOCK_SIZE,
(map.players[c].pos_y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
if(map.players[c].direction)
{
this.ctx.drawImage(this.sprite, 1*BLOCK_SIZE, 2 * BLOCK_SIZE,
BLOCK_SIZE+10, BLOCK_SIZE+5, map.players[c].pos_x *BLOCK_SIZE-20,
(map.players[c].pos_y) * BLOCK_SIZE-18, BLOCK_SIZE+10 , BLOCK_SIZE +5);
}
else{
//moose right horn
// this.ctx.drawImage(this.sprite, 10, 2 * BLOCK_SIZE,
// BLOCK_SIZE, BLOCK_SIZE, map.players[c].pos_x * BLOCK_SIZE-4,
// (map.players[c].pos_y) * BLOCK_SIZE-3, BLOCK_SIZE , BLOCK_SIZE);
this.ctx.drawImage(this.sprite, 55, 2 * BLOCK_SIZE,
BLOCK_SIZE+10, BLOCK_SIZE+5, map.players[c].pos_x *BLOCK_SIZE + (map.players[c].direction *(12)),
(map.players[c].pos_y) * BLOCK_SIZE-18, BLOCK_SIZE +10 , BLOCK_SIZE+5);
}
// Play any sound assocciated with the player.
map.players[ c ].sfx_playlist.forEach( function(a) { play(a); } );
}
if(color +1 >=5)
color = 0;
else
color = color +1;
}
this.flip();
}
}
}
connect();