Initial commit

This commit is contained in:
Fabrice Ecaille
2013-09-25 18:26:19 +02:00
commit 883157e3a6
22 changed files with 3271 additions and 0 deletions

84
js/countdown.js Normal file
View File

@@ -0,0 +1,84 @@
var COUNTDOWN = {
mustStop: false,
init: function( minutes, seconds ) {
COUNTDOWN.minutes = minutes;
COUNTDOWN.seconds = seconds;
COUNTDOWN.mustStop = false;
// $( "#countdown" ).width( ( minutes > 0 ? 90 : 0 ) + 90);
// $( "#countdown" ).css( "background-color", "black" );
},
start: function() {
if( COUNTDOWN.mustStop )
return;
COUNTDOWN.running = true;
var currentMinutes = "";
var currentSeconds = "";
var imageMinutes = "";
var imageSeconds = "";
currentMinutes = COUNTDOWN.minutes;
currentSeconds = COUNTDOWN.seconds;
var nextMinutes = COUNTDOWN.minutes;
var nextSeconds = COUNTDOWN.seconds - 1;
if( nextSeconds < 0 && nextMinutes > 0 ) {
nextSeconds = 59;
nextMinutes = Math.min(0, nextMinutes -1);
}
COUNTDOWN.minutes = nextMinutes;
COUNTDOWN.seconds = nextSeconds;
if( currentMinutes <= 0 && currentSeconds < 10 )
$( "#countdown" ).css( "background-color", "red" );
if(parseInt(currentMinutes) < 10 ) currentMinutes = "0" + currentMinutes;
if(parseInt(currentSeconds) < 10 ) currentSeconds = "0" + currentSeconds;
for(i = 0; i < String(currentMinutes).length; i++) {
imageMinutes += "<div class='clock n"+ String(currentMinutes)[i]+"'></div>";
}
for(i = 0; i < String(currentSeconds).length; i++) {
imageSeconds += "<div class='clock n"+ String(currentSeconds)[i]+"'></div>";
}
if( COUNTDOWN.minutes > 0) {
$("#subMinutes").empty().removeClass( "hide" ).append( imageMinutes );;
$(".clock.clock.separator").removeClass( "hide" );
} else {
$("#subMinutes").empty().addClass( "hide" );
$(".clock.clock.separator").addClass( "hide" );
}
$("#subSeconds").empty().append( imageSeconds );
if( nextMinutes >= 0 && nextSeconds >= 0 )
setTimeout( "COUNTDOWN.start()", 1000 );
else
COUNTDOWN.callback();
},
add: function(seconds) {
console.log( "Adding " + seconds + " seconds to countdown" );
COUNTDOWN.seconds = COUNTDOWN.seconds + seconds;
},
setTime: function( seconds ) {
console.log( "Setting " + seconds + " seconds to countdown" );
COUNTDOWN.seconds = seconds;
},
stop: function() {
COUNTDOWN.mustStop = true;
},
callback: function() {
console.log( "COUNTDOWN.callback" );
}
};

71
js/lettering.js Normal file
View File

@@ -0,0 +1,71 @@
(function($) {
function injector(t, splitter, klass, after) {
var a = t.text().split(splitter);
var html = "",
clazz = "clock",
letterSize = 32,
count = 0,
width = 0,
height = 0,
lineSize,
letter, iLetter,
i, x, y
forceSmall = false;
if( typeof customClazz != "undefined" ) {
clazz = " clock " + customClazz;
}
if (a.length) {
$(a).each(
function(i, letter) {
iLetter = (letter.charCodeAt(0) - 97);
if( letter === " " ) {
html += "<span class='blank'></span>";
width += 16;
count = count + 1;
} else {
if( letter.charCodeAt(0) > 47 && letter.charCodeAt(0) < 58 ) {
letterSize = 32;
if( forceSmall ) {
letterSize = 16;
}
html += "<span class='" + clazz + (forceSmall ? "small" : "") + "' style='top: -50%;background-position: -" + ( parseInt( letter, null ) * letterSize) + "px -" + (forceSmall > -1 ? 128 : 0) +"px'></span>";
count = count + 1;
} else {
if( ( letter.charCodeAt(0) >= 'a'.charCodeAt(0) && letter.charCodeAt(0) <= 'z'.charCodeAt(0)) ) {
if( height < 16 ) {
height = 16;
}
width += 16;
lineSize = 20;
x = (iLetter % lineSize) * 16;
y = Math.floor(iLetter / lineSize) * 16 + 144;
html += "<span class='" + clazz + " small' style='background-position: -" + x + "px -" + y + "px'></span>";
count = count + 1;
} else {
if( letter.charCodeAt(0) >= 'A'.charCodeAt(0) && letter.charCodeAt(0) <= 'Z'.charCodeAt(0)) {
iLetter = letter.charCodeAt(0) - 'A'.charCodeAt(0);
if( height < 32 ) {
height = 32;
}
width += 32;
lineSize = 10;
x = (iLetter % lineSize) * 32;
y = Math.floor(iLetter / lineSize) * 32 + 32;
html += "<span class='" + clazz + "' style='background-position: -" + x + "px -" + y + "px'></span>";
count = count + 1;
}
}
}
}
});
t.empty().append(html);
}
}
$.fn.lettering = function() {
return injector( $(this), '', 'char', '' ); // always
// pass
// an
// array
};
})(jQuery);

