Skip to content

Commit 4e7eba2

Browse files
Load template tile objects without preexisting map tilesets
AI-assisted by Codex (GPT-6).
1 parent 00691fa commit 4e7eba2

3 files changed

Lines changed: 87 additions & 6 deletions

File tree

‎pytiled_parser/parsers/json/tiled_map.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,13 @@ def parse(file: Path, encoding: str) -> TiledMap:
156156
break
157157

158158
if not already_loaded:
159-
highest_firstgid = max(map_.tilesets.keys())
160-
last_tileset_count = map_.tilesets[highest_firstgid].tile_count
161-
new_firstgid = highest_firstgid + last_tileset_count
159+
new_firstgid = 1
160+
if map_.tilesets:
161+
highest_firstgid = max(map_.tilesets.keys())
162+
last_tileset_count = map_.tilesets[
163+
highest_firstgid
164+
].tile_count
165+
new_firstgid = highest_firstgid + last_tileset_count
162166
map_.tilesets[new_firstgid] = parse_json_tileset(
163167
tiled_object.new_tileset,
164168
new_firstgid,

‎pytiled_parser/parsers/tmx/tiled_map.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,13 @@ def parse(file: Path, encoding: str) -> TiledMap:
110110

111111
if not already_loaded:
112112
print("here")
113-
highest_firstgid = max(map_.tilesets.keys())
114-
last_tileset_count = map_.tilesets[highest_firstgid].tile_count
115-
new_firstgid = highest_firstgid + last_tileset_count
113+
new_firstgid = 1
114+
if map_.tilesets:
115+
highest_firstgid = max(map_.tilesets.keys())
116+
last_tileset_count = map_.tilesets[
117+
highest_firstgid
118+
].tile_count
119+
new_firstgid = highest_firstgid + last_tileset_count
116120
map_.tilesets[new_firstgid] = parse_tmx_tileset(
117121
tiled_object.new_tileset,
118122
new_firstgid,

‎tests/test_template_tilesets.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Tilesets supplied by object templates must also work without map tilesets."""
2+
3+
import copy
4+
import json
5+
import xml.etree.ElementTree as etree
6+
from pathlib import Path
7+
8+
import pytest
9+
10+
from pytiled_parser import parse_map
11+
12+
TEMPLATE_DATA = Path(__file__).parent / "test_data" / "map_tests" / "template"
13+
14+
15+
@pytest.mark.parametrize("format_", ["json", "tmx"])
16+
@pytest.mark.parametrize("with_map_tilesets", [False, True])
17+
def test_template_tileset_allocation(tmp_path, format_, with_map_tilesets):
18+
source = TEMPLATE_DATA / ("map." + format_)
19+
destination = tmp_path / source.name
20+
if format_ == "json":
21+
document = json.loads(source.read_text())
22+
if with_map_tilesets:
23+
for tileset in document["tilesets"]:
24+
tileset["source"] = str((TEMPLATE_DATA / tileset["source"]).resolve())
25+
else:
26+
document["tilesets"] = []
27+
document["nextobjectid"] = 9
28+
objects = document["layers"][0]["objects"]
29+
objects.append(copy.deepcopy(objects[1]))
30+
objects[-1]["id"] = 8
31+
for object_ in objects:
32+
object_["template"] = str((TEMPLATE_DATA / object_["template"]).resolve())
33+
destination.write_text(json.dumps(document))
34+
else:
35+
document = etree.parse(source)
36+
for tileset in document.findall("tileset"):
37+
if with_map_tilesets:
38+
tileset.set(
39+
"source", str((TEMPLATE_DATA / tileset.get("source")).resolve())
40+
)
41+
else:
42+
document.getroot().remove(tileset)
43+
document.getroot().set("nextobjectid", "9")
44+
layer = document.find("objectgroup")
45+
repeated = copy.deepcopy(layer.findall("object")[1])
46+
repeated.set("id", "8")
47+
layer.append(repeated)
48+
for object_ in layer.findall("object"):
49+
object_.set(
50+
"template", str((TEMPLATE_DATA / object_.get("template")).resolve())
51+
)
52+
document.write(destination, encoding="utf-8")
53+
54+
parsed = parse_map(destination)
55+
objects = parsed.layers[0].tiled_objects
56+
expected_gids = [49, 50, 49] if with_map_tilesets else [1, 2, 1]
57+
assert [object_.gid for object_ in objects[1:]] == expected_gids
58+
assert sorted(parsed.tilesets) == ([1, 49, 50] if with_map_tilesets else [1, 2])
59+
for gid, name in zip(
60+
expected_gids,
61+
[
62+
"tile_set_image_for_template",
63+
"tile_set_single_image",
64+
"tile_set_image_for_template",
65+
],
66+
):
67+
assert parsed.tilesets[gid].name == name
68+
assert parsed.tilesets[gid].firstgid == gid
69+
assert (
70+
parsed.tilesets[gid].image is not None
71+
or parsed.tilesets[gid].tiles[0].image is not None
72+
)
73+
assert all(object_.new_tileset is None for object_ in objects[1:])

0 commit comments

Comments
 (0)