Primary collision tests are successfull !!

This commit is contained in:
Guillaume DE LA RUE 2011-02-13 02:13:16 +01:00
parent 2c46de5ffc
commit c5bd7ecfe1
5 changed files with 102 additions and 52 deletions

View file

@ -110,6 +110,9 @@
height: 68px; height: 68px;
} }
#element_0 {
border:2px solid green;
}
/* Ennemies */ /* Ennemies */
/*#game .ennemy { border: 1px solid blue; } */ /*#game .ennemy { border: 1px solid blue; } */

View file

@ -1,5 +1,5 @@
// Init Global vars // Init Global vars
var FPS = 30; var FPS = 10;
var Stages = {} ; var Stages = {} ;
var Level = 1 ; var Level = 1 ;
var Layouts = {} ; var Layouts = {} ;
@ -72,51 +72,11 @@ app.prototype.log = function(txt) {
} }
// -- Call all canvas updates if declared as 'running' // -- Call all canvas updates if declared as 'running'
app.prototype.loopAnimation = function() { app.prototype.loopAnimation = function() {
// -- Build an active elements list
this.activeEls = [] ;
// -- Detect collisions
console.log('-------------------' ) ;
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 ;
// -- Make some clean
if (el.deleteAfter ) {
delete Layouts[i].els[j] ;
}
// -- Detect only defined types
else {
if ( type == 'ennemy' || type == 'bullet' || type == 'ship' || type == 'ship' ) {
var pos = {
x: el.x,
y: el.y,
xX: el.x + el.with,
yY: el.x + el.with
}
// -- Test if in viewport
//if ( pos )
//console.log(type, coords) ;
}
}
}
}
}
// -- Search for elements that have to be updated // -- Search for elements that have to be updated
console.log('-------------------' ) ;
for ( var i in Layouts ) { for ( var i in Layouts ) {
var _layout = Layouts[i] ; var _layout = Layouts[i] ;
if ( _layout && _layout.running ) { if ( _layout && _layout.running ) {
@ -124,13 +84,13 @@ app.prototype.loopAnimation = function() {
} }
} }
// -- Create ennemies // -- Create ennemies if needed
var numEnnemies = 0 ; var numEnnemies = 0 ;
for ( var i in Layouts.Ennemies.els ) { for ( var i in Layouts.Ennemies.els ) {
var _el = Layouts.Ennemies.els[i] ; var _el = Layouts.Ennemies.els[i] ;
if ( ! _el.deleteAfter ) numEnnemies++ ; if ( ! _el.deleteAfter ) numEnnemies++ ;
} }
for ( var i = numEnnemies-1 ; i < Level + 3 ; i++ ) { for ( var i = numEnnemies-1 ; i < Level + 0 ; i++ ) {
Layouts.Ennemies.createRandom() ; Layouts.Ennemies.createRandom() ;
} }

View file

@ -170,10 +170,6 @@ Layout.prototype.createObj = function(opts) {
this.lastSprite++ ; this.lastSprite++ ;
if ( typeof this.settings.sprites[this.lastSprite] == 'undefined' ) this.lastSprite = 0 ; 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'}) ; this.box.css({'backgroundPosition': -1*this.settings.sprites[this.lastSprite]*this.settings.width+'px 0'}) ;
//if ( this.id == 'ship' )
// console.log(this.settings.sprites, -1*this.settings.sprites[this.lastSprite]*this.settings.width+'px 0') ;
} }
// -- Move div // -- Move div
@ -189,13 +185,98 @@ Layout.prototype.createObj = function(opts) {
} ; } ;
// -- Animate the Framebuffer into the scene // -- Animate the Framebuffer into the scene
Obj.prototype.animate = function() { Obj.prototype.animate = function() {
// -- Execute custom animate function if specified
if ( $.isFunction(this.settings.animate) ) { if ( $.isFunction(this.settings.animate) ) {
this.parent = self ; this.parent = self ;
this.settings.animate(this) ; this.settings.animate(this) ;
} }
// -- Detect collision
this.detectCollision() ;
// -- Apply effects
if ( ! this.nodraw ) this.draw() ; if ( ! this.nodraw ) this.draw() ;
} } ;
// -- Detect collision
Obj.prototype.detectCollision = function() {
// -- Build an active elements list
this.activeEls = [] ;
// -- 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 ;
// -- Make some clean
if (el.deleteAfter) {
delete Layouts[i].els[j] ;
}
// -- Detect only defined types
else {
if ( type == 'ennemy' || type == 'bullet' || type == 'ship' || 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') && (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 ) {
console.log('█▬█ █ ▀█▀') ;
}
}
}
}
}
}
}
} ;
// -- Remove object // -- Remove object
Obj.prototype.deleteObj = function() { Obj.prototype.deleteObj = function() {

View file

@ -19,6 +19,7 @@ Layouts.Ennemies.createRandom = function(opts) {
var self = this ; var self = this ;
var bulletConf = { var bulletConf = {
name: 'ennemy', name: 'ennemy',
type: 'alien',
width: 60, width: 60,
height: 60, height: 60,
power: 40, power: 40,
@ -27,6 +28,9 @@ Layouts.Ennemies.createRandom = function(opts) {
speed: Math.round(Math.max(10, Math.random()*20)), speed: Math.round(Math.max(10, Math.random()*20)),
direction: 1, direction: 1,
origin: {x:Math.round(Math.random()*self.width), y:Math.round(-Math.random()*self.height)}, origin: {x:Math.round(Math.random()*self.width), y:Math.round(-Math.random()*self.height)},
explode: function(obj) {
},
animate: function (obj) { animate: function (obj) {
obj.y += obj.settings.speed*obj.settings.direction ; obj.y += obj.settings.speed*obj.settings.direction ;
if ( obj.y > obj.parent.height+obj.height ) { if ( obj.y > obj.parent.height+obj.height ) {

View file

@ -8,6 +8,7 @@ Layouts.Player = new Layout({
name: 'ship', name: 'ship',
width: 160, width: 160,
height: 160, height: 160,
type: 'human',
sprites: [0,1], sprites: [0,1],
origin: { origin: {
x: Wyrian.width/2 - 80, x: Wyrian.width/2 - 80,
@ -74,6 +75,7 @@ Layouts.Player.bulletLib = function(obj, bulletType) {
sprites: [0,1,2], sprites: [0,1,2],
speed: 40, speed: 40,
direction: -1, direction: -1,
type: 'human',
name: 'bullet', name: 'bullet',
origin: {x:0, y:0} origin: {x:0, y:0}
} ; } ;