-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
405 lines (373 loc) · 12.8 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
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
$(function(){
soundManager.debugMode = false;
var uid, accessToken;
// score
var fbEnabled = false;
var score = 0;
var topScore = 0;
var topScoreHolder = "";
var playerLife = 3;
var updateScore = function() {
var scoreDiv = $("#score");
$("#score div").remove();
var scoreToDisplay = score;
for(var i=6 ; i >= 0; i--){
var display = Math.floor(scoreToDisplay / Math.pow(10, i));
scoreDiv.addSprite("num_"+i,{animation: font[display], posx: (5-i)*14, height: 14, width: 12});
scoreToDisplay -= display * Math.pow(10, i);
}
};
var updateTopScore = function() {
var scoreDiv = $("#topscore");
$("#topscore div").remove();
var scoreToDisplay = topScore;
for(var i=6 ; i >= 0; i--){
var display = Math.floor(scoreToDisplay / Math.pow(10, i));
scoreDiv.addSprite("num_t_"+i,{animation: font[display], posx: (5-i)*14, height: 14, width: 12});
scoreToDisplay -= display * Math.pow(10, i);
}
$("#topScore").html("Top Score amongst your friends<br/><strong>"+topScoreHolder+": "+topScore+"pt</strong>");
};
var leaderBoard = function () {
if(fbEnabled){
FB.api('/XXXXXXXXXXXXXXX/scores', function(response) {
$("#gameover").html("Your score: "+score+"pt<br/><br/><strong>Top Scores amongst your friends</strong><br/>");
for(var i = 0; i < response.data.length; i++) {
$("#gameover").append(response.data[i].user.name+": "+response.data[i].score+"pt</br>")
}
$("#gameover").append("<a href='.'>Retry</a>");
});
} else {
$("#gameover").html("Your score: "+score+"pt<br/><a href='.'>Retry</a>");
}
}
var saveScore = function() {
if(fbEnabled){
FB.api('/'+uid+'/scores', function(response) {
if((response.data.length === 0) || (score > response.data[0].score)) {
$.ajax({
type: 'POST',
async: false,
url: 'savescore.php' + '?score=' + score,
success: function(response) {
leaderBoard();
}
});
} else {
leaderBoard();
}
});
} else {
leaderBoard();
}
}
var over = false;
var gameOver = function() {
over = true;
laser3.stop();
enemy_dies.stop();
bibischSound.stop();
$("#gameover").show().fadeTo(0, 0.9);
// publish high score to FB
saveScore();
}
// define sounds
var laser3 = new $.gameQuery.SoundWrapper("laser3.mp3", false);
var enemy_dies = new $.gameQuery.SoundWrapper("die.mp3", false);
var bibischSound = new $.gameQuery.SoundWrapper("bibisch.mp3", true);
//define animations
var font = [];
font[0] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 0});
font[1] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 12});
font[2] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 24});
font[3] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 36});
font[4] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 48});
font[5] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 60});
font[6] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 72});
font[7] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 84});
font[8] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 96});
font[9] = new $.gameQuery.Animation({imageURL: "font.png", offsetx: 108});
var playerLaser = [];
playerLaser[0] = new $.gameQuery.Animation({imageURL: "shoot_player.png", offsety: 0});
playerLaser[1] = new $.gameQuery.Animation({imageURL: "shoot_player.png", offsety: 14});
playerLaser[2] = new $.gameQuery.Animation({imageURL: "shoot_player.png", offsety: 28});
playerLaser[3] = new $.gameQuery.Animation({imageURL: "shoot_player.png", offsety: 42});
playerLaser[4] = new $.gameQuery.Animation({imageURL: "shoot_player.png", offsety: 56});
var unilLaser = new $.gameQuery.Animation({imageURL: "shoot_unil.png",
numberOfFrame: 3,
delta: 12,
rate: 300,
type: $.gameQuery.ANIMATION_HORIZONTAL});
var background = new $.gameQuery.Animation({imageURL: "back.png"});
var player = new $.gameQuery.Animation({imageURL: "player.png"});
var shoot = new $.gameQuery.Animation({imageURL: "shoot.png"});
var enemy1 = new $.gameQuery.Animation({imageURL: "enemies.png",
numberOfFrame: 2,
delta: 24,
rate: 600,
type: $.gameQuery.ANIMATION_VERTICAL});
var enemy2 = new $.gameQuery.Animation({imageURL: "enemies.png",
numberOfFrame: 2,
offsetx: 27,
delta: 24,
rate: 600,
type: $.gameQuery.ANIMATION_VERTICAL});
var enemy3 = new $.gameQuery.Animation({imageURL: "enemies.png",
numberOfFrame: 2,
offsetx: 63,
delta: 24,
rate: 600,
type: $.gameQuery.ANIMATION_VERTICAL});
var explosion = new $.gameQuery.Animation({imageURL: "enemies.png",
numberOfFrame: 3,
offsetx: 102,
delta: 24,
rate: 300,
type: $.gameQuery.ANIMATION_VERTICAL | $.gameQuery.ANIMATION_ONCE | $.gameQuery.ANIMATION_CALLBACK});
var bibisch = new $.gameQuery.Animation({imageURL: "bibisch.png"});
var life = new $.gameQuery.Animation({imageURL: "life.png"});
// sets the div to use to display the game and its dimension
$("#playground").playground({width: 512, height: 512, keyTracker: true})
.addSprite("background", {posx: 0, posy: 0, animation: background, width: 512, height: 512})
.addSprite("player", {posx: 241, posy: 476, animation: player, width: 30, height: 19})
.addGroup("enemies", {posy: 80}).end()
.addGroup("score", {posx: 24, posy: 40, height: 14, width: 100}).end()
.addGroup("topscore", {posx: 230, posy: 40, height: 14, width: 100}).end()
.addSprite("life1", {posx: 480, posy: 40, animation: life, width: 19, height: 10})
.addSprite("life2", {posx: 455, posy: 40, animation: life, width: 19, height: 10})
.addSprite("life3", {posx: 430, posy: 40, animation: life, width: 19, height: 10})
// generate enemies
var dir = 1;
var generateEnemies = function() {
dir = 1;
for(var i=0; i < 10; i++){
$("#enemies")
.addSprite("enemy1a_"+i, {posx: i * 40 + 5, posy: 0, animation: enemy1, width: 23, height: 23})
.addSprite("enemy1b_"+i, {posx: i * 40 + 5, posy: 40, animation: enemy1, width: 23, height: 23})
.addSprite("enemy2a_"+i, {posx: i * 40 + 2, posy: 80, animation: enemy2, width: 32, height: 23})
.addSprite("enemy2b_"+i, {posx: i * 40 + 2, posy: 120, animation: enemy2, width: 32, height: 23})
.addSprite("enemy3a_"+i, {posx: i * 40, posy: 160, animation: enemy3, width: 35, height: 23})
.addSprite("enemy3b_"+i, {posx: i * 40, posy: 200, animation: enemy3, width: 35, height: 23});
$("#enemy1a_"+i).data("offset",7).addClass("enemy");
$("#enemy1b_"+i).data("offset",7).addClass("enemy");
$("#enemy2a_"+i).data("offset",4).addClass("enemy");
$("#enemy2b_"+i).data("offset",4).addClass("enemy");
$("#enemy3a_"+i).data("offset",2).addClass("enemy");
$("#enemy3b_"+i).data("offset",2).addClass("enemy");
}
}
generateEnemies();
// configure the loading bar
$.loadCallback(function(percent){
$("#loadBar").width(400*percent);
});
// register the start button and remove the splash screen once the game is ready to starts
$("#start").click(function(){
$.playground().startGame(function(){
updateScore();
updateTopScore();
$("#splash").remove();
// configure the mute button
$.playground().append("<div id='mutebutton' class='muteButton' style='position: absolute; top: 0px; z-index:1000'></div>");
$("#mutebutton").click(function(){
$this = $(this);
$this.toggleClass("muteButton");
$this.toggleClass("unmuteButton");
if($this.data("muted")){
$.muteSound(false);
$this.data("muted", false);
} else {
$.muteSound(true);
$this.data("muted", true);
}
return false;
});
});
return false;
});
// control
var missilCounter = 0;
var bibischUsed = false;
$.playground().registerCallback(function(){
if(!over){
if($.gameQuery.keyTracker[65]){ //this is left! (a)
if($("#player").x() > 10){
$("#player").x(-10, true);
}
}
if($.gameQuery.keyTracker[68]){ //this is right! (d)
if($("#player").x() < 480){
$("#player").x(10, true);
}
}
}
}, 90);
var canShoot = true;
var bibischActive = false;
$.playground().registerCallback(function(){
canShoot = true;
}, 300);
$(document).keydown(function(){
if(!over){
if($.gameQuery.keyTracker[77] && canShoot){ //this is shoot! (m)
//Shoot
laser3.play();
canShoot = false;
missilCounter = (missilCounter + 1) % 10000;
$.playground().addSprite("missil_"+missilCounter, {animation: playerLaser[Math.floor(Math.random()*5)], posx: $("#player").x() + 10, posy: 461, width: 10, height: 14});
$("#missil_"+missilCounter).addClass("missil");
}
if($.gameQuery.keyTracker[66] && !bibischUsed){
bibischSound.play();
bibischUsed = true;
bibischActive = true;
$("#background").addSprite("bibisch",{animation: bibisch, width: 141, height: 180, posx: -141, posy:200});
}
}
});
// game Logic
var deleteEnemy = function () {
if(!$(this).data("dead")){
enemy_dies.play();
$(this).x(-$(this).data("offset"), true);
$(this).w(38);
$(this).setAnimation(explosion, function(node){
$(node).remove();
});
score += 10;
updateScore();
}
$(this).data("dead", true);
}
$.playground().registerCallback(function(){
if(!over){
// move missil up
$(".missil").each(function(){
$(this).y(-10,true);
if($(this).y() < 0) {
$(this).remove();
}
// test collison
var collide = $(this).collision("#enemies, .enemy");
if(collide.size() > 0){
$(this).remove();
collide.each(deleteEnemy);
}
});
$(".missil_unil").each(function() {
$(this).y(5,true);
if($(this).y() > 475) {
$(this).remove();
}
})
$("#player").collision(".missil_unil").each(function() {
$(this).remove();
$("#life"+playerLife).remove();
playerLife--;
if(playerLife < 0){
// gameOver
gameOver();
return false;
}
})
if(bibischActive){
$("#bibisch").x(10, true);
if($("#bibisch").x() > 512){
$("#bibisch").remove();
bibischActive = false;
bibischSound.stop();
}
var collide = $("#bibisch").collision("#enemies, .enemy");
if(collide.size() > 0){
$(this).remove();
collide.each(deleteEnemy);
}
}
}
}, 90)
var speed = 1200;
$.playground().registerCallback(function(){
if(!over){
// move invader arround
$("#enemies").x(10*dir, true);
if($("#enemies").x() > 110){
$("#enemies").x(110);
dir = -1;
$("#enemies").y(40, true);
}
if($("#enemies").x() < 0){
$("#enemies").x(0);
dir = 1;
$("#enemies").y(40, true);
}
var bottom = 0;
$(".enemy").each(function(){
var toto = $(this).y() + $("#enemies").y();
if($(this).y() + $("#enemies").y() > bottom) {
bottom = $(this).y() + $("#enemies").y();
}
});
if(bottom > 400){
// gameOver
gameOver();
return false;
} else {
if(Math.random() > 0.5) {
missilCounter++;
$.playground().addSprite("missil_unil_"+missilCounter, {animation: unilLaser, posx: Math.floor(Math.random()*400), posy: bottom, width: 12, height: 20});
$("#missil_unil_"+missilCounter).addClass("missil_unil");
}
}
if($(".enemy").size() == 0){
$("#enemies").xy(0, 80);
generateEnemies();
score += 50;
speed *= 0.9;
return speed;
}
}
}, speed);
// Facebook integration:
// get Score:
var getFbScore = function () {
if(fbEnabled){
FB.api('/XXXXXXXXXXXXXXX/scores', function(response) {
topScore = response.data[0].score;
topScoreHolder = response.data[0].user.name;
updateTopScore();
});
}
};
// init
FB.init({
appId : 'XXXXXXXXXXXXXXX', // App ID
channelUrl : '//CHANEL_URL', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
});
// login button:
FB.getLoginStatus(function(response) {
if (response.authResponse) {
//user is already logged in and connected
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
fbEnabled = true;
getFbScore();
} else {
$("#fb-start").show();
}
});
$("#fb-start").click(function(){
FB.login(function(response) {
if (response.authResponse) {
$("#fb-start").hide();
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
fbEnabled = true;
getFbScore();
}
},{scope: "publish_actions"});
return false;
});
});