Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Created January 4, 2012 15:11
Show Gist options
  • Save BlackScorp/1560457 to your computer and use it in GitHub Desktop.
Save BlackScorp/1560457 to your computer and use it in GitHub Desktop.
This are 2 Components for Crafty, with this Components it is possible to display collision Polygons of Enteties
//Draw Wired HitBox
Crafty.c("WiredHitBox", {
init:function(){
if (Crafty.support.canvas){
var c = document.getElementById('HitBox');
if(!c){
c = document.createElement("canvas");
c.id = 'HitBox';
c.width = Crafty.viewport.width;
c.height = Crafty.viewport.height;
c.style.position = 'absolute';
c.style.left = "0px";
c.style.top = "0px";
c.style.zIndex = '1000';
Crafty.stage.elem.appendChild(c);
}
}
this.requires("Collision").bind("EnterFrame",function(){
if(!c){
var ctx = c.getContext('2d');
ctx.beginPath();
ctx.clearRect(0, 0, Crafty.viewport.width, Crafty.viewport.height);
for(var p in this.map.points){
ctx.lineTo(Crafty.viewport.x+this.map.points[p][0],Crafty.viewport.y+this.map.points[p][1]);
}
ctx.closePath();
ctx.stroke();
}
});
return this;
}
});
//Draw Solid Hitbox
Crafty.c("SolidHitBox", {
init:function(){
//If Browser Support Canvas
if (Crafty.support.canvas){
//Find HitBox Canvas Element
var c = document.getElementById('HitBox');
if(!c){
//Create new one if not exists
c = document.createElement("canvas");
c.id = 'HitBox';
c.width = Crafty.viewport.width;
c.height = Crafty.viewport.height;
c.style.position = 'absolute';
c.style.left = "0px";
c.style.top = "0px";
c.style.zIndex = '1000';
Crafty.stage.elem.appendChild(c);
}
}
this.requires("Collision").bind("EnterFrame",function(){
if(!c){
//Draw Canvas.polygon points
var ctx = c.getContext('2d');
ctx.beginPath();
for(var p in this.map.points){
ctx.lineTo(Crafty.viewport.x+this.map.points[p][0],Crafty.viewport.y+this.map.points[p][1]);
}
ctx.closePath();
ctx.fill();
}
});
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment