From 882c950258c48f190833cc4502e1ded5627843b0 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Tue, 18 Jun 2024 16:19:43 -0500 Subject: [PATCH 1/4] add TypedTileset --- src/ldtk/Tileset.hx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ldtk/Tileset.hx b/src/ldtk/Tileset.hx index c741840..f33120e 100644 --- a/src/ldtk/Tileset.hx +++ b/src/ldtk/Tileset.hx @@ -1,6 +1,8 @@ package ldtk; -class Tileset { +typedef Tileset = TypedTileset; + +class TypedTileset { var untypedProject: ldtk.Project; /** Original parsed JSON object **/ @@ -85,6 +87,16 @@ class Tileset { return Std.int( (pixelX-padding) / tileGridSize ) + cWid * Std.int( pixelY / tileGridSize ); } + /** Return TRUE if the specifiied tile ID was tagged with given enum `tag`. **/ + public inline function hasTag(tileId:Int, tag:TTag) { + final allTileIds = untypedTags.get( tag.getName() ); + return allTileIds==null ? false : allTileIds.exists(tileId); + } + + /** Return an array of all tags associated with give tile ID. WARNING: this allocates a new array on call. **/ + public function getAllTags(tileId:Int) : Array { + throw "getAllTags not implemented"; + } /*************************************************************************** HEAPS API From c5fec34e48b09a09858011d36654ddfb96e25db5 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Tue, 18 Jun 2024 16:38:13 -0500 Subject: [PATCH 2/4] add TypedLayer_Tiles --- src/ldtk/Layer_Tiles.hx | 10 +++++-- src/ldtk/macro/TypeBuilder.hx | 55 ++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/ldtk/Layer_Tiles.hx b/src/ldtk/Layer_Tiles.hx index a698bc5..d36be52 100644 --- a/src/ldtk/Layer_Tiles.hx +++ b/src/ldtk/Layer_Tiles.hx @@ -1,11 +1,15 @@ package ldtk; -class Layer_Tiles extends ldtk.Layer { +import ldtk.Tileset; + +typedef Layer_Tiles = TypedLayer_Tiles; + +class TypedLayer_Tiles extends ldtk.Layer { var tiles : Map>; /** Getter to layer untyped Tileset instance. The typed value is created in macro. **/ - var untypedTileset(get,never) : ldtk.Tileset; - inline function get_untypedTileset() return untypedProject._untypedTilesets.get(tilesetUid); + var untypedTileset(get,never) : TypedTileset; + inline function get_untypedTileset() return cast untypedProject._untypedTilesets.get(tilesetUid); /** Tileset UID **/ public var tilesetUid(default,null) : Int; diff --git a/src/ldtk/macro/TypeBuilder.hx b/src/ldtk/macro/TypeBuilder.hx index fdf0d33..7fe24ea 100644 --- a/src/ldtk/macro/TypeBuilder.hx +++ b/src/ldtk/macro/TypeBuilder.hx @@ -547,36 +547,45 @@ class TypeBuilder { timer("tilesetClasses"); tilesets = new Map(); for(t in json.defs.tilesets) { - // Create tileset class - var parentTypePath : TypePath = { pack: [APP_PACKAGE], name:"Tileset" } - var tilesetType : TypeDefinition = { - pos : curPos, - name : "Tileset_"+t.identifier, - pack : modPack, - doc: 'Tileset class of atlas "${t.relPath}"', - kind : TDClass(parentTypePath), - fields : (macro class { - override public function new(p,json) { - super(p,json); - } - }).fields, - } - // Enum tags - if( t.tagsSourceEnumUid!=null ) { + var tilesetType : TypeDefinition; + if( t.tagsSourceEnumUid==null ) { + // Create tileset class + var parentTypePath : TypePath = { pack: [APP_PACKAGE], name:"Tileset" }; + tilesetType = { + pos : curPos, + name : "Tileset_"+t.identifier, + pack : modPack, + doc: 'Tileset class of atlas "${t.relPath}"', + kind : TDClass(parentTypePath), + fields : (macro class { + override public function new(p,json) { + super(p,json); + } + }).fields, + } + } else { var enumTypeDef = localEnums.get(t.tagsSourceEnumUid); var enumComplexType = Context.getType(enumTypeDef.name).toComplexType(); + // Create tileset class + var parentTypePath : TypePath = { pack: [APP_PACKAGE], name:"Tileset", sub:"TypedTileset", params:[TPType(enumComplexType)] }; + tilesetType = { + pos : curPos, + name : "Tileset_"+t.identifier, + pack : modPack, + doc: 'Tileset class of atlas "${t.relPath}"', + kind : TDClass(parentTypePath), + fields : (macro class { + override public function new(p,json) { + super(p,json); + } + }).fields, + } var enumTypeExpr = { pos:curPos, expr:EConst(CIdent(enumTypeDef.name)) } tilesetType.fields = tilesetType.fields.concat( (macro class { - /** Return TRUE if the specifiied tile ID was tagged with given enum `tag`. **/ - public inline function hasTag(tileId:Int, tag:$enumComplexType) { - final allTileIds = untypedTags.get( tag.getName() ); - return allTileIds==null ? false : allTileIds.exists(tileId); - } - /** Return an array of all tags associated with give tile ID. WARNING: this allocates a new array on call. **/ - public function getAllTags(tileId:Int) : Array<$enumComplexType> { + override function getAllTags(tileId:Int) : Array<$enumComplexType> { var all = []; for(t in untypedTags.keys()) { if( untypedTags.get(t).exists(tileId) ) From 5762defe60ad4245e8f2b0e2ad9c6025c20d31ac Mon Sep 17 00:00:00 2001 From: George FunBook Date: Wed, 19 Jun 2024 11:22:12 -0500 Subject: [PATCH 3/4] extend TypedLayer_Tile --- src/ldtk/Layer_Tiles.hx | 12 ++++++++---- src/ldtk/macro/TypeBuilder.hx | 6 +----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ldtk/Layer_Tiles.hx b/src/ldtk/Layer_Tiles.hx index d36be52..912bc99 100644 --- a/src/ldtk/Layer_Tiles.hx +++ b/src/ldtk/Layer_Tiles.hx @@ -2,15 +2,19 @@ package ldtk; import ldtk.Tileset; -typedef Layer_Tiles = TypedLayer_Tiles; +typedef Layer_Tiles = TypedLayer_Tiles; -class TypedLayer_Tiles extends ldtk.Layer { +class TypedLayer_Tiles> extends ldtk.Layer { var tiles : Map>; - /** Getter to layer untyped Tileset instance. The typed value is created in macro. **/ - var untypedTileset(get,never) : TypedTileset; + /** Getter to layer untyped Tileset instance. **/ + var untypedTileset(get,never) : TTileset; inline function get_untypedTileset() return cast untypedProject._untypedTilesets.get(tilesetUid); + /** Getter for this layer's tileset */ + public var tileset(get,never) : TTileset; + inline function get_tileset() return untypedTileset; + /** Tileset UID **/ public var tilesetUid(default,null) : Int; diff --git a/src/ldtk/macro/TypeBuilder.hx b/src/ldtk/macro/TypeBuilder.hx index 7fe24ea..168b877 100644 --- a/src/ldtk/macro/TypeBuilder.hx +++ b/src/ldtk/macro/TypeBuilder.hx @@ -765,8 +765,8 @@ class TypeBuilder { if( l.tilesetDefUid==null || !tilesets.exists(l.tilesetDefUid) ) error('Missing default tileset in layer "${l.identifier}"'); - var parentTypePath : TypePath = { pack: [APP_PACKAGE], name:"Layer_Tiles" } var tilesetCT = Context.getType( tilesets.get(l.tilesetDefUid).typeName ).toComplexType(); + var parentTypePath : TypePath = { pack: [APP_PACKAGE], name:"Layer_Tiles", sub:"TypedLayer_Tiles", params:[TPType(tilesetCT)] }; var layerType : TypeDefinition = { pos : curPos, name : "Layer_"+l.identifier, @@ -774,10 +774,6 @@ class TypeBuilder { doc: "Tile layer", kind : TDClass(parentTypePath), fields : (macro class { - public var tileset(get,never) : $tilesetCT; - inline function get_tileset() return cast untypedTileset; - - override public function new(p,json) { super(p,json); } From 47894a0544af656b9574bcbd862ee6d8842a0139 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Wed, 19 Jun 2024 11:29:18 -0500 Subject: [PATCH 4/4] follow prev style --- src/ldtk/Layer_Tiles.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ldtk/Layer_Tiles.hx b/src/ldtk/Layer_Tiles.hx index 912bc99..99e1597 100644 --- a/src/ldtk/Layer_Tiles.hx +++ b/src/ldtk/Layer_Tiles.hx @@ -9,11 +9,11 @@ class TypedLayer_Tiles> extends ldtk.Layer { /** Getter to layer untyped Tileset instance. **/ var untypedTileset(get,never) : TTileset; - inline function get_untypedTileset() return cast untypedProject._untypedTilesets.get(tilesetUid); + inline function get_untypedTileset() return cast untypedProject._untypedTilesets.get(tilesetUid); /** Getter for this layer's tileset */ public var tileset(get,never) : TTileset; - inline function get_tileset() return untypedTileset; + inline function get_tileset() return untypedTileset; /** Tileset UID **/ public var tilesetUid(default,null) : Int;