Skip to content

Commit

Permalink
add json map and render it
Browse files Browse the repository at this point in the history
  • Loading branch information
afaur committed Jul 10, 2024
1 parent fe9aacb commit 988755d
Show file tree
Hide file tree
Showing 6 changed files with 5,662 additions and 2,123 deletions.
44 changes: 44 additions & 0 deletions Main.hx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import hxd.res.TiledMap;

class Main extends hxd.App {
var wizard: h2d.Object;
var timer: Float = 0.0;
Expand All @@ -17,12 +19,54 @@ class Main extends hxd.App {
myscene.addChild(toSprite(rlTiles[DirtPathRL.Middle], 0, 16));
myscene.addChild(toSprite(rlTiles[DirtPathRL.Bottom], 0, 32));

var mapJson = hxd.Res.dungeon_crawler_v2_tmj.entry.getText();

// parse Tiled json file
var mapData: TiledMapData = haxe.Json.parse(mapJson);

// get tile image (tiles.png) from resources
var tileImage = hxd.Res.tinydungeon.toTile();

// create a TileGroup for fast tile rendering, attach to 2d scene
var group = new h2d.TileGroup(tileImage, myscene);

var tw = 16;
var th = 16;

var mw = mapData.width;
var mh = mapData.height;

var margin = 1;

// make sub tiles from tile
var tiles = [
for(y in 0 ... Std.int((tileImage.height + margin) / (th + margin)))
for(x in 0 ... Std.int((tileImage.width + margin) / (tw + margin)))
tileImage.sub(x * (tw + margin), y * (th + margin), tw, th)
];

// iterate on all layers
for(layer in mapData.layers) {
// iterate on x and y
for(y in 0 ... mh) for (x in 0 ... mw) {
if (layer?.data == null) continue;
// get the tile id at the current position
var tid = layer.data[x + y * mw];
if (tid != 0) { // skip transparent tiles
// add a tile to the TileGroup
group.add(x * tw, y * th, tiles[tid - 1]);
}
}
}

/*
var tdTiles = tinydungeonTiles();
wizard = toSprite(tdTiles[CharacterTD.Wizard]);
myscene.addChild(wizard);
myscene.addChild(toSprite(tdTiles[CharacterTD.VillagerOne], 16, 16));
*/
}

override private function update(dt: Float) {
Expand Down
4 changes: 0 additions & 4 deletions Util.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ enum abstract DirtPathRL (Int) to Int {
var Bottom = 182;
}

enum abstract DirectionsPermissable (Int) to Int {
var 153 = 16;
}

// tinydungeon
enum abstract CharacterTD (Int) to Int {
var Wizard = 84;
Expand Down
Loading

0 comments on commit 988755d

Please sign in to comment.