-
Notifications
You must be signed in to change notification settings - Fork 27
/
levels.js
175 lines (148 loc) · 4.85 KB
/
levels.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
/**
* This file describe different scenes
*/
//Loading Scene
Crafty.scene("Loading",function(){
var toLoad = [];
toLoad.push(game_path + "assets/img/loading.jpg", game_path + "assets/img/bg.png");
for(var i in Crafty.assets){
toLoad.push(i);
}
//Setup background image
Crafty.background("url("+game_path+"assets/img/loading.jpg) black");
//Select DOM elements
var bar = $('#load');
var button = $('.button');
var text = bar.find('.text');
$('#interface').hide();
//Setup progressbar
text.text("Loading ...");
bar.progressbar({
value:0
});
//Bind click event on button
button.live('click',function(){
//Start scene level 1
Crafty.scene("Level1");
});
$('.skip').live('click',function(){
bar.fadeOut(1000,function(){
button.show();
});
});
Crafty.load(toLoad,
function() {
//Everything is loaded
bar.fadeOut(1000,function(){
button.show();
});
},
function(e) {
var src = e.src ||"";
//update progress
text.text("Loading "+src.substr(src.lastIndexOf('/') + 1).toLowerCase()+" Loaded: "+~~e.percent+"%");
bar.progressbar({
value:~~e.percent
});
},
function(e) {
//uh oh, error loading
var src = e.src ||"";
console.log("Error on loading: "+src.substr(src.lastIndexOf('/') + 1).toLowerCase());
}
);
Crafty.audio.play("intro",-1);
},
//Uninit Scene
function(){
Crafty.audio.stop();
//Display loading interface
$('#loading').hide();
});
//Level 1 Scene
Crafty.scene("Level1",function(){
//Display interface
$('#interface').show();
//Setup background of level
Crafty.background("url(" + game_path + "/assets/img/bg.png)");
$('.level').text('Level: 1');
//Get the Interface elements
var bars = {
hp:$('#hp'),
heat:$('#heat'),
shield:$('#shield')
};
bars.hp.addClass('green');
bars.shield.addClass('green');
bars.heat.addClass('green');
var infos = {
lives :$('.lives'),
score: $('.score'),
hp:bars.hp.find('.text'),
heat:bars.heat.find('.text'),
shield:bars.shield.find('.text'),
alert:$('.alert')
}
var spotEnemys = function(frame){
//Spot each 50th Fram one Asteroid
if(frame % 50 == 0 && Crafty("Asteroid").length < 2 && Crafty("SmallAsteroid").length < 8){
Crafty.e("Asteroid");
}
if(frame % 70 == 0 && Crafty("Kamikaze").length < 1){
Crafty.e("Kamikaze");
}
if(frame % 80 == 0 && Crafty("Level1").length < 1){
Crafty.e("Rookie");
}
if(frame % 90 == 0 && Crafty("Level2").length < 1){
Crafty.e("Level2");
}
};
//Create the player
var player = Crafty.e("Player");
//Bind Gameloop to the Scene
Crafty.bind("EnterFrame",function(frame){
//Trigger Event to display enemies
spotEnemys(frame.frame);
//Setup Background position
Crafty.stage.elem.style.backgroundPosition ="0px "+frame.frame+"px";
});
//Bind UpdateStats Event
Crafty.bind("UpdateStats",function(){
//calculate percents
player.heat.percent = Math.round(player.heat.current/player.heat.max * 100);
player.hp.percent = Math.round(player.hp.current/player.hp.max * 100);
player.shield.percent = Math.round(player.shield.current/player.shield.max * 100);
//display the values
infos.heat.text('Heat: '+player.heat.current+ '/'+player.heat.max);
infos.hp.text('HP: '+player.hp.current+ '/'+player.hp.max);
infos.shield.text('Shield: '+player.shield.current+ '/'+player.shield.max);
infos.score.text("Score: "+player.score);
infos.lives.text("Lives: "+player.lives);
//Update progressbars
bars.heat.progressbar({
value:player.heat.percent
});
bars.hp.progressbar({
value:player.hp.percent
});
bars.shield.progressbar({
value:player.shield.percent
});
});
//Bind global Event Show Text
Crafty.bind("ShowText",function(text){
infos.alert.text(text).show().effect('pulsate',500)
});
Crafty.bind("HideText",function(){
infos.alert.text("").hide();
});
//Global Event for Game Over
Crafty.bind("GameOver",function(score){
Crafty.trigger("ShowText","Game Over!");
Crafty.audio.stop();
Crafty.audio.play("gameover",-1);
});
//Play background music and repeat
Crafty.audio.play("space",-1);
});