44
js/scoreboard.js Normal file
View File

@@ -0,0 +1,44 @@
var SCOREBOARD = {
score: 0,
scoreLength: 6,
init: function(size) {
if( typeof size !== "undefined" )
SCOREBOARD.scoreLength = size;
SCOREBOARD.score = 0;
SCOREBOARD.set_score( 0 );
},
add: function(addToScore, div) {
SCOREBOARD.set_score( SCOREBOARD.score + addToScore, div);
},
set_score: function( score, div ) {
var currentScore = "";
var imageScore = "";
SCOREBOARD.score = score;
currentScore = SCOREBOARD.pad();
for(i = 0; i < String(currentScore).length; i++) {
imageScore += "<div class='clock n"+ String(currentScore)[i]+"'></div>";
}
if( typeof div === "undefined" )
div = $(".subScoreboard");
div.empty();
div.append( imageScore );
},
pad: function() {
var str = '' + SCOREBOARD.score;
while (str.length < SCOREBOARD.scoreLength) {
str = '0' + str;
}
return str;
},
callback: function() {
console.log( "SCOREBOARD.callback" );
}
};

233
js/spaceinvaders-core.js Normal file
View File

@@ -0,0 +1,233 @@
var WAVES = [
{
wave : [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ],
move : function() {
var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
this.directionX *= -1;
this.y = (this.y + this.height / 4);
}
}
},
{
wave : [ [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ],
move : function() {
var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2;
if (Math.abs((this.getOriginX() - this.getX())) >= offset) {
this.directionX *= -1;
this.y = (this.y + this.height / 4);
}
}
} ];
Game = {
running : false,
wave : -1,
aliens : [],
ship : null,
score : 0,
init : function() {
"use strict";
Game.wave = Game.wave + 1;
var row, col;
var wave = WAVES[Game.wave].wave;
for (row = 0; row < wave.length; row = row + 1) {
var aliensRow = wave[row], type = aliensRow[0], offset = (PLAYGROUND_WIDTH - ((aliensRow.length - 1) * 0.5 + aliensRow.length)
* ALIENS_WIDTH) / 2;
for (col = 0; col < aliensRow.length; col = col + 1) {
Game.setAlien(col, row, offset, type, WAVES[Game.wave].move);
}
}
SCOREBOARD.init();
SCOREBOARD.set_score( Game.score );
Game.setShip();
Game.running = true;
},
levelComplete : function() {
"use strict";
Game.running = false;
setTimeout(function() {
Game.init();
}, 3000);
},
hit : function() {
"use strict";
if( !Game.running ) {
return false;
}
console.log( "Game.hit" );
var health = Game.ship.hit();
$(".alienShot").remove();
$(".shipShot").remove();
$("#life" + Game.ship.lives).remove();
Game.running = false;
Game.ship.lives = Game.ship.lives - 1;
$("#hero").children().hide();
if( Game.ship.lives > 0 ) {
var _this = Game.ship;
setTimeout(function() {
Game.running = true;
$("#hero").children().show();
_this.health = 1;
}, 2000);
}
else {
GUI.drawText( $("#message"), game_over, true );
Game.show_game_over();
}
},
setAlien : function(x, y, offset, type, move) {
"use strict";
var id = x * ROWS + y;
var alien = new Alien("alien" + id, {
x : offset + x * ALIENS_WIDTH * 1.5,
y : START_Y + (y * 1.25 * ALIENS_HEIGHT)
}, move);
$("#actors").addSprite("alien" + id, $.extend({posx : alien.x, posy : alien.y}, animations.alien));
alien.node = $("#alien" + id);
alien.node.addClass("alien");
$("#alien" + id)[0].alien = alien;
Game.aliens.push(alien);
},
setShip : function() {
var type = SHIPS.scout;
Game.ship = new Ship("ship", {
x : $("#hero").x(),
y : $("#hero").y()
}, 3, animations.hero.ship.animation);
var hero = $("#hero");
$.each(animations.hero,
function(id, obj){
hero.addSprite(id, obj);
});
Game.ship.node = $("#hero");
},
addToScore : function( toAdd ) {
Game.score = Game.score + toAdd;
SCOREBOARD.add( toAdd );
},
control : function() {
if( !Game.running ) {
return false;
}
$(document).keyup(function(e){
switch(e.keyCode) {
case 37:
e.preventDefault();
Game.ship.left(false);
break;
case 39:
e.preventDefault();
Game.ship.right(false);
break;
}
});
$(document).keydown(function(e){
switch(e.keyCode) {
case 37:
e.preventDefault();
Game.ship.left(true);
break;
case 39:
e.preventDefault();
Game.ship.right(true);
break;
case 32:
e.preventDefault();
Game.ship.fire($("#shipShots"), "shipShot");
return false;
}
});
},
alienControl : function() {
if( !Game.running ) {
return false;
}
$.each(Game.aliens, function(index, alien ) {
alien.move();
if( alien.health > 0 && Math.random() < alien.aggression ) {
alien.fire($("#aliensShots"), "alienShot");
}
});
},
heroShotCollision : function() {
if( !Game.running ) {
return false;
}
$(".shipShot").each(function(i,e) {
var posy = $(this).y();
if( posy < -$(this).height() ) {
this.remove();
return;
}
var weapon = $(this)[0].weapon;
$(this).y(weapon.directionY * weapon.speed, true);
$(this).x(weapon.directionX * weapon.speed, true);
var collisions = $(this).collision(".alien,."+$.gQ.groupCssClass);
collisions.each( function() {
var alien = $(this)[0];
var aliensNotInArray = $.grep( Game.aliens, function( elementOfArray, index) {
return elementOfArray.id == alien.id;
}, true);
Game.aliens = aliensNotInArray;
$(this)[0].alien.hit();
})
if( collisions.length > 0 ) {
this.remove()
}
if( Game.aliens.length == 0 ) {
Game.running = false;
Game.levelComplete();
}
});
},
alienShotCollision : function() {
if( !Game.running ) {
return false;
}
$(".alienShot").each(function(i,e) {
var posy = $(this).y();
if( posy > PLAYGROUND_HEIGHT ) {
this.remove();
return;
}
var weapon = $(this)[0].weapon;
$(this).y(weapon.directionY * weapon.speed, true);
$(this).x(weapon.directionX * weapon.speed, true);
var collisions = $(this).collision("#ship,."+$.gQ.groupCssClass);
collisions.each( function() {
Game.hit();
})
if( collisions.length > 0 ) {
this.remove()
}
});
}
};

312
js/spaceinvaders-models.js Normal file
View File

@@ -0,0 +1,312 @@
/*** Weapons ***/
function Weapon() {
"use strict";
}
Weapon.prototype = {
speed : 5,
strength : 10,
rof : 300,
ror : 1500,
load : 3,
max_load : 3,
width : 5,
height : 20,
shot_timer : false,
reload_timer : false,
directionX : 0,
directionY : 1,
animation : null,
fire : function() {
if (this.shot_timer || this.load <= 0) {
return false;
}
var _this = this;
this.load = Math.max(0,this.load - 1);
this.shot_timer = setInterval(function() {
if (_this.load > 0) {
clearTimeout(_this.shot_timer);
_this.shot_timer = false;
}
}, this.rof);
if( !this.reload_timer) {
this.reload_timer = setInterval( function() {
_this.load = Math.min(_this.load + 1, _this.max_load);
if( _this.load == _this.max_load ) {
clearInterval(_this.reload_timer);
_this.reload_timer = false;
}
}, this.ror);
}
return true;
}
}
function HeroWeapon() {
"use strict";
this.directionY = -1;
}
HeroWeapon.prototype = {
}
heriter(HeroWeapon.prototype, Weapon.prototype);
function AlienWeapon() {
"use strict";
this.directionY = 1;
this.width = 5;
this.height = 10;
}
AlienWeapon.prototype = {
}
heriter(AlienWeapon.prototype, Weapon.prototype);
/*** Weapons -END ***/
/*** Actors ***/
function Actor() {
"use strict";
}
Actor.prototype = {
id : null,
node : null,
x : null,
y : null,
originX : 0,
originY : 0,
speed : null,
health : 1,
directionX : 0,
directionY : 0,
fireDirectionX : 0,
fireDirectionY : 0,
weapon : null,
animations : {},
getX : function() {
"use strict";
return this.x;
},
getY : function() {
"use strict";
return this.y;
},
move : function() {
"use strict";
if (!Game.running) {
return;
}
this.x += this.directionX * this.speed;
this.y += this.directionY * this.speed;
this.x = Math.min(this.x, PLAYGROUND_WIDTH - this.node.w());
this.x = Math.max(this.x, 0);
this.node.x(this.x);
this.node.y(this.y);
},
getOriginX : function() {
return this.originX;
},
getOriginY : function() {
return this.originY;
},
up : function(active) {
"use strict";
this.directionY = active ? -1 : 0;
},
down : function(active) {
"use strict";
this.directionY = active ? 1 : 0;
},
left : function(active) {
"use strict";
this.directionX = active ? -1 : 0;
},
right : function(active) {
"use strict";
this.directionX = active ? 1 : 0;
},
fire : function(layout, clazz) {
var name = "shot" + Math.ceil(Math.random() * 1000);
if (this.weapon.fire(layout)) {
layout.addSprite(name, $.extend({ posx : this.x + this.node.w() / 2, posy : this.y + this.fireDirectionY * this.node.h()}, this.weapon));
$("#" + name).addClass(clazz)
$("#" + name)[0].weapon = this.weapon;
}
},
hit : function() {
this.health = this.health - 1;
if( this.health == 0 ) {
this.destroy();
}
return this.health;
},
destroy : function() {
$("#" + this.id).remove();
}
};
/*** Actors - Aliens ***/
function Alien(id, start, move) {
"use strict";
this.id = id;
this.x = start.x;
this.y = start.y;
this.moveFct = move;
this.weapon = new AlienWeapon();
this.fireDirectionY = 1;
this.originX = this.x;
this.originY = this.y;
this.directionX = -1;
this.speed = 0.5;
}
Alien.prototype = {
speed : 0,
directionX : 0,
directionY : 0,
moveFct : null,
width : ALIENS_WIDTH,
height : ALIENS_HEIGHT,
aggression : 0.0005,
init : function() {
"use strict";
this.speed = 0;
this.node.x(this.x);
this.node.y(this.y);
},
move : function() {
"use strict";
this._super("move", arguments);
if (typeof this.moveFct !== undefined) {
this.moveFct();
}
},
destroy : function() {
this._super("destroy", arguments);
Game.addToScore( 5 );
}
};
heriter(Alien.prototype, Actor.prototype);
/*** Actors - Aliens - END ***/
/*** Actors - Heroes - END ***/
function Ship(id, start, speed, animation) {
"use strict";
this.id = id;
this.x = start.x;
this.y = start.y;
this.weapon = new HeroWeapon();
this.fireDirectionY = -1;
this.originX = this.x;
this.originY = this.y;
this.speed = speed;
this.animation = animation;
/*this.bigWheel = $("#bigWheel");
this.smallWheel = $("#smallWheel");
var bigWheelRadius = bigWheel.h() / 2,
smallWheelRadius = smallWheel.h() / 2;
this.bigWheelAngle = this.speed * 360 / ( 2 * Math.PI * bigWheelRadius );
this.smallWheelAngle = this.speed * 360 / ( 2 * Math.PI * smallWheelRadius );*/
}
Ship.prototype = {
speed : 0,
directionX : 0,
directionY : 0,
lives : 3,
animation : null,
/*bigWheel : null,
bigWheelAngle : 0,
smallWheel : null,
smallWheelAngle : 0,*/
init : function() {
"use strict";
this.speed = 0;
this.node.x(this.x);
this.node.y(this.y);
},
/**
* Arc = (2* Pi * R) / (360) * alpha
*
*/
move : function() {
"use strict";
this._super("move", arguments);
},
/* right : function(active) {
if( this.x + this.node.w() > PLAYGROUND_WIDTH ){
return false;
}
this._super("right", arguments);
this.bigWheel.rotate(this.bigWheelAngle, true);
this.smallWheel.rotate(this.smallWheelAngle, true);
},
left : function(active) {
if( this.x < 0 ){
return false;
}
this._super("left", arguments);
this.bigWheel.rotate(this.bigWheelAngle, true);
this.smallWheel.rotate(this.smallWheelAngle, true);
},
*/
up : function(active) {
if (this.y < (PLAYGROUND_HEIGHT - 2 * HUD_HEIGHT)) {
return false;
}
this._super("up", arguments);
},
destroy : function() {
$("#life" + this.lives).remove();
this.lives = this.lives - 1;
$("#hero").children().hide();
var _this = this;
setTimeout(function() {
$("#hero").children().show();
_this.health = 1;
}, 2000);
}
};
heriter(Ship.prototype, Actor.prototype);
/*** Actors - Heroes - END ***/
/*** Actors - END ***/

