{ Crafty.c( "Wall", { init: function() { this.addComponent("2D,Canvas,Color,Collision,WiredHitBox,wall") .color('black'); }, wall: function(x,y,w,h) { this.x = x*1 || 0; this.y = y*1 || 0; this.w = w*1 || 0; this.h = h*1 || 0; this.attr({x:this.x, y:this.y, w:this.w, h:this.h}) .collision(); return this; } } ); Crafty.c( "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; this.y = y || 0; this.w = w || 20; 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; this.old_pos["y"]=this.y; return this; } } ); /* Crafty.c( "Creature", { init: function() { this.addComponent*/ Crafty.c( "Creature", { init: function() { this.addComponent("Thing,Fourway,Draggable,Collision,WiredHitBox,Mouse,creature"); }, /* 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() { 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); } }); // this.onhitStop('creature')); this.bind('MouseDown', function(e) { if(e.mouseButton === Crafty.mouseButtons.LEFT) { this.old_pos["x"]=this.x; this.old_pos["y"]=this.y; }; Crafty("Creature").each( function() { this.disableControl(); }); this.enableControl(); }); return this; } } ); var start_init = function() { Crafty.init(500,250, document.getElementById('game')); }; var end_init = function() { Crafty("Creature").each( function() { this.disableControl(); }); }; }