Skip to content

Instantly share code, notes, and snippets.

@bobobo1618
Last active December 19, 2015 10:18
Show Gist options
  • Save bobobo1618/5938850 to your computer and use it in GitHub Desktop.
Save bobobo1618/5938850 to your computer and use it in GitHub Desktop.
package entities;
import com.haxepunk.Entity;
import com.haxepunk.graphics.Image;
import com.haxepunk.utils.Input;
import com.haxepunk.utils.Key;
import nape.phys.BodyType;
class Block extends Entity {
public var moveSpeed:Float;
public var napeBody:nape.phys.Body;
public var napeShape:nape.shape.Polygon;
var image:Image;
public inline static function radToDeg(rad:Float):Float
{
return 180 / Math.PI * rad;
}
public function new(x:Int, y:Int) {
moveSpeed = 1;
image = new Image("gfx/block.png");
graphic = image;
image.originX = (0.5*image.scaledWidth);
image.originY = (0.5*image.scaledHeight);
napeBody = new nape.phys.Body(BodyType.DYNAMIC);
napeShape = new nape.shape.Polygon(nape.shape.Polygon.box(image.scaledWidth, image.scaledHeight));
//napeShape.material.density = 4;
napeShape.material = nape.phys.Material.wood();
napeShape.material.dynamicFriction *= 4;
napeShape.material.density = 40;
napeBody.shapes.add(napeShape);
napeBody.position.setxy(x, y);
centerOrigin();
super(x,y);
}
public override function update() {
x = napeBody.position.x;// - (0.5*image.scaledWidth);
y = napeBody.position.y;// - (0.5*image.scaledHeight);
image.angle = -radToDeg(napeBody.rotation);
if (Input.check(Key.LEFT)){
moveBy(-2, 0);
}
if (Input.check(Key.RIGHT)){
moveBy(2, 0);
}
if (Input.check(Key.UP)){
moveBy(0, -2);
}
if (Input.check(Key.DOWN)){
moveBy(0, 2);
}
super.update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment