Skip to content

Commit d215450

Browse files
authored
Merge branch 'master' into rf/caster
2 parents 2a5e475 + 2d11951 commit d215450

18 files changed

+2692
-0
lines changed

pytiled_parser/objects.py

Lines changed: 569 additions & 0 deletions
Large diffs are not rendered by default.

pytiled_parser/typing_helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def is_float(string: str):
2+
try:
3+
float(string)
4+
return True
5+
except (ValueError, TypeError):
6+
return False

pytiled_parser/utilities.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Helper unitilies for pytiled_parser."""
2+
3+
from typing import List, Optional
4+
5+
import pytiled_parser.objects as objects
6+
7+
8+
def parse_color(color: str) -> objects.Color:
9+
"""Convert Tiled color format into Arcade's.
10+
11+
Args:
12+
color (str): Tiled formatted color string.
13+
14+
Returns:
15+
:Color: Color object in the format that Arcade understands.
16+
"""
17+
# the actual part we care about is always an even number
18+
if len(color) % 2:
19+
# strip initial '#' character
20+
color = color[1:]
21+
22+
if len(color) == 6:
23+
# full opacity if no alpha specified
24+
alpha = 0xFF
25+
red = int(color[0:2], 16)
26+
green = int(color[2:4], 16)
27+
blue = int(color[4:6], 16)
28+
else:
29+
alpha = int(color[0:2], 16)
30+
red = int(color[2:4], 16)
31+
green = int(color[4:6], 16)
32+
blue = int(color[6:8], 16)
33+
34+
return objects.Color(red, green, blue, alpha)
35+
36+
37+
def _get_tile_set_key(gid: int, tile_set_keys: List[int]) -> int:
38+
"""Gets tile set key given a tile GID.
39+
40+
Args:
41+
gid (int): Global ID of the tile.
42+
tile_set_keys (List[int]): List of tile set keys.
43+
44+
Returns:
45+
int: The key of the tile set that contains the tile for the GID.
46+
"""
47+
48+
# credit to __m4ch1n3__ on ##learnpython for this idea
49+
return max([key for key in tile_set_keys if key <= gid])
50+
51+
52+
def get_tile_by_gid(gid: int, tile_sets: objects.TileSetDict) -> Optional[objects.Tile]:
53+
"""Gets correct Tile for a given global ID.
54+
55+
Args:
56+
tile_sets (objects.TileSetDict): TileSetDict from TileMap.
57+
gid (int): Global tile ID of the tile to be returned.
58+
59+
Returns:
60+
objects.Tile: The Tile object reffered to by the global tile ID.
61+
None: If there is no objects.Tile object in the tile_set.tiles dict for the associated gid.
62+
"""
63+
tile_set_key = _get_tile_set_key(gid, list(tile_sets.keys()))
64+
tile_set = tile_sets[tile_set_key]
65+
66+
if tile_set.tiles is not None:
67+
return tile_set.tiles.get(gid - tile_set_key)
68+
69+
return None

0 commit comments

Comments
 (0)