{ Crafty.c( "Wall", { init: function() { this.addComponent("2D,Canvas,Color,Collision,WiredHitBox,wall,coll") .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"; 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("Thing,Fourway,Draggable,Collision,WiredHitBox,Mouse,creature,coll"); }, 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("coll", function(hb) { this.stopDrag(); 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.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(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() { Crafty("Creature").each( function() { this.disableControl(); }); }; }