Skip to content
Browse files

some changes at loader, added Global Events

  • Loading branch information...
1 parent 2fd5724 commit 710909a3295df6300e51fec20b62afc9235744d2 @BlackScorp committed
Showing with 126 additions and 98 deletions.
  1. +17 −4 assets/js/audio.js
  2. +5 −1 assets/js/game.js
  3. +75 −12 assets/js/levels.js
  4. +9 −8 assets/js/loader.js
  5. +19 −72 assets/js/player.js
  6. +1 −1 assets/js/sounds.js
View
21 assets/js/audio.js
@@ -25,7 +25,7 @@ Crafty.extend({
},
audioElement:function(){
//IE does not support Audio Object
- return typeof Audio !== 'undefined' ? new Audio() : document.createElement('audio');
+ return typeof Audio !== 'undefined' ? new Audio("") : document.createElement('audio');
},
add:function(id,url){
Crafty.support.audio = !!this.audioElement().canPlayType; //Setup audio support
@@ -66,7 +66,11 @@ Crafty.extend({
if(this.supported[ext]){
audio.src = url;
if (!Crafty.assets[url]) Crafty.assets[url] = audio;
-
+ this.sounds[id] = {
+ obj:audio,
+ played:0
+ }
+
}
}
@@ -112,11 +116,20 @@ Crafty.extend({
};
},
stop:function(id){
- if(!Crafty.support.audio || !this.sounds[id]) return;
- var s = this.sounds[id];
+ if(!Crafty.support.audio) return;
+ var s;
+ if(!id){
+ for(var i in this.sounds){
+ s = this.sounds[i];
+ if(!s.obj.paused) s.obj.pause();
+ }
+ }
+ if(!this.sounds[id]) return;
+ s = this.sounds[id];
if(!s.obj.paused) s.obj.pause();
},
mute:function(){
+ if(!Crafty.support.audio) return;
var s;
if(!this.muted){
for(var i in this.sounds){
View
6 assets/js/game.js
@@ -1,6 +1,9 @@
+//Enable Console log in opera
+if(window.opera){ console = {log:window.opera.postError} }
/**
* This is the Main JS File
*/
+
$(function(){
//Init Crafty
@@ -12,4 +15,5 @@ $(function(){
//play the loading scene
Crafty.scene("Loading");
-});
+});
+
View
87 assets/js/levels.js
@@ -4,7 +4,7 @@
//Loading Scene
Crafty.scene("Loading",function(){
var toLoad = [];
- toLoad.push(game_path + "assets/img/loading.jpg", game_path + "assets/img/bg.png",game_path + "media/music/spaceship.ogg");
+ toLoad.push(game_path + "assets/img/loading.jpg", game_path + "assets/img/bg.png");
for(var i in Crafty.assets){
toLoad.push(i);
}
@@ -50,25 +50,48 @@ Crafty.scene("Loading",function(){
},
function(e) {
//uh oh, error loading
-
+
}
);
- //Play background music and repeat will work only Safari /IE
- Crafty.audio.play("spaceship",-1);
- Crafty.audio.play("spaceship.ogg",-1); //Works with others
+ Crafty.audio.play("gameover",-1);
+},
+//Uninit Scene
+function(){
+ Crafty.audio.stop();
});
//Level 1 Scene
Crafty.scene("Level1",function(){
//Stop loading sound
- Crafty.audio.stop("spaceship");
- Crafty.audio.stop("spaceship.ogg");
+ Crafty.audio.stop();
//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
@@ -87,16 +110,56 @@ Crafty.scene("Level1",function(){
}
};
//Create the player
- Crafty.e("Player");
- //Bin Gameloop to the Scene
- this.bind("EnterFrame",function(frame){
+ 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);
View
17 assets/js/loader.js
@@ -56,12 +56,12 @@ Crafty.extend({
//Progress function
function pro(){
var src = this.src;
+
+ //Remove events cause audio trigger this event more than once(depends on browser)
if (this.removeEventListener) {
this.removeEventListener('canplaythrough', pro, true);
- }else{
- this.detachEvent('canplaythrough',pro);
}
- this.onload=null;
+
++j;
//if progress callback, give information of assets loaded, total and percent
if (onprogress)
@@ -102,20 +102,20 @@ Crafty.extend({
obj = Crafty.audio.audioElement();
obj.id = name;
obj.src = current;
+ obj.preload = "auto";
+ obj.volume = Crafty.audio.volume;
if (!Crafty.assets[current]) Crafty.assets[current] = obj;
Crafty.audio.sounds[name] = {
obj:obj,
played:0
}
}
+
if (obj.addEventListener) {
obj.addEventListener('canplaythrough', pro, true);
- }else if(obj.attachEvent){
- obj.attachEvent('oncanplaythrough',pro);
- }else{
- //obj.oncanplaythrough=pro;
}
- //
+
+
} else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
if(!obj) {
obj = new Image();
@@ -123,6 +123,7 @@ Crafty.extend({
}
obj.onload=pro;
obj.src = current; //setup src after onload function Opera/IE Bug
+
} else {
total--;
continue; //skip if not applicable
View
91 assets/js/player.js
@@ -29,27 +29,10 @@ Crafty.c("Player",{
preparing:true,
bounce:false,
init:function(){
- this.bars = {
- hp:$('#hp'),
- heat:$('#heat'),
- shield:$('#shield')
- };
- this.infos = {
- lives :$('.lives'),
- score: $('.score'),
- hp:this.bars.hp.find('.text'),
- heat:this.bars.heat.find('.text'),
- shield:this.bars.shield.find('.text'),
- alert:$('.alert')
- }
+
var stage = $('#cr-stage');
- this.bars.hp.addClass('green');
- this.bars.shield.addClass('green');
- this.bars.heat.addClass('green');
-
var keyDown = false; //Player didnt pressed a key
- this
- .requires("2D,Canvas,"+this.ship+",Multiway,Keyboard,Collision,Flicker") /*Add needed Components*/
+ this.requires("2D,Canvas,"+this.ship+",Multiway,Keyboard,Collision,Flicker") /*Add needed Components*/
.multiway(this.movementSpeed, { /*Enable Movement Control*/
UP_ARROW: -90,
DOWN_ARROW: 90,
@@ -89,11 +72,11 @@ Crafty.c("Player",{
this.heat.current = ~~(this.heat.current*29/30);
}
- this.updateHeat();
+ Crafty.trigger("UpdateStats");
if(this.weapon.overheated && this.heat.percent < 85){
this.weapon.overheated = false;
- this.infos.alert.hide();
+ Crafty.trigger("HideText");
}
}
@@ -110,15 +93,17 @@ Crafty.c("Player",{
})
.bind("Killed",function(points){
this.score += points;
- this.updateScore();
+ Crafty.trigger("UpdateStats");
})
.bind("Hurt",function(dmg){
if(this.flicker) return;
if(this.bounce == false) {
this.bounce = true;
var t = this;
- stage.effect('highlight',{color:'#990000'},100,function(){
- t.bounce = false;
+ stage.effect('highlight',{
+ color:'#990000'
+ },100,function(){
+ t.bounce = false;
});
}
Crafty.e("Damage").attr({
@@ -131,8 +116,7 @@ Crafty.c("Player",{
}else{
this.shield.current -= dmg;
}
- this.updateShield();
- this.updateHp();
+ Crafty.trigger("UpdateStats");
if(this.hp.current <= 0) this.die();
})
.onHit("EnemyBullet",function(ent){
@@ -143,14 +127,16 @@ Crafty.c("Player",{
.bind("RestoreHP",function(val){
if(this.hp.current < this.hp.max){
this.hp.current += val;
- this.updateHp();
+ Crafty.trigger("UpdateStats");
}
+
})
.bind("RestoreShield",function(val){
if(this.shield.current < this.shield.max){
this.shield.current += val;
- this.updateShield();
+ Crafty.trigger("UpdateStats");
}
+
})
.reset() /*Set initial points*/;
return this;
@@ -171,11 +157,7 @@ Crafty.c("Player",{
max:100,
percent:0
}
- this.updateHp();
- this.updateShield();
- this.updateHeat();
- this.updateLives();
- this.updateScore();
+ Crafty.trigger("UpdateStats");
//Init position
this.x = Crafty.viewport.width/2-this.w/2;
this.y = Crafty.viewport.height-this.h-36;
@@ -185,6 +167,7 @@ Crafty.c("Player",{
},
shoot:function(){
if(this.preparing) return;
+
var bullet = Crafty.e(this.weapon.name,"PlayerBullet");
bullet.attr({
playerID:this[0],
@@ -199,7 +182,7 @@ Crafty.c("Player",{
this.heat.current ++;
if(this.heat.current >= this.heat.max){
- this.infos.alert.text('Weapon Overheated!').show().effect('pulsate',500);
+ Crafty.trigger("ShowText","Weapon Overheated!");
this.weapon.overheated = true;
}
@@ -210,51 +193,15 @@ Crafty.c("Player",{
y:this.y
});
this.lives--;
- this.updateLives();
+ Crafty.trigger("UpdateStats");
if(this.lives <= 0){
this.destroy();
- this.infos.alert.show().text('Game Over!').effect('pulsate',500);
- gameHooks.endGame(this.score);
-
- Crafty.pause();
+ Crafty.trigger("GameOver",this.score);
}else{
-
this.reset();
}
- },
- updateHeat:function(){
-
- this.heat.percent = Math.round(this.heat.current/this.heat.max * 100);
- this.infos.heat.text('Heat: '+this.heat.current+ '/'+this.heat.max);
- this.bars.heat.progressbar({
- value:this.heat.percent
- });
- },
- updateHp:function(){
- this.hp.percent = Math.round(this.hp.current/this.hp.max * 100);
- this.infos.hp.text('HP: '+this.hp.current+ '/'+this.hp.max);
- this.bars.hp.progressbar({
- value:this.hp.percent
- });
- },
- updateShield:function(){
- this.shield.percent = Math.round(this.shield.current/this.shield.max * 100);
- this.infos.shield.text('Shield: '+this.shield.current+ '/'+this.shield.max);
- this.bars.shield.progressbar({
- value:this.shield.percent
- });
- },
- updateScore:function(){
- this.infos.score.text("Score: "+this.score);
- },
- updateLives:function(){
- this.infos.lives.text("Lives: "+this.lives);
}
});
-
-var gameHooks = {
- endGame: function (score) {}
-}
View
2 assets/js/sounds.js
@@ -31,5 +31,5 @@ Crafty.audio.add("space",[
game_path + "media/music/through-space.ogg"
]);
-
+Crafty.audio.add("gameover",game_path + "media/music/ambient.ogg");
Crafty.audio.add("spaceship", game_path + "media/music/spaceship.mp3"); //Will work only Safari/IE

0 comments on commit 710909a

Please sign in to comment.
Something went wrong with that request. Please try again.