2011-02-12 14:55:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -- Build Background Scene
|
|
|
|
|
var Layout = function(opts) {
|
|
|
|
|
|
|
|
|
|
// -- Default settings
|
|
|
|
|
var settings = {
|
|
|
|
|
el: [],
|
|
|
|
|
dom: null
|
|
|
|
|
} ;
|
|
|
|
|
$.extend(settings, opts) ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Set running or not
|
|
|
|
|
this.running = true ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Store settings
|
2011-12-19 20:46:06 +00:00
|
|
|
|
this.settings = settings ;
|
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Declare Scene here
|
|
|
|
|
this.dom = this.settings.dom.get(0) ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Set width and height shortcuts
|
|
|
|
|
this.width = this.settings.dom.width() ;
|
|
|
|
|
this.height = this.settings.dom.height() ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Create objects in scene
|
|
|
|
|
this.els = [] ;
|
|
|
|
|
if ( typeof settings.el == 'object' ) {
|
|
|
|
|
for ( var i in settings.el ) {
|
|
|
|
|
this.els.push(this.createObj(settings.el[i])) ;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
return this;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
};
|
2011-02-12 14:55:38 +00:00
|
|
|
|
|
|
|
|
|
// -- Update elements into layout
|
|
|
|
|
Layout.prototype.update = function() {
|
|
|
|
|
for ( var j in this.els ) {
|
|
|
|
|
var _el = this.els[j] ;
|
|
|
|
|
if ( _el ) {
|
|
|
|
|
if( ! _el.deleteAfter ) {
|
|
|
|
|
_el.animate() ;
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
else {
|
|
|
|
|
_el.box.addClass('removed').remove() ;
|
2011-02-13 05:19:58 +00:00
|
|
|
|
delete this.els[j] ;
|
2011-02-12 14:55:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- Create an object
|
|
|
|
|
Layout.prototype.createObj = function(opts) {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
var self = this ;
|
|
|
|
|
|
|
|
|
|
// -- Init and build Framebuffer object
|
|
|
|
|
var Obj = function (optsObj) {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Parse options
|
|
|
|
|
var settingsObj = {
|
|
|
|
|
origin: { x: 0, y: 0},
|
|
|
|
|
imageSrc: false,
|
2011-12-19 20:46:06 +00:00
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
|
backgroundRepeat: 'no-repeat',
|
|
|
|
|
backgroundPosition: '0 0',
|
2011-02-12 14:55:38 +00:00
|
|
|
|
imageOrigin: { x:0, y:0 }
|
|
|
|
|
} ;
|
|
|
|
|
$.extend(true, settingsObj, optsObj) ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Store options
|
2011-02-12 21:24:14 +00:00
|
|
|
|
this.parent = self ;
|
2011-02-12 14:55:38 +00:00
|
|
|
|
this.name = settingsObj.name || 'default' ;
|
2011-02-13 12:28:40 +00:00
|
|
|
|
this.id = settingsObj.id || 'element_'+Game.uniqId++ ;
|
2011-02-12 14:55:38 +00:00
|
|
|
|
this.width = settingsObj.width ? settingsObj.width : self.width ;
|
|
|
|
|
this.height = settingsObj.height ? settingsObj.height : self.height ;
|
|
|
|
|
this.settings = settingsObj ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Find backgroundImage in CSS if specified
|
|
|
|
|
if ( ! settingsObj.imageSrc ) {
|
|
|
|
|
var bgImg = self.settings.dom.css('backgroundImage') ;
|
|
|
|
|
if ( bgImg != 'none' ) {
|
|
|
|
|
settingsObj.imageSrc = bgImg ;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Init position
|
|
|
|
|
this.init() ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Create a DOM object
|
2011-02-12 17:54:57 +00:00
|
|
|
|
this.box = $('#'+this.id) ;
|
2011-02-13 12:28:40 +00:00
|
|
|
|
this.dynamic = false ;
|
|
|
|
|
this.cssObj = {} ;
|
2011-02-12 17:54:57 +00:00
|
|
|
|
if ( ! this.box.length ) {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 17:54:57 +00:00
|
|
|
|
this.box = $('<div>', {
|
2011-02-13 12:52:18 +00:00
|
|
|
|
'class':'sprite '+this.name,
|
|
|
|
|
'id': this.id
|
2011-02-13 12:28:40 +00:00
|
|
|
|
}) ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 12:28:40 +00:00
|
|
|
|
this.cssObj = {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
position: 'absolute',
|
2011-02-13 12:28:40 +00:00
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
display: 'block',
|
2011-12-19 20:46:06 +00:00
|
|
|
|
backgroundColor: settingsObj.backgroundColor,
|
2011-02-13 12:28:40 +00:00
|
|
|
|
backgroundRepeat: settingsObj.backgroundRepeat,
|
|
|
|
|
backgroundPosition: settingsObj.backgroundPosition,
|
|
|
|
|
backgroundImage: settingsObj.imageSrc ? 'url('+settingsObj.imageSrc+')' : ''
|
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 12:28:40 +00:00
|
|
|
|
this.dynamic = true ;
|
2011-02-12 17:54:57 +00:00
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 17:54:57 +00:00
|
|
|
|
// -- Apply CSS
|
2011-02-13 12:28:40 +00:00
|
|
|
|
this.cssObj.width = this.width ;
|
|
|
|
|
this.cssObj.height = this.height ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 21:24:14 +00:00
|
|
|
|
// -- Move
|
|
|
|
|
if ( ! this.settings.nomove ) {
|
2011-02-13 12:28:40 +00:00
|
|
|
|
this.cssObj.translate = this.x+'px, '+this.y+'px' ;
|
2011-02-12 21:24:14 +00:00
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 12:28:40 +00:00
|
|
|
|
// -- Apply CSS Append and display
|
|
|
|
|
this.box.css(this.cssObj) ;
|
|
|
|
|
if ( this.dynamic ) this.box.appendTo(self.dom) ;
|
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
|
|
|
|
// -- Init
|
2011-02-12 14:55:38 +00:00
|
|
|
|
Obj.prototype.init = function() {
|
|
|
|
|
this.x = this.settings.origin.x ;
|
|
|
|
|
this.y = this.settings.origin.y ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
} ;
|
|
|
|
|
|
2011-02-12 21:24:14 +00:00
|
|
|
|
// -- Return instance
|
|
|
|
|
Obj.prototype.getInstance = function() {
|
|
|
|
|
return this;
|
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Draw object into scene
|
|
|
|
|
Obj.prototype.draw = function() {
|
2011-02-13 12:28:40 +00:00
|
|
|
|
if ( (this.y >= -2*this.height) && (this.y <= (Game.height+this.height)) && this.x >= -this.width && this.x <= (Game.width+this.width) ) {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 12:28:40 +00:00
|
|
|
|
Game.activeElements++ ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Set sprite to display
|
|
|
|
|
if ( this.settings.sprites ) {
|
|
|
|
|
this.lastSprite = this.lastSprite || 0 ;
|
2011-02-13 12:28:40 +00:00
|
|
|
|
if ( (Game.loops%(this.settings.spriteMod||1)) == 0 ) this.lastSprite++ ;
|
2011-02-12 21:24:14 +00:00
|
|
|
|
if ( typeof this.settings.sprites[this.lastSprite] == 'undefined' ) this.lastSprite = 0 ;
|
|
|
|
|
this.box.css({'backgroundPosition': -1*this.settings.sprites[this.lastSprite]*this.settings.width+'px 0'}) ;
|
2011-02-12 14:55:38 +00:00
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Move div
|
2011-02-12 21:24:14 +00:00
|
|
|
|
if ( ! this.settings.nomove ) {
|
|
|
|
|
if ( this.settings.moveParent ) {
|
|
|
|
|
this.box.parent().css({'translate': this.x+'px, '+this.y+'px'}) ;
|
|
|
|
|
} else {
|
2011-02-13 05:19:58 +00:00
|
|
|
|
this.box.css({'translate': this.x+'px, '+this.y+'px', display:'block'}) ;
|
2011-02-12 21:24:14 +00:00
|
|
|
|
}
|
2011-02-12 17:54:57 +00:00
|
|
|
|
}
|
2011-02-12 14:55:38 +00:00
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Animate the Framebuffer into the scene
|
2011-12-19 20:46:06 +00:00
|
|
|
|
Obj.prototype.animate = function() {
|
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Execute custom animate function if specified
|
2011-02-12 14:55:38 +00:00
|
|
|
|
if ( $.isFunction(this.settings.animate) ) {
|
|
|
|
|
this.parent = self ;
|
|
|
|
|
this.settings.animate(this) ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Detect collision
|
|
|
|
|
this.detectCollision() ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Apply effects
|
2011-02-12 21:24:14 +00:00
|
|
|
|
if ( ! this.nodraw ) this.draw() ;
|
2011-02-13 01:13:16 +00:00
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Detect collision
|
|
|
|
|
Obj.prototype.detectCollision = function() {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 05:19:58 +00:00
|
|
|
|
var obj = self ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Detect collisions
|
|
|
|
|
for ( var i in Layouts ) {
|
|
|
|
|
var _layout = Layouts[i] ;
|
|
|
|
|
if ( _layout && _layout.running ) {
|
|
|
|
|
for ( var j in _layout.els ) {
|
|
|
|
|
var el = _layout.els[j],
|
|
|
|
|
type = el.name ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Detect only defined types
|
2011-12-19 20:46:06 +00:00
|
|
|
|
if (! el.deleteAfter && ! el.explosing && type != 'neutral' ) {
|
|
|
|
|
|
2011-02-13 05:19:58 +00:00
|
|
|
|
if ( (type == 'ennemy' || type == 'bullet' || type == 'ship') ) {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
var A = {
|
|
|
|
|
x: this.x,
|
|
|
|
|
y: this.y,
|
|
|
|
|
xX: this.x+this.width,
|
|
|
|
|
yY: this.y+this.height
|
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
var B = {
|
|
|
|
|
x: el.x,
|
|
|
|
|
y: el.y,
|
|
|
|
|
xX: el.x + el.width,
|
2011-12-19 20:46:06 +00:00
|
|
|
|
yY: el.y + el.height
|
2011-02-13 01:13:16 +00:00
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
// -- Test if in viewport
|
2011-02-13 07:16:43 +00:00
|
|
|
|
if ( (type != this.name) && (this.name != 'default' && this.name != 'explosion') && (el.settings.type != this.settings.type) ) {
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
|
|
|
|
var touchTopRight = (
|
|
|
|
|
( B.x <= A.xX && B.x >= A.x )
|
|
|
|
|
&&
|
|
|
|
|
( B.yY >= A.y) && ( B.yY <= A.yY )
|
|
|
|
|
) ? true : false ;
|
|
|
|
|
|
|
|
|
|
var touchTopLeft = (
|
|
|
|
|
( B.x >= A.x && B.x <= A.xX )
|
|
|
|
|
&&
|
|
|
|
|
( B.yY >= A.y) && ( B.yY <= A.yY )
|
2011-02-13 01:13:16 +00:00
|
|
|
|
) ? true : false ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
|
|
|
|
var touchBottomRight = (
|
|
|
|
|
( B.x >= A.x && B.x <= A.xX )
|
|
|
|
|
&&
|
|
|
|
|
( B.y >= A.yY) && ( B.y <= A.y )
|
2011-02-13 01:13:16 +00:00
|
|
|
|
) ? true : false ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
|
|
|
|
var touchBottomLeft = (
|
|
|
|
|
( B.xX >= A.x && B.xX <= A.xX )
|
|
|
|
|
&&
|
|
|
|
|
( B.y >= A.yY) && ( B.y <= A.y )
|
2011-02-13 01:13:16 +00:00
|
|
|
|
) ? true : false ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
if ( touchTopRight || touchTopLeft || touchBottomRight || touchBottomLeft ) {
|
2011-02-13 05:19:58 +00:00
|
|
|
|
if ( typeof el.settings.explode == 'function' ) {
|
|
|
|
|
el.settings.explode(el) ;
|
|
|
|
|
}
|
|
|
|
|
if ( typeof this.settings.explode == 'function' ) {
|
|
|
|
|
this.settings.explode(this) ;
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 12:28:40 +00:00
|
|
|
|
Game.log('█▬█ █ ▀█▀') ;
|
2011-02-13 01:13:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
|
|
|
|
|
2011-02-13 01:13:16 +00:00
|
|
|
|
} ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
// -- Remove object
|
|
|
|
|
Obj.prototype.deleteObj = function() {
|
|
|
|
|
this.deleteAfter = true ;
|
|
|
|
|
}
|
2011-12-19 20:46:06 +00:00
|
|
|
|
|
2011-02-12 14:55:38 +00:00
|
|
|
|
return new Obj(opts) ;
|
2011-12-19 20:46:06 +00:00
|
|
|
|
};
|