From 009ae449df1be0b0701226d163c70819d963a2c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= <oli_r@fg4f.de>
Date: Wed, 24 Sep 2014 13:33:12 +0200
Subject: [PATCH] Refactured collision, using new class 'coll'

---
 src/adventure_01.js |  4 +--
 src/jsds_crafty.js  | 60 +++++++++++++++++----------------------------
 2 files changed, 24 insertions(+), 40 deletions(-)

diff --git a/src/adventure_01.js b/src/adventure_01.js
index 7eaaf26..5c55690 100644
--- a/src/adventure_01.js
+++ b/src/adventure_01.js
@@ -1,10 +1,10 @@
 {
-    start_init();
+    start_init(300,300);
 
     var wall = [];
     
     wall.push(Crafty.e("Wall").wall(240,0,10,250));
-    wall.push(Crafty.e("Wall").wall(0,240,240,10));
+    //wall.push(Crafty.e("Wall").wall(0,240,240,10));
     wall.push(Crafty.e("Wall").wall(0,0,10,240));
     wall.push(Crafty.e("Wall").wall(10,0,230,10));
     
diff --git a/src/jsds_crafty.js b/src/jsds_crafty.js
index aa6bcca..5467f73 100644
--- a/src/jsds_crafty.js
+++ b/src/jsds_crafty.js
@@ -2,7 +2,7 @@
     Crafty.c(
 	"Wall", {
 	    init: function() {
-		this.addComponent("2D,Canvas,Color,Collision,WiredHitBox,wall")
+		this.addComponent("2D,Canvas,Color,Collision,WiredHitBox,wall,coll")
 		    .color('black');
 	    },
 	    wall: function(x,y,w,h) {
@@ -32,9 +32,6 @@
 		this.h = h || 20;
 		this.col = col || "black";
 		
-		/* Might get usefull later!
-		 this.last_position={x:this.x, y:this.y};*/
-		
 		this.attr({x:this.x, y:this.y, w:this.w, h:this.h})
 		    .color(col);
 		this.old_pos["x"]=this.x;
@@ -44,52 +41,30 @@
 	}
     );
     
-    /*    Crafty.c(
-     "Creature", {
-     init: function() {
-     this.addComponent*/
-    
     Crafty.c(
 	"Creature", {
 	    init: function() {
-		this.addComponent("Thing,Fourway,Draggable,Collision,WiredHitBox,Mouse,creature");
+		this.addComponent("Thing,Fourway,Draggable,Collision,WiredHitBox,Mouse,creature,coll");
 	    },
-/*	    onhitStop: function(comp) {
-		this.stopDrag();
-		var hb;
-		while( (hb=this.hit(comp)) && (this.hit(comp)[0].normal) ) {
-		    this.x += Math.ceil(hb[0].normal.x * -hb[0].overlap);
-		    this.y += Math.ceil(hb[0].normal.y * -hb[0].overlap);
-		}
-	    },*/
-	    
 	    player: function(x,y,w,h,col) {
 		this.h = h || 40;
 		this.w = w || 40;
 		this.thing(x,y,w,h,col)
 		    .collision()
 		    .fourway(4)
-		    .onHit("wall", function() {
+		    .onHit("coll", function(hb) {
 			this.stopDrag();
-			var hb;
-			while( (hb=this.hit("wall")) && (this.hit("wall")[0].normal) ) {
-			    this.x += Math.ceil(hb[0].normal.x * -hb[0].overlap);
-			    this.y += Math.ceil(hb[0].normal.y * -hb[0].overlap);
-			}
-		    })
-		// this.onhitStop('wall')) // Function (see above) would be nicer, but doesn't work.
-		    .onHit("creature",function() {
-			this.stopDrag();
-			var hb;
-			while( (hb=this.hit("creature")) && (this.hit("creature")[0].normal) ) {
-			    this.x += Math.ceil(hb[0].normal.x * -hb[0].overlap);
-			    this.y += Math.ceil(hb[0].normal.y * -hb[0].overlap);
+			if (hb[0].normal && (! isNaN(hb[0].overlap)) ) {
+			    do {
+				var nor=hb[0].normal;
+				var ol=hb[0].overlap;
+				this.x += Math.ceil(nor.x * -ol);
+				this.y += Math.ceil(nor.y * -ol);
+			    }
+			    while( (hb=this.hit("coll")) && (this.hit("coll")[0].normal) && (this.hit("coll")[0].overlap) );
 			}
 		    });
 		
-		
-		
-		//	this.onhitStop('creature'));
 		this.bind('MouseDown', function(e) {
 		    if(e.mouseButton === Crafty.mouseButtons.LEFT) {
 			this.old_pos["x"]=this.x;
@@ -106,8 +81,17 @@
 	}
     );
     
-    var start_init = function() {
-	Crafty.init(500,250, document.getElementById('game'));
+    var start_init = function(w,h) {
+	var width=w || 500;
+	var height=h|| 400;
+	Crafty.init(width,height, document.getElementById('game'));
+
+	//Create Bounding Box
+	Crafty.e("Wall,Color").wall(0,0,width,1).color('white'); // Top
+	Crafty.e("Wall,Color").wall(0,0,1,height).color('white');
+	Crafty.e("Wall,Color").wall(0,height,width,-1).color('white'); // Bottom
+	Crafty.e("Wall,Color").wall(width,0,-1,height).color('white');
+
     };	
 
     var end_init = function() {