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 spacing support to Tileset #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions src/ldtk/Tileset.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Tileset {
/** Spacing between each tile in pixels */
public var spacing: Int;

var cWid(get,never) : Int; inline function get_cWid() return Math.ceil(pxWid/tileGridSize);
var cWid(get,never) : Int; inline function get_cWid() return
Math.ceil(pxWid/(tileGridSize + spacing));

/** Untyped Enum based tags (stored as String). The "typed" getter method is created in macro. **/
var untypedTags : Map< String, Map<Int,Int> >;
Expand Down Expand Up @@ -67,22 +68,24 @@ class Tileset {
Get X pixel coordinate (in atlas image) from a specified tile ID
**/
public inline function getAtlasX(tileId:Int) {
return ( tileId - Std.int( tileId / cWid ) * cWid ) * tileGridSize;
return ( tileId - Std.int( tileId / cWid ) * cWid ) * (tileGridSize +
spacing);
}

/**
Get Y pixel coordinate (in atlas image) from a specified tile ID
**/
public inline function getAtlasY(tileId:Int) {
return Std.int( tileId / cWid ) * tileGridSize;
return Std.int( tileId / cWid ) * (tileGridSize + spacing);
}

/**
Resolve an atlas coordinate to a tile ID. The result is only relevant if the coordinates are over a tile (ie. in atlas bounds, not in margins etc.).
WARNING: tile spacing is not supported yet!
**/
public inline function getTileIdFromCoords(pixelX:Int, pixelY:Int) {
return Std.int( (pixelX-padding) / tileGridSize ) + cWid * Std.int( pixelY / tileGridSize );
return Std.int( (pixelX-padding) / (tileGridSize + spacing) ) + cWid
* Std.int( pixelY / (tileGridSize + spacing) );
}


Expand Down