glasshouse/src/World.hx

66 lines
1.3 KiB
Haxe

class World {
var game:Game;
var map:hxd.res.TiledMap.TiledMapData;
public var root:h2d.Object;
var layers:Map<String, {
name:String,
data:Array<Int>,
g:h2d.TileGroup,
alpha:Float
}>;
var tiles:Array<h2d.Tile>;
public var width:Int;
public var height:Int;
public function new(r:hxd.res.TiledMap, tiles:hxd.res.Image) {
game = Game.inst;
root = new h2d.Object();
map = r.toMap();
root = new h2d.Object();
width = map.width;
height = map.height;
var t = tiles.toTile();
layers = new Map();
var font:h2d.Font = hxd.res.DefaultFont.get();
this.tiles = [
for (y in 0...Std.int(t.height) >> 5) for (x in 0...Std.int(t.width) >> 5) t.sub(x * 32, y * 32, 32, 32)
];
for (ld in map.layers) {
if (ld.name == "hotspots")
continue;
var l = {
name: ld.name,
data: ld.data,
g: new h2d.TileGroup(t, root),
alpha: ld.opacity,
}
// l.g.colorKey = 0x1D8700;
l.g.alpha = ld.opacity;
layers.set(ld.name, l);
rebuildLayer(ld.name);
}
}
function rebuildLayer(name:String) {
var l = layers.get(name);
if (l == null)
return;
var pos = 0;
var g = l.g;
g.clear();
while (g.numChildren > 0)
g.getChildAt(0).remove();
for (y in 0...height)
for (x in 0...width) {
var t = l.data[pos++] - 1;
if (t < 0)
continue;
g.add(x * 32, y * 32, tiles[t]);
}
}
}