wyrian/js/layouts/LayoutClass.js

271 lines
6.5 KiB
JavaScript
Raw Normal View History

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-02-12 14:55:38 +00:00
// -- Set running or not
this.running = true ;
2011-02-12 14:55:38 +00:00
// -- Store settings
this.settings = settings ;
2011-02-12 14:55:38 +00:00
// -- Declare Scene here
this.dom = this.settings.dom.get(0) ;
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-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-02-12 14:55:38 +00:00
return this;
};
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() ;
}
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-02-12 14:55:38 +00:00
var self = this ;
// -- Init and build Framebuffer object
var Obj = function (optsObj) {
2011-02-12 14:55:38 +00:00
// -- Parse options
var settingsObj = {
origin: { x: 0, y: 0},
imageSrc: false,
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-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' ;
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-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-02-12 14:55:38 +00:00
// -- Init position
this.init() ;
2011-02-12 14:55:38 +00:00
// -- Create a DOM object
2011-02-12 17:54:57 +00:00
this.box = $('#'+this.id) ;
this.dynamic = false ;
this.cssObj = {} ;
2011-02-12 17:54:57 +00:00
if ( ! this.box.length ) {
2011-02-12 17:54:57 +00:00
this.box = $('<div>', {
'class':'sprite '+this.name,
'id': this.id
}) ;
this.cssObj = {
position: 'absolute',
top: 0,
left: 0,
display: 'block',
backgroundColor: settingsObj.backgroundColor,
backgroundRepeat: settingsObj.backgroundRepeat,
backgroundPosition: settingsObj.backgroundPosition,
backgroundImage: settingsObj.imageSrc ? 'url('+settingsObj.imageSrc+')' : ''
} ;
this.dynamic = true ;
2011-02-12 17:54:57 +00:00
}
2011-02-12 17:54:57 +00:00
// -- Apply CSS
this.cssObj.width = this.width ;
this.cssObj.height = this.height ;
2011-02-12 21:24:14 +00:00
// -- Move
if ( ! this.settings.nomove ) {
this.cssObj.translate = this.x+'px, '+this.y+'px' ;
2011-02-12 21:24:14 +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
} ;
// -- 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-02-12 21:24:14 +00:00
// -- Return instance
Obj.prototype.getInstance = function() {
return this;
} ;
2011-02-12 14:55:38 +00:00
// -- Draw object into scene
Obj.prototype.draw = function() {
if ( (this.y >= -2*this.height) && (this.y <= (Game.height+this.height)) && this.x >= -this.width && this.x <= (Game.width+this.width) ) {
Game.activeElements++ ;
2011-02-12 14:55:38 +00:00
// -- Set sprite to display
if ( this.settings.sprites ) {
this.lastSprite = this.lastSprite || 0 ;
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-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-02-12 14:55:38 +00:00
} ;
2011-02-12 14:55:38 +00:00
// -- Animate the Framebuffer into the scene
Obj.prototype.animate = function() {
// -- 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) ;
}
// -- Detect collision
this.detectCollision() ;
// -- Apply effects
2011-02-12 21:24:14 +00:00
if ( ! this.nodraw ) this.draw() ;
} ;
// -- Detect collision
Obj.prototype.detectCollision = function() {
2011-02-13 05:19:58 +00:00
var obj = self ;
// -- 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 ;
// -- Detect only defined types
if (! el.deleteAfter && ! el.explosing && type != 'neutral' ) {
2011-02-13 05:19:58 +00:00
if ( (type == 'ennemy' || type == 'bullet' || type == 'ship') ) {
var A = {
x: this.x,
y: this.y,
xX: this.x+this.width,
yY: this.y+this.height
} ;
var B = {
x: el.x,
y: el.y,
xX: el.x + el.width,
yY: el.y + el.height
} ;
// -- Test if in viewport
if ( (type != this.name) && (this.name != 'default' && this.name != 'explosion') && (el.settings.type != this.settings.type) ) {
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 )
) ? true : false ;
var touchBottomRight = (
( B.x >= A.x && B.x <= A.xX )
&&
( B.y >= A.yY) && ( B.y <= A.y )
) ? true : false ;
var touchBottomLeft = (
( B.xX >= A.x && B.xX <= A.xX )
&&
( B.y >= A.yY) && ( B.y <= A.y )
) ? true : false ;
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) ;
}
Game.log('█▬█ █ ▀█▀') ;
}
}
}
}
}
}
}
} ;
2011-02-12 14:55:38 +00:00
// -- Remove object
Obj.prototype.deleteObj = function() {
this.deleteAfter = true ;
}
2011-02-12 14:55:38 +00:00
return new Obj(opts) ;
};