Skip to content

Commit 72bd24b

Browse files
authored
Merge pull request #31 from Beefy-Swain/development
Development->Master
2 parents 2d3d121 + 3ad32b1 commit 72bd24b

File tree

9 files changed

+398
-10
lines changed

9 files changed

+398
-10
lines changed

pytiled_parser/tiled_map.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ def parse_map(file: Path) -> TiledMap:
129129
for raw_tileset in raw_tilesets:
130130
if raw_tileset.get("source") is not None:
131131
# Is an external Tileset
132-
with open(parent_dir / raw_tileset["source"]) as raw_tileset_file:
132+
tileset_path = Path(parent_dir / raw_tileset["source"])
133+
with open(tileset_path) as raw_tileset_file:
133134
tilesets[raw_tileset["firstgid"]] = tileset.cast(
134-
json.load(raw_tileset_file)
135+
json.load(raw_tileset_file), external_path=tileset_path.parent
135136
)
136137
else:
137138
# Is an embedded Tileset

pytiled_parser/tileset.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def _cast_terrain(raw_terrain: RawTerrain) -> Terrain:
287287
)
288288

289289

290-
def _cast_tile(raw_tile: RawTile) -> Tile:
290+
def _cast_tile(raw_tile: RawTile, external_path: Optional[Path] = None) -> Tile:
291291
"""Cast the raw_tile to a Tile object.
292292
293293
Args:
@@ -312,7 +312,10 @@ def _cast_tile(raw_tile: RawTile) -> Tile:
312312
tile.properties = properties_.cast(raw_tile["properties"])
313313

314314
if raw_tile.get("image") is not None:
315-
tile.image = Path(raw_tile["image"])
315+
if external_path:
316+
tile.image = Path(external_path / raw_tile["image"]).absolute().resolve()
317+
else:
318+
tile.image = Path(raw_tile["image"])
316319

317320
if raw_tile.get("imagewidth") is not None:
318321
tile.image_width = raw_tile["imagewidth"]
@@ -353,7 +356,7 @@ def _cast_grid(raw_grid: RawGrid) -> Grid:
353356
)
354357

355358

356-
def cast(raw_tileset: RawTileSet) -> Tileset:
359+
def cast(raw_tileset: RawTileSet, external_path: Optional[Path] = None) -> Tileset:
357360
"""Cast the raw tileset into a pytiled_parser type
358361
359362
Args:
@@ -383,7 +386,12 @@ def cast(raw_tileset: RawTileSet) -> Tileset:
383386
tileset.tiled_version = raw_tileset["tiledversion"]
384387

385388
if raw_tileset.get("image") is not None:
386-
tileset.image = Path(raw_tileset["image"])
389+
if external_path:
390+
tileset.image = (
391+
Path(external_path / raw_tileset["image"]).absolute().resolve()
392+
)
393+
else:
394+
tileset.image = Path(raw_tileset["image"])
387395

388396
if raw_tileset.get("imagewidth") is not None:
389397
tileset.image_width = raw_tileset["imagewidth"]
@@ -418,7 +426,7 @@ def cast(raw_tileset: RawTileSet) -> Tileset:
418426
if raw_tileset.get("tiles") is not None:
419427
tiles = {}
420428
for raw_tile in raw_tileset["tiles"]:
421-
tiles[raw_tile["id"]] = _cast_tile(raw_tile)
429+
tiles[raw_tile["id"]] = _cast_tile(raw_tile, external_path=external_path)
422430
tileset.tiles = tiles
423431

424432
return tileset
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
from pathlib import Path
2+
3+
from pytiled_parser import common_types, layer, tiled_map, tiled_object, tileset
4+
5+
EXPECTED = tiled_map.TiledMap(
6+
infinite=False,
7+
map_size=common_types.Size(8, 6),
8+
next_layer_id=3,
9+
next_object_id=1,
10+
orientation="orthogonal",
11+
render_order="right-down",
12+
tiled_version="1.5.0",
13+
tile_size=common_types.Size(32, 32),
14+
version=1.5,
15+
background_color=common_types.Color(255, 0, 4, 255),
16+
layers=[
17+
layer.TileLayer(
18+
name="Layer 1",
19+
opacity=1,
20+
visible=True,
21+
id=2,
22+
size=common_types.Size(8, 6),
23+
data=[
24+
[4, 3, 2, 1, 0, 0, 0, 0],
25+
[
26+
0,
27+
0,
28+
0,
29+
0,
30+
0,
31+
0,
32+
0,
33+
0,
34+
],
35+
[
36+
0,
37+
0,
38+
0,
39+
0,
40+
0,
41+
0,
42+
0,
43+
0,
44+
],
45+
[
46+
0,
47+
0,
48+
0,
49+
0,
50+
0,
51+
0,
52+
0,
53+
0,
54+
],
55+
[
56+
0,
57+
0,
58+
0,
59+
0,
60+
0,
61+
0,
62+
0,
63+
0,
64+
],
65+
[
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
],
75+
],
76+
),
77+
],
78+
tilesets={
79+
1: tileset.Tileset(
80+
columns=0,
81+
margin=0,
82+
spacing=0,
83+
name="tileset",
84+
tile_count=4,
85+
tiled_version="1.3.5",
86+
tile_height=32,
87+
tile_width=32,
88+
version=1.2,
89+
type="tileset",
90+
grid=tileset.Grid(orientation="orthogonal", width=1, height=1),
91+
tiles={
92+
0: tileset.Tile(
93+
animation=[
94+
tileset.Frame(duration=100, tile_id=0),
95+
tileset.Frame(duration=100, tile_id=1),
96+
tileset.Frame(duration=100, tile_id=2),
97+
tileset.Frame(duration=100, tile_id=3),
98+
],
99+
id=0,
100+
image=Path(Path(__file__).parent / "../../images/tile_01.png")
101+
.absolute()
102+
.resolve(),
103+
image_height=32,
104+
image_width=32,
105+
properties={"float property": 2.2},
106+
type="tile",
107+
),
108+
1: tileset.Tile(
109+
id=1,
110+
image=Path(Path(__file__).parent / "../../images/tile_02.png")
111+
.absolute()
112+
.resolve(),
113+
image_height=32,
114+
image_width=32,
115+
objects=layer.ObjectLayer(
116+
name="",
117+
opacity=1,
118+
visible=True,
119+
draw_order="index",
120+
tiled_objects=[
121+
tiled_object.Rectangle(
122+
id=2,
123+
name="",
124+
size=common_types.Size(
125+
14.4766410408043, 13.7196924896511
126+
),
127+
rotation=0,
128+
type="",
129+
visible=True,
130+
coordinates=common_types.OrderedPair(
131+
13.4358367829687, 13.5304553518628
132+
),
133+
),
134+
tiled_object.Ellipse(
135+
id=3,
136+
name="",
137+
size=common_types.Size(
138+
14.287403903016, 11.070372560615
139+
),
140+
rotation=0,
141+
type="",
142+
visible=True,
143+
coordinates=common_types.OrderedPair(
144+
13.8143110585452, 1.98698994677705
145+
),
146+
),
147+
],
148+
),
149+
properties={"string property": "testing"},
150+
type="tile",
151+
),
152+
2: tileset.Tile(
153+
id=2,
154+
image=Path(Path(__file__).parent / "../../images/tile_03.png")
155+
.absolute()
156+
.resolve(),
157+
image_height=32,
158+
image_width=32,
159+
properties={"bool property": True},
160+
type="tile",
161+
),
162+
3: tileset.Tile(
163+
id=3,
164+
image=Path(Path(__file__).parent / "../../images/tile_04.png")
165+
.absolute()
166+
.resolve(),
167+
image_height=32,
168+
image_width=32,
169+
type="tile",
170+
),
171+
},
172+
)
173+
},
174+
properties={
175+
"bool property - true": True,
176+
"color property": common_types.Color(255, 73, 252, 255),
177+
"file property": Path("../../../../../../var/log/syslog"),
178+
"float property": 1.23456789,
179+
"int property": 13,
180+
"string property": "Hello, World!!",
181+
},
182+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{ "backgroundcolor":"#ff0004",
2+
"compressionlevel":0,
3+
"height":6,
4+
"infinite":false,
5+
"layers":[
6+
{
7+
"compression":"zlib",
8+
"data":"eAFjYWBgYAZiJiBmBOKhBgAIGAAL",
9+
"encoding":"base64",
10+
"height":6,
11+
"id":2,
12+
"name":"Layer 1",
13+
"opacity":1,
14+
"type":"tilelayer",
15+
"visible":true,
16+
"width":8,
17+
"x":0,
18+
"y":0
19+
}],
20+
"nextlayerid":3,
21+
"nextobjectid":1,
22+
"orientation":"orthogonal",
23+
"properties":[
24+
{
25+
"name":"bool property - true",
26+
"type":"bool",
27+
"value":true
28+
},
29+
{
30+
"name":"color property",
31+
"type":"color",
32+
"value":"#ff49fcff"
33+
},
34+
{
35+
"name":"file property",
36+
"type":"file",
37+
"value":"..\/..\/..\/..\/..\/..\/var\/log\/syslog"
38+
},
39+
{
40+
"name":"float property",
41+
"type":"float",
42+
"value":1.23456789
43+
},
44+
{
45+
"name":"int property",
46+
"type":"int",
47+
"value":13
48+
},
49+
{
50+
"name":"string property",
51+
"type":"string",
52+
"value":"Hello, World!!"
53+
}],
54+
"renderorder":"right-down",
55+
"tiledversion":"1.5.0",
56+
"tileheight":32,
57+
"tilesets":[
58+
{
59+
"firstgid":1,
60+
"source":"tileset\/tileset.json"
61+
}],
62+
"tilewidth":32,
63+
"type":"map",
64+
"version":1.5,
65+
"width":8
66+
}

0 commit comments

Comments
 (0)