From c5bd7ecfe1fbae95ea0112fa3f644b5bfd665af6 Mon Sep 17 00:00:00 2001
From: Guillaume DE LA RUE <delarueguillaume@gmail.com>
Date: Sun, 13 Feb 2011 02:13:16 +0100
Subject: [PATCH] Primary collision tests are successfull !!

---
 guiltouf/css/style.css             |  3 +
 guiltouf/js/GameClass.js           | 52 ++---------------
 guiltouf/js/LayoutClass.js         | 93 ++++++++++++++++++++++++++++--
 guiltouf/js/layouts/Ennemies.js    |  4 ++
 guiltouf/js/layouts/PlayerLayer.js |  2 +
 5 files changed, 102 insertions(+), 52 deletions(-)

diff --git a/guiltouf/css/style.css b/guiltouf/css/style.css
index 9b5fb7c..87a9996 100644
--- a/guiltouf/css/style.css
+++ b/guiltouf/css/style.css
@@ -110,6 +110,9 @@
 	height: 68px;
 }
 
+#element_0 {
+	border:2px solid green;
+}
 /* Ennemies */
 /*#game .ennemy { border: 1px solid blue; } */
 
diff --git a/guiltouf/js/GameClass.js b/guiltouf/js/GameClass.js
index a576af3..24e76cb 100644
--- a/guiltouf/js/GameClass.js
+++ b/guiltouf/js/GameClass.js
@@ -1,5 +1,5 @@
 // Init Global vars
-var FPS = 30;
+var FPS = 10;
 var Stages = {} ;
 var Level = 1 ;
 var Layouts = {} ;
@@ -72,51 +72,11 @@ app.prototype.log = function(txt) {
 }
 
 // -- Call all canvas updates if declared as 'running'
-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) ;
-						
-						
-						
-						
-					}
-				}
-			}
-		}
-	}
-	
+app.prototype.loopAnimation = function() {	
 	
 	// -- Search for elements that have to be updated
+	
+    	console.log('-------------------' ) ;
 	for ( var i in Layouts ) {
 		var _layout = Layouts[i] ;
 		if ( _layout && _layout.running ) {
@@ -124,13 +84,13 @@ app.prototype.loopAnimation = function() {
 		}
 	}
 	
-	// -- Create ennemies
+	// -- Create ennemies if needed
 	var numEnnemies = 0 ;
 	for ( var i in Layouts.Ennemies.els ) {
 		var _el = Layouts.Ennemies.els[i] ;
 		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() ;
 	}
 	
diff --git a/guiltouf/js/LayoutClass.js b/guiltouf/js/LayoutClass.js
index f87beef..b336bed 100644
--- a/guiltouf/js/LayoutClass.js
+++ b/guiltouf/js/LayoutClass.js
@@ -170,10 +170,6 @@ Layout.prototype.createObj = function(opts) {
 				this.lastSprite++ ;
 				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'}) ;
-				
-				
-				//if ( this.id == 'ship' ) 
-			//		console.log(this.settings.sprites,  -1*this.settings.sprites[this.lastSprite]*this.settings.width+'px 0') ;
 			}
 			
 			// -- Move div
@@ -189,13 +185,98 @@ Layout.prototype.createObj = function(opts) {
 	} ;
 	
 	// -- 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) ) {
 			this.parent = self ;
 			this.settings.animate(this) ;
 		} 
+		
+		// -- Detect collision
+		this.detectCollision() ;
+		
+		// -- Apply effects
 		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
 	Obj.prototype.deleteObj = function() {
diff --git a/guiltouf/js/layouts/Ennemies.js b/guiltouf/js/layouts/Ennemies.js
index ab235c9..438e549 100644
--- a/guiltouf/js/layouts/Ennemies.js
+++ b/guiltouf/js/layouts/Ennemies.js
@@ -19,6 +19,7 @@ Layouts.Ennemies.createRandom = function(opts) {
 	var self = this ;
 	var bulletConf = {
 		name: 'ennemy',
+		type: 'alien',
 		width: 60,
 		height: 60,
 		power: 40,
@@ -27,6 +28,9 @@ Layouts.Ennemies.createRandom = function(opts) {
 		speed:  Math.round(Math.max(10, Math.random()*20)),
 		direction: 1,
 		origin: {x:Math.round(Math.random()*self.width), y:Math.round(-Math.random()*self.height)},
+		explode: function(obj) {
+		
+		},
 		animate: function (obj) {
 	     	obj.y += obj.settings.speed*obj.settings.direction ;
 	    	if ( obj.y > obj.parent.height+obj.height ) {
diff --git a/guiltouf/js/layouts/PlayerLayer.js b/guiltouf/js/layouts/PlayerLayer.js
index 2899257..7b6f4d9 100644
--- a/guiltouf/js/layouts/PlayerLayer.js
+++ b/guiltouf/js/layouts/PlayerLayer.js
@@ -8,6 +8,7 @@ Layouts.Player = new Layout({
 		name: 'ship',
 		width: 160,
 		height: 160,
+		type: 'human',
 		sprites: [0,1],
 		origin: {
 			x: Wyrian.width/2 - 80,
@@ -74,6 +75,7 @@ Layouts.Player.bulletLib = function(obj, bulletType) {
 		sprites: [0,1,2],
 		speed:  40,
 		direction: -1,
+		type: 'human',
 		name: 'bullet',
 		origin: {x:0, y:0}
 	} ;