Refactured collision, using new class 'coll'
This commit is contained in:
parent
51186cfb30
commit
009ae449df
2 changed files with 24 additions and 40 deletions
|
@ -1,10 +1,10 @@
|
||||||
{
|
{
|
||||||
start_init();
|
start_init(300,300);
|
||||||
|
|
||||||
var wall = [];
|
var wall = [];
|
||||||
|
|
||||||
wall.push(Crafty.e("Wall").wall(240,0,10,250));
|
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(0,0,10,240));
|
||||||
wall.push(Crafty.e("Wall").wall(10,0,230,10));
|
wall.push(Crafty.e("Wall").wall(10,0,230,10));
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
Crafty.c(
|
Crafty.c(
|
||||||
"Wall", {
|
"Wall", {
|
||||||
init: function() {
|
init: function() {
|
||||||
this.addComponent("2D,Canvas,Color,Collision,WiredHitBox,wall")
|
this.addComponent("2D,Canvas,Color,Collision,WiredHitBox,wall,coll")
|
||||||
.color('black');
|
.color('black');
|
||||||
},
|
},
|
||||||
wall: function(x,y,w,h) {
|
wall: function(x,y,w,h) {
|
||||||
|
@ -32,9 +32,6 @@
|
||||||
this.h = h || 20;
|
this.h = h || 20;
|
||||||
this.col = col || "black";
|
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})
|
this.attr({x:this.x, y:this.y, w:this.w, h:this.h})
|
||||||
.color(col);
|
.color(col);
|
||||||
this.old_pos["x"]=this.x;
|
this.old_pos["x"]=this.x;
|
||||||
|
@ -44,52 +41,30 @@
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/* Crafty.c(
|
|
||||||
"Creature", {
|
|
||||||
init: function() {
|
|
||||||
this.addComponent*/
|
|
||||||
|
|
||||||
Crafty.c(
|
Crafty.c(
|
||||||
"Creature", {
|
"Creature", {
|
||||||
init: function() {
|
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) {
|
player: function(x,y,w,h,col) {
|
||||||
this.h = h || 40;
|
this.h = h || 40;
|
||||||
this.w = w || 40;
|
this.w = w || 40;
|
||||||
this.thing(x,y,w,h,col)
|
this.thing(x,y,w,h,col)
|
||||||
.collision()
|
.collision()
|
||||||
.fourway(4)
|
.fourway(4)
|
||||||
.onHit("wall", function() {
|
.onHit("coll", function(hb) {
|
||||||
this.stopDrag();
|
this.stopDrag();
|
||||||
var hb;
|
if (hb[0].normal && (! isNaN(hb[0].overlap)) ) {
|
||||||
while( (hb=this.hit("wall")) && (this.hit("wall")[0].normal) ) {
|
do {
|
||||||
this.x += Math.ceil(hb[0].normal.x * -hb[0].overlap);
|
var nor=hb[0].normal;
|
||||||
this.y += Math.ceil(hb[0].normal.y * -hb[0].overlap);
|
var ol=hb[0].overlap;
|
||||||
}
|
this.x += Math.ceil(nor.x * -ol);
|
||||||
})
|
this.y += Math.ceil(nor.y * -ol);
|
||||||
// this.onhitStop('wall')) // Function (see above) would be nicer, but doesn't work.
|
}
|
||||||
.onHit("creature",function() {
|
while( (hb=this.hit("coll")) && (this.hit("coll")[0].normal) && (this.hit("coll")[0].overlap) );
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// this.onhitStop('creature'));
|
|
||||||
this.bind('MouseDown', function(e) {
|
this.bind('MouseDown', function(e) {
|
||||||
if(e.mouseButton === Crafty.mouseButtons.LEFT) {
|
if(e.mouseButton === Crafty.mouseButtons.LEFT) {
|
||||||
this.old_pos["x"]=this.x;
|
this.old_pos["x"]=this.x;
|
||||||
|
@ -106,8 +81,17 @@
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
var start_init = function() {
|
var start_init = function(w,h) {
|
||||||
Crafty.init(500,250, document.getElementById('game'));
|
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() {
|
var end_init = function() {
|
||||||
|
|
Loading…
Reference in a new issue