Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed Tileset and Layer_Tiles #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/ldtk/Layer_Tiles.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package ldtk;

class Layer_Tiles extends ldtk.Layer {
import ldtk.Tileset;

typedef Layer_Tiles = TypedLayer_Tiles<Tileset>;

class TypedLayer_Tiles<TTileset:TypedTileset<EnumValue>> extends ldtk.Layer {
var tiles : Map<Int, Array<{ tileId:Int, flipBits:Int }>>;

/** 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;
Expand Down
14 changes: 13 additions & 1 deletion src/ldtk/Tileset.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ldtk;

class Tileset {
typedef Tileset = TypedTileset<EnumValue>;

class TypedTileset<TTag:EnumValue> {
var untypedProject: ldtk.Project;

/** Original parsed JSON object **/
Expand Down Expand Up @@ -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<TTag> {
throw "getAllTags not implemented";
}

/***************************************************************************
HEAPS API
Expand Down
61 changes: 33 additions & 28 deletions src/ldtk/macro/TypeBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down Expand Up @@ -756,19 +765,15 @@ 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,
pack : modPack,
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);
}
Expand Down