Finished Collision-Detection Player - Wall
This commit is contained in:
parent
5cfa97d071
commit
16a58b0fb2
2 changed files with 17 additions and 3 deletions
1
TODO
1
TODO
|
@ -3,4 +3,3 @@ ToDo for JSDSH:
|
|||
- Implement Player, Creatures
|
||||
- Background and Wall Editing/Importing
|
||||
- Shoot, Attack
|
||||
- Repair Hit-Action.
|
||||
|
|
19
src/jsds.js
19
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;
|
||||
|
||||
}
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue