Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
508 lines (395 loc) · 12.6 KB
/
Copy pathapp.js
File metadata and controls
508 lines (395 loc) · 12.6 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Require external node.js modules
*/
var express = require('express'),
http = require('http'),
Buffer = require('buffer').Buffer;
/*
* Random name generator library
*/
var Moniker = require('moniker');
/*
* Define express framework settings
*/
var app = express();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
/*
* Serve views/index.ejs when requesting domain
*/
app.get('/', function(req, res){
res.render('index');
});
/*
* Start Webserver
*/
var server = http.createServer(app).listen(3000);
console.log("Izveidots http serveris, listening on port 3000");
/*
* Start Socket.io
*/
var io = require('socket.io').listen(server);
/*
* Include jQuery Extend
* Include Box2D library
*/
var Box2D = require('./public/javascripts/box2d.js');
/*
* Declare all the commonly used objects as variables for convenience
*/
var b2Vec2 = Box2D.Common.Math.b2Vec2;
var b2BodyDef = Box2D.Dynamics.b2BodyDef;
var b2Body = Box2D.Dynamics.b2Body;
var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
var b2Fixture = Box2D.Dynamics.b2Fixture;
var b2World = Box2D.Dynamics.b2World;
var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
var b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef;
var game = {
// Start initializing objects, preloading assets and display start screen
init: function(){
levels.load(0);
game.timeStep = 1/60;
game.activeAnimation = false;
game.status = "intro";
// DEBUG VARIABLES
game.sentStuff = false;
// ALLOWED USERS
game.totalUsers = 2;
},
checkCollision: function(posX,posY){
var mousePositionVec = new b2Vec2(posX,posY);
function clickedOn(fixture) {
var b = fixture.GetBody();
var entity = b.GetUserData();
if(entity.name == 'ice'){
box2d.world.DestroyBody(b);
}
return true;
}
box2d.world.QueryPoint(clickedOn, mousePositionVec);
},
checkSuccess: function(){
var levelComplete = true;
for (var body = box2d.world.GetBodyList(); body; body = body.GetNext()) {
var entity = body.GetUserData();
if(entity){
if(entity.name == 'ice'){
levelComplete = false;
}
}
}
if(levelComplete){
game.nextLevel();
}
},
nextLevel: function(){
if(game.currentLevel < levels.data.length-1){
levels.load(game.currentLevel + 1);
io.sockets.in('istaba').emit('levelInfo', {currentLevel:game.currentLevel,totalLevels:levels.data.length});
}else{
io.sockets.in('istaba').emit('levelInfo', {currentLevel:(game.currentLevel+1),totalLevels:levels.data.length});
game.status = "intro";
processUsers.kickEverybody();
game.activeAnimation = false;
levels.load(0);
}
},
restartLevel: function(){
//console.log("restart lvl");
io.sockets.in('istaba').emit('levelInfo', {currentLevel:game.currentLevel,totalLevels:levels.data.length});
levels.load(game.currentLevel);
},
prepareGameData: function(){
var dataToSend = Array();
for (var body = box2d.world.GetBodyList(); body; body = body.GetNext()) {
var entity = body.GetUserData();
if(entity){
if(entity.type == "block"){
var bodyPosition = body.GetPosition();
var elX = bodyPosition.x*30;
var elY = bodyPosition.y*30;
var elAngle = body.GetAngle();
var elName = entity.name;
var elType = entity.type;
var elementInfo = {
x: elX,
y: elY,
angle: elAngle,
name: elName,
type: elType,
};
dataToSend.push(elementInfo);
}
}
}
return dataToSend;
}
}
var levels = {
// Level data
data:[
{
background:'bg',
entities:[
{type:"ground", name:"ground", x:320,y:408,width:640,height:25,isStatic:true},//Ground
{type:"ground", name:"ground", x:12,y:210,width:25,height:600,isStatic:true},// Left side
{type:"ground", name:"ground", x:628,y:210,width:25,height:600,isStatic:true},// Right side
{type:"block", name:"ice", x:240,y:374,angle:0,width:40,height:40},
{type:"block", name:"ice", x:240,y:334,angle:0,width:40,height:40},
{type:"block", name:"ice", x:240,y:294,angle:0,width:40,height:40},
{type:"block", name:"ice", x:400,y:374,angle:0,width:40,height:40},
{type:"block", name:"ice", x:400,y:334,angle:0,width:40,height:40},
{type:"block", name:"ice", x:400,y:294,angle:0,width:40,height:40},
{type:"block", name:"platform", x:320,y:261,angle:0,width:240,height:25},
{type:"block", name:"tnt", x:320,y:218,angle:0,width:60,height:60},
]
},
{
background:'bg',
entities:[
{type:"ground", name:"ground", x:320,y:408,width:640,height:25,isStatic:true},//Ground
{type:"ground", name:"ground", x:12,y:210,width:25,height:600,isStatic:true},// Left side
{type:"ground", name:"ground", x:628,y:210,width:25,height:600,isStatic:true},// Right side
{type:"block", name:"ice", x:200,y:374,angle:0,width:40,height:40},
{type:"block", name:"ice", x:240,y:374,angle:0,width:40,height:40},
{type:"block", name:"ice", x:220,y:334,angle:0,width:40,height:40},
{type:"block", name:"ice", x:235,y:294,angle:0,width:40,height:40},
{type:"block", name:"ice", x:250,y:254,angle:0,width:40,height:40},
{type:"block", name:"ice", x:440,y:374,angle:0,width:40,height:40},
{type:"block", name:"ice", x:400,y:374,angle:0,width:40,height:40},
{type:"block", name:"ice", x:420,y:334,angle:0,width:40,height:40},
{type:"block", name:"ice", x:405,y:294,angle:0,width:40,height:40},
{type:"block", name:"ice", x:390,y:254,angle:0,width:40,height:40},
{type:"block", name:"platform", x:320,y:221,angle:0,width:240,height:25},
{type:"block", name:"tnt", x:320,y:178,angle:0,width:60,height:60},
]
},
],
load:function(number){
console.log("levels.load");
box2d.init();
game.currentLevel = number;
var level = levels.data[number];
// Load all the entities
for (var i = level.entities.length - 1; i >= 0; i--){
var entity = level.entities[i];
entities.create(entity);
};
},
}
var entities = {
definitions:{
"tnt":{
density:0.2,
friction:0.1,
restitution:0.4,
},
"ice":{
density:0.3,
friction:0.4,
restitution:0.4,
},
"platform":{
density:0.2,
friction:0.4,
restitution:0.4,
},
"ground":{
density:0.7,
friction:0.4,
restitution:0.4,
},
},
create:function(entity){
var definition = entities.definitions[entity.name];
switch(entity.type){
case 'ground':
entity.shape = "rectangle";
box2d.createRectangle(entity,definition);
break;
case 'block':
case 'tnt':
entity.shape = "rectangle";
box2d.createRectangle(entity,definition);
break;
default:
}
},
}
var box2d = {
scale:30,
init:function(){
console.log("box2d.init");
var gravity = new b2Vec2(0,9.8); //declare gravity as 9.8 m/s^2 downwards
var allowSleep = true; //Allow objects that are at rest to fall asleep and be excluded from calculations
box2d.world = new b2World(gravity,allowSleep);
var listener = new Box2D.Dynamics.b2ContactListener;
listener.PostSolve = function(contact,impulse){
var body1 = contact.GetFixtureA().GetBody();
var body2 = contact.GetFixtureB().GetBody();
var entity1 = body1.GetUserData();
var entity2 = body2.GetUserData();
//console.log("test");
if(entity1.name == "tnt" && entity2.name != "platform"){
game.restartLevel();
}
}
box2d.world.SetContactListener(listener);
},
step:function(timeStep){
// velocity iterations = 8
// position iterations = 3
box2d.world.Step(timeStep,8,3);
},
createRectangle:function(entity,definition){
var bodyDef = new b2BodyDef;
if(entity.isStatic){
bodyDef.type = b2Body.b2_staticBody;
} else {
bodyDef.type = b2Body.b2_dynamicBody;
}
bodyDef.position.x = entity.x/box2d.scale;
bodyDef.position.y = entity.y/box2d.scale;
if (entity.angle) {
bodyDef.angle = Math.PI*entity.angle/180;
}
var fixtureDef = new b2FixtureDef;
fixtureDef.density = definition.density;
fixtureDef.friction = definition.friction;
fixtureDef.restitution = definition.restitution;
fixtureDef.shape = new b2PolygonShape;
fixtureDef.shape.SetAsBox(entity.width/2/box2d.scale,entity.height/2/box2d.scale);
var body = box2d.world.CreateBody(bodyDef);
body.SetUserData(entity);
var fixture = body.CreateFixture(fixtureDef);
return body;
},
}
var animateGame = setInterval(function(){
if(game.activeAnimation){
box2d.step(1/30);
game.checkSuccess();
// Send update to users
var dataToSend = game.prepareGameData();
io.sockets.in('istaba').emit('gameData', dataToSend);
}
},1.0/60.0*1000);
var processUsers = {
addUser: function(){
var user = {
name: Moniker.choose(),
joined: false,
}
users.push(user);
return user;
},
removeUser: function(user){
for(var i=0; i<users.length; i++) {
if(user.name === users[i].name) {
users.splice(i, 1);
return;
}
}
},
getJoinedCount: function(){
var joinedCount = 0;
for(var i=0; i<users.length; i++) {
if(users[i].joined){
joinedCount++;
}
}
return joinedCount;
},
getPlayingCount: function(){
var playingCount = 0;
for(var i=0; i<users.length; i++) {
if(users[i].playing){
playingCount++;
}
}
return playingCount;
},
sendJoinedCount: function(){
io.sockets.emit("playingATM", { joinedNow: processUsers.getJoinedCount(), total: game.totalUsers});
console.log(processUsers.getJoinedCount());
},
kickEverybody: function(){
for(var i=0; i<users.length; i++) {
users[i].playing = false;
users[i].joined = false;
}
io.sockets.emit("playingATM", { joinedNow: processUsers.getJoinedCount(), total: game.totalUsers});
},
}
/*
* Socket.io
*/
// Reduce logging
io.set('log level', 1); // reduce logging
//
var users = [];
io.sockets.on('connection', function (socket) {
// Send joined player count on connection
socket.emit("playingATM", { joinedNow: processUsers.getJoinedCount(), total: game.totalUsers});
// Define user
var user = processUsers.addUser();
socket.on('disconnect', function () {
if(user.playing){
game.status = "intro";
game.activeAnimation = false;
io.sockets.in('istaba').emit('playerDisconnected', true);
processUsers.sendJoinedCount();
levels.load(0);
}
processUsers.removeUser(user);
processUsers.sendJoinedCount();
});
// LOBBY - Listen to JOIN button
socket.on("join", function(data) {
console.log("Join - " + data);
if(processUsers.getJoinedCount() < game.totalUsers && !user.joined){
socket.join('istaba');
user.joined = true;
}
processUsers.sendJoinedCount();
// Send info only to already joined folks
if(processUsers.getJoinedCount() == game.totalUsers){
io.sockets.in('istaba').emit('readyToPlay', true);
}
});
// LOBBY - Listen to PLAY button
socket.on("play", function(data) {
console.log("Play - " + data);
if(processUsers.getJoinedCount() == game.totalUsers && game.status == "intro"){
user.playing = true;
}
console.log("Playing skaits = " + processUsers.getPlayingCount());
if(processUsers.getPlayingCount() == game.totalUsers && game.status == "intro"){
console.log("spele sakas");
game.activeAnimation = true;
io.sockets.in('istaba').emit('gameOn', true);
game.status = "game-on";
io.sockets.in('istaba').emit('levelInfo', {currentLevel:game.currentLevel,totalLevels:levels.data.length});
}
});
// GAME - listen to user clicks in canvas
socket.on("click", function(data) {
//console.log(data);
game.checkCollision(data.x,data.y);
});
});
// Start game..
game.init();