diff --git a/src/ldtk/Layer_Tiles.hx b/src/ldtk/Layer_Tiles.hx index a698bc5..99e1597 100644 --- a/src/ldtk/Layer_Tiles.hx +++ b/src/ldtk/Layer_Tiles.hx @@ -1,11 +1,19 @@ 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); + /** 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/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 diff --git a/src/ldtk/macro/TypeBuilder.hx b/src/ldtk/macro/TypeBuilder.hx index fdf0d33..168b877 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) ) @@ -756,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, @@ -765,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); }