From 16a58b0fb22b6578616fd1de0af9dc2a83e2f535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Tue, 23 Sep 2014 15:36:44 +0200 Subject: [PATCH] Finished Collision-Detection Player - Wall --- TODO | 1 - src/jsds.js | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 22224b6..dae3cc4 100644 --- a/TODO +++ b/TODO @@ -3,4 +3,3 @@ ToDo for JSDSH: - Implement Player, Creatures - Background and Wall Editing/Importing - Shoot, Attack - - Repair Hit-Action. diff --git a/src/jsds.js b/src/jsds.js index 0ee5921..69a3a6f 100644 --- a/src/jsds.js +++ b/src/jsds.js @@ -25,6 +25,7 @@ "Thing", { init: function() { this.addComponent("2D,Canvas,Color,thing"); + this.old_pos={"x":0, "y":0}; }, thing: function(x,y,w,h,col) { this.x = x || 0; @@ -38,6 +39,8 @@ this.attr({x:this.x, y:this.y, w:this.w, h:this.h}) .color(col); + this.old_pos["x"]=this.x; + this.old_pos["y"]=this.y; return this; } } @@ -51,7 +54,8 @@ Crafty.c( "Player", { init: function() { - this.addComponent("Thing,Fourway,Draggable,Collision,WiredHitBox,player");}, + this.addComponent("Thing,Fourway,Draggable,Collision,WiredHitBox,Mouse,player"); + }, player: function(x,y,w,h,col) { this.h = h || 40; this.w = w || 40; @@ -60,10 +64,21 @@ .fourway(4) .onHit("wall",function() { 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.bind('MouseDown', function(e) { + if(e.mouseButton === Crafty.mouseButtons.LEFT) { + this.old_pos["x"]=this.x; + this.old_pos["y"]=this.y; + console.log("New Pos saved: "+this.old_pos["x"]+' '+this.old_pos["y"]); + }; + }); return this; - } } );