Skip to content
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
11 changes: 8 additions & 3 deletions pytiled_parser/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ class World:
only_show_adjacent: bool = False


class RawPattern(TypedDict):
class _RequiredPattern(TypedDict):
regexp: str
multiplierX: float
multiplierY: float
offsetX: float
offsetY: float


class RawPattern(_RequiredPattern, total=False):
mapWidth: float
mapHeight: float


class RawWorldMap(TypedDict):
fileName: str
height: float
Expand Down Expand Up @@ -150,8 +155,8 @@ def parse_world(file: Path, encoding: str) -> World:

raw_world_map: RawWorldMap = {
"fileName": map_file,
"width": width,
"height": height,
"width": raw_pattern.get("mapWidth", width),
"height": raw_pattern.get("mapHeight", height),
"x": x,
"y": y,
}
Expand Down
46 changes: 46 additions & 0 deletions tests/test_world_pattern_dimensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import json

import pytest

from pytiled_parser import parse_world
from pytiled_parser.common_types import OrderedPair, Size


@pytest.mark.parametrize(
"dimensions, expected",
[
({}, Size(100, 200)),
({"mapWidth": 80}, Size(80, 200)),
({"mapHeight": 160}, Size(100, 160)),
({"mapWidth": 80, "mapHeight": 160}, Size(80, 160)),
({"mapWidth": 0}, Size(0, 200)),
({"mapHeight": 0}, Size(100, 0)),
],
)
def test_world_pattern_dimensions(tmp_path, dimensions, expected):
map_file = tmp_path / "map_-2_3.tmx"
map_file.touch()
(tmp_path / "unrelated.txt").touch()
pattern = {
"regexp": r"^map_(-?\d+)_(-?\d+)\.tmx$",
"multiplierX": 100,
"multiplierY": 200,
"offsetX": 7,
"offsetY": 11,
}
pattern.update(dimensions)
world_file = tmp_path / "maps.world"
world_data = {"patterns": [pattern], "type": "world"}
world_data["onlyShowAdjacentMaps"] = True
world_file.write_text(
json.dumps(world_data),
encoding="utf-8",
)

parsed = parse_world(world_file)
assert parsed.only_show_adjacent
assert len(parsed.maps) == 1
parsed_map = parsed.maps[0]
assert parsed_map.map_file == map_file
assert parsed_map.size == expected
assert parsed_map.coordinates == OrderedPair(-193, 611)