Skip to content

Commit 6beb77b

Browse files
committed
Initial world implementation
1 parent 7a474a2 commit 6beb77b

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

pytiled_parser/world.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import json
2+
import re
3+
from os import listdir
4+
from os.path import isfile, join
5+
from pathlib import Path
6+
from typing import List
7+
8+
import attr
9+
from typing_extensions import TypedDict
10+
11+
from .common_types import OrderedPair, Size
12+
from .tiled_map import TiledMap, parse_map
13+
14+
15+
@attr.s(auto_attribs=True)
16+
class WorldMap:
17+
18+
tiled_map: TiledMap
19+
size: Size
20+
coordinates: OrderedPair
21+
22+
23+
@attr.s(auto_attribs=True)
24+
class World:
25+
26+
maps: List[WorldMap]
27+
only_show_adjacent: bool = False
28+
29+
30+
class RawPattern(TypedDict):
31+
"""The keys and their types that appear in a Pattern JSON Object."""
32+
33+
regexp: str
34+
multiplierX: float
35+
multiplierY: float
36+
offsetX: float
37+
offsetY: float
38+
39+
40+
class RawWorldMap(TypedDict):
41+
"""The keys and their types that appear in a WorldMap JSON Object."""
42+
43+
fileName: str
44+
height: float
45+
width: float
46+
x: float
47+
y: float
48+
49+
50+
class RawWorld(TypedDict):
51+
"""The keys and their types that appear in a World JSON Object."""
52+
53+
maps: List[RawWorldMap]
54+
patterns: List[RawPattern]
55+
onlyShowAdjacentMaps: bool
56+
57+
58+
def _cast_world_map(raw_world_map: RawWorldMap, map_file: Path) -> WorldMap:
59+
"""Parse the RawWorldMap into a WorldMap.
60+
61+
Args:
62+
raw_world_map: The RawWorldMap to parse
63+
map_file: The file of tiled_map to parse
64+
65+
Returns:
66+
WorldMap: The parsed WorldMap object
67+
"""
68+
tiled_map = parse_map(map_file)
69+
70+
return WorldMap(
71+
tiled_map=tiled_map,
72+
size=Size(raw_world_map["width"], raw_world_map["height"]),
73+
coordinates=OrderedPair(raw_world_map["x"], raw_world_map["y"]),
74+
)
75+
76+
77+
def parse_world(file: Path) -> World:
78+
"""Parse the raw world into a pytiled_parser type
79+
80+
Args:
81+
file: Path to the world's file
82+
83+
Returns:
84+
World: A properly parsed World
85+
"""
86+
87+
with open(file) as world_file:
88+
raw_world = json.load(world_file)
89+
90+
parent_dir = file.parent
91+
92+
maps: List[WorldMap] = []
93+
94+
if raw_world.get("maps"):
95+
for raw_map in raw_world["maps"]:
96+
map_path = Path(parent_dir / raw_map["fileName"])
97+
maps.append(_cast_world_map(raw_map, map_path))
98+
99+
if raw_world.get("patterns"):
100+
for raw_pattern in raw_world["patterns"]:
101+
regex = re.compile(raw_pattern["regexp"])
102+
map_files = [
103+
f
104+
for f in listdir(parent_dir)
105+
if isfile(join(parent_dir, f)) and regex.match(f)
106+
]
107+
for map_file in map_files:
108+
search = regex.search(map_file)
109+
if search:
110+
width = raw_pattern["multiplierX"]
111+
height = raw_pattern["multiplierY"]
112+
113+
offset_x = 0
114+
offset_y = 0
115+
116+
if raw_pattern.get("offsetX"):
117+
offset_x = raw_pattern["offsetX"]
118+
119+
if raw_pattern.get("offsetY"):
120+
offset_y = raw_pattern["offsetY"]
121+
122+
x = (float(search.group(1)) * width) + offset_x
123+
y = (float(search.group(2)) * height) + offset_y
124+
125+
raw_world_map: RawWorldMap = {
126+
"fileName": map_file,
127+
"width": width,
128+
"height": height,
129+
"x": x,
130+
"y": y,
131+
}
132+
133+
map_path = Path(parent_dir / map_file)
134+
maps.append(_cast_world_map(raw_world_map, map_path))
135+
136+
world = World(maps=maps)
137+
138+
if raw_world.get("onlyShowAdjacentMaps"):
139+
world.only_show_adjacent = raw_world["onlyShowAdjacentMaps"]
140+
141+
return world

0 commit comments

Comments
 (0)