275
js/spaceinvaders-ui.js Normal file
View File

@@ -0,0 +1,275 @@
/*global jQuery, console */
var PLAYGROUND_WIDTH = 448,
PLAYGROUND_HEIGHT = 544,
REFRESH_RATE = 15,
HUD_HEIGHT = 70,
ROWS = 5,
ALIENS = 11,
ALIENS_WIDTH = 24,
ALIENS_HEIGHT = 17,
START_Y = 40,
IMAGES_PREFIX = "images/";
var SHIPS = {
scout: {width: 30, height: 25},
bomber: {width: 30, height: 25},
cruser: {width: 30, height: 25}
};
var animations = {
hero : {
ship : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png"
}),
width : 48,
height : 24,
posx : 0,
posy : 17
},
/*cockpit : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsety : 24,
offsetx : 20
}),
width : 20,
height : 33,
posx : 28,
posy : 3
},*/
smallWheel : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsetx : 0,
offsety : 24
}),
posx : 4,
posy : 30,
width: 14,
height: 14
},
bigWheel : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsetx : 0,
offsety : 38
}),
width : 19,
height : 19,
posx : 25,
posy : 27
}
},
alien : {
animation : new $.gQ.Animation({
imageURL : IMAGES_PREFIX + "invader.png",
numberOfFrame : 2,
delta : ALIENS_WIDTH,
rate : 400,
type : $.gQ.ANIMATION_HORIZONTAL
}),
width : ALIENS_WIDTH,
height : ALIENS_HEIGHT
},
ufo : {
animation: new $.gQ.Animation({imageURL: IMAGES_PREFIX + "ufo.png"}),
width: 48,
height: 32,
posy: 10
},
life : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsetx : 14,
offsety : 24
}),
width: 32,
height: 16
},
weapons : {
carot : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsetx : 19,
offsety : 37
}),
width: 19,
height: 19
},
corn : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsetx : 38,
offsety : 37
}),
width: 19,
height: 19
},
gun : {
animation : new $.gameQuery.Animation({
imageURL : IMAGES_PREFIX + "farm.png",
offsetx : 57,
offsety : 37
}),
width: 19,
height: 19
}
},
backgrounds : {
farm: {
background1 : {
animation : new $.gQ.Animation({
imageURL : IMAGES_PREFIX + "background.png"
})
},
background2 : {
animation : new $.gQ.Animation({
imageURL : IMAGES_PREFIX + "background2.png"
})
}
}
}
};
function updateWeaponLoad( load ) {
"use strict";
var HTMLDiv = $("#weapon_load");
HTMLDiv.removeClass().addClass("weapon_level");
if( load > 2/3) {
HTMLDiv.addClass("good");
} else {
if( load > 1/3) {
HTMLDiv.addClass("middle");
} else {
HTMLDiv.addClass("bad");
}
}
HTMLDiv.width( (load * 100) + "%" );
}
$(function(){
"use strict";
//Playground Sprites
$("#playground").playground({height: PLAYGROUND_HEIGHT, width: PLAYGROUND_WIDTH, keyTracker: true});
$.playground({refreshRate: 60})
.addGroup("background", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
.addSprite( "background1", {animation: animations.backgrounds.farm.background1.animation, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
.addSprite( "background2", {animation: animations.backgrounds.farm.background2.animation, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
.addSprite("ground", {posx: 0, posy : PLAYGROUND_HEIGHT - HUD_HEIGHT - 20, width: PLAYGROUND_WIDTH, height: 20})
.end()
.addGroup("actors", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
.addGroup("hero", {
width: 48,
height: 60,
posx: (PLAYGROUND_WIDTH - 48) / 2,
posy: PLAYGROUND_HEIGHT - HUD_HEIGHT - 50
})
.end()
.end()
.addGroup( "shipShots", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
.end()
.addGroup( "aliensShots", {posx: 0, posy: HUD_HEIGHT, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT - HUD_HEIGHT})
.end()
.addGroup( "hud", {width: PLAYGROUND_WIDTH, height: HUD_HEIGHT, posx: 0, posy: 0})
.addSprite( "current_weapon", $.extend({posx: 10, posy: 40}, animations.weapons.gun))
.addGroup("weapon_bar", {
posx : 50,
posy : 40,
width : 100,
height : 10
})
.addSprite("weapon_load", {
width : 100,
height : 10
})
.end()
.addGroup("scoreboard", {
posx: 10,
posy: 2,
width: 6 * 32,
height: 32
})
.addSprite( "subScoreboard", {
width: 6 * 32,
height: 32
})
.end()
.addGroup("lives", {
posx: PLAYGROUND_WIDTH - 100,
posy: 32,
width: 100,
height: 32
})
.addSprite("life3", animations.life)
.addSprite("life2", $.extend({posx: 32}, animations.life))
.addSprite("life1", $.extend({posx: 64}, animations.life))
.end()
.addGroup("levelGrp", {
posx: PLAYGROUND_WIDTH - 6 * 32 + 16,
posy: 2,
width: 32 + 5 * 16,
height: 16
})
.addSprite("levelLbl", {
width: 32 + 5 * 16
})
.addSprite("level", {
width : 64,
posx: 32 + 7 * 16,
posy: 16,
height: 16
})
.end()
.addSprite( "carot", $.extend({posx: 210, posy: 40}, animations.weapons.carot))
.addSprite( "corn", $.extend({posx: 230, posy: 40}, animations.weapons.corn))
.end()
;
$("#ground").css("background-color", "brown");
$("#levelLbl").append("Level").lettering();
$("#level").append("0").lettering();
$("#scoreboard").addClass("scoreboard");
$("#subScoreboard").addClass("subScoreboard");
var hud = $("#hud");
/*
hud.append("<div id='scoreMessage'></div>");
hud.append("<div id='message'></div>'");
hud.append("<div id='level'></div>'");
hud.append("<div id='levelNumber'></div>'");*/
// Controls
$.playground().registerCallback(Game.control, REFRESH_RATE);
$.playground().registerCallback(Game.alienControl, REFRESH_RATE);
// Collisions management
$.playground().registerCallback(Game.heroShotCollision, REFRESH_RATE);
$.playground().registerCallback(Game.alienShotCollision, REFRESH_RATE);
// Refresh playground
$.playground().registerCallback(function(){
updateWeaponLoad(Game.ship.weapon.load / Game.ship.weapon.max_load);
if( Game.running ) {
Game.ship.move();
}
}, REFRESH_RATE);
});
$(document).ready(function () {
Game.init();
$.loadCallback(function(percent){
$("#loadingBar").width(400*percent);
});
$.playground().startGame(
function() {
$("#welcomeScreen").remove();
}
);
});

24
js/tools.js Normal file
View File

@@ -0,0 +1,24 @@
function heriter(destination, source) {
function initClassIfNecessary(obj) {
if( typeof obj["_super"] == "undefined" ) {
obj["_super"] = function() {
var methodName = arguments[0];
var parameters = arguments[1];
this["__parent_methods"][methodName].apply(this, parameters);
}
}
if( typeof obj["__parent_methods"] == "undefined" ) {
obj["__parent_methods"] = {};
}
}
for (var element in source) {
if( typeof destination[element] != "undefined" ) {
initClassIfNecessary(destination);
destination["__parent_methods"][element] = source[element];
} else {
destination[element] = source[element];
}
}
}