Last active
January 2, 2018 08:54
-
-
Save markknol/15d64dc1122f4f4a85f297c29925d30a to your computer and use it in GitHub Desktop.
Heaps Nape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game; | |
import h2d.Bitmap; | |
import h2d.Sprite; | |
import hxd.Res; | |
import nape.geom.Vec2; | |
import nape.phys.Body; | |
import nape.phys.BodyType; | |
import nape.shape.Polygon; | |
import nape.space.Space; | |
/** | |
* @author Mark Knol | |
*/ | |
class Main extends hxd.App | |
{ | |
private static var SIZE = { WIDTH: 1300, HEIGHT: 650 }; | |
private var space:Space = new Space(Vec2.weak(0, 600)); | |
private var elements:Array<{ sprite:Sprite, body:Body}>; | |
static function main() | |
{ | |
#if hl | |
hxd.res.Resource.LIVE_UPDATE = true; // if you want to detect something logical has changed you can use `myResource.entry.watch(function() trace("Changed!"))` | |
Res.initLocal(); // < no resource embedding, should compile faster | |
#else | |
Res.initEmbed(); | |
#end | |
new Main(); | |
} | |
override private function setup() | |
{ | |
engine.backgroundColor = 0xFFFFFF; | |
hxd.Stage.getInstance().resize(SIZE.WIDTH, SIZE.HEIGHT); | |
super.setup(); | |
} | |
override private function init() | |
{ | |
super.init(); | |
// invisible floor (physics only) | |
var floor = new Body(BodyType.STATIC); | |
floor.shapes.add(new Polygon(Polygon.box(SIZE.WIDTH * 4, 40))); | |
floor.position.setxy(-SIZE.WIDTH, SIZE.HEIGHT); | |
floor.space = space; | |
//add some swords | |
elements = [for (i in 0...20) | |
{ | |
// add bitmap to s2d | |
var bmp = new Bitmap(Res.sword.toTile(), s2d); | |
bmp.tile.setCenterRatio(); | |
bmp.setPos(Math.random() * SIZE.WIDTH, 0); | |
// add body with same size/position as bmp | |
var body = new Body(BodyType.DYNAMIC); | |
body.shapes.add(new Polygon(Polygon.box(bmp.tile.width, bmp.tile.height))); | |
body.position.setxy(bmp.x, bmp.y); | |
body.rotation = Math.random() * Math.PI*2; | |
body.space = space; | |
{ | |
sprite: bmp, | |
body: body, | |
} | |
}]; | |
} | |
override private function update(dt:Float) | |
{ | |
super.update(dt); | |
if (hxd.Timer.deltaT <= 0) return; // skip frame | |
// update physics first | |
space.step(hxd.Timer.deltaT); | |
// update visuals second (visuals reflect physics) | |
for (element in elements) | |
{ | |
var body = element.body; | |
var bodyPos = body.position; | |
element.sprite.setPos(bodyPos.x, bodyPos.y); | |
element.sprite.rotation = body.rotation; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment