Skip to content

Commit 9a92a32

Browse files
committed
Add support for parallax origin in maps
1 parent 83ee2c4 commit 9a92a32

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

pytiled_parser/parsers/json/tiled_map.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from typing_extensions import TypedDict
77

8-
from pytiled_parser.common_types import Size
8+
from pytiled_parser.common_types import OrderedPair, Size
99
from pytiled_parser.exception import UnknownFormat
1010
from pytiled_parser.parsers.json.layer import RawLayer
1111
from pytiled_parser.parsers.json.layer import parse as parse_layer
@@ -44,6 +44,8 @@
4444
"type": str,
4545
"version": Union[str, float],
4646
"width": int,
47+
"parallaxoriginx": float,
48+
"parallaxoriginy": float,
4749
},
4850
)
4951
RawTiledMap.__doc__ = """
@@ -185,4 +187,15 @@ def parse(file: Path) -> TiledMap:
185187
if raw_tiled_map.get("staggerindex") is not None:
186188
map_.stagger_index = raw_tiled_map["staggerindex"]
187189

190+
_parallax_origin_x = 0
191+
_parallax_origin_y = 0
192+
193+
if raw_tiled_map.get("parallaxoriginx") is not None:
194+
_parallax_origin_x = raw_tiled_map["parallaxoriginx"]
195+
196+
if raw_tiled_map.get("parallaxoriginy") is not None:
197+
_parallax_origin_y = raw_tiled_map["parallaxoriginy"]
198+
199+
map_.parallax_origin = OrderedPair(_parallax_origin_x, _parallax_origin_y)
200+
188201
return map_

pytiled_parser/parsers/tmx/tiled_map.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import xml.etree.ElementTree as etree
33
from pathlib import Path
44

5-
from pytiled_parser.common_types import Size
5+
from pytiled_parser.common_types import OrderedPair, Size
66
from pytiled_parser.exception import UnknownFormat
77
from pytiled_parser.parsers.json.tileset import parse as parse_json_tileset
88
from pytiled_parser.parsers.tmx.layer import parse as parse_layer
@@ -145,4 +145,15 @@ def parse(file: Path) -> TiledMap:
145145
if raw_map.attrib.get("class") is not None:
146146
map_.class_ = raw_map.attrib["class"]
147147

148+
_parallax_origin_x = 0
149+
_parallax_origin_y = 0
150+
151+
if raw_map.attrib.get("parallaxoriginx") is not None:
152+
_parallax_origin_x = float(raw_map.attrib["parallaxoriginx"])
153+
154+
if raw_map.get("parallaxoriginy") is not None:
155+
_parallax_origin_y = float(raw_map.attrib["parallaxoriginy"])
156+
157+
map_.parallax_origin = OrderedPair(_parallax_origin_x, _parallax_origin_y)
158+
148159
return map_

pytiled_parser/tiled_map.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import attr
99

10-
from pytiled_parser.common_types import Color, Size
10+
from pytiled_parser.common_types import Color, OrderedPair, Size
1111
from pytiled_parser.layer import Layer
1212
from pytiled_parser.properties import Properties
1313
from pytiled_parser.tileset import Tileset
@@ -66,6 +66,8 @@ class TiledMap:
6666
tilesets: TilesetDict
6767
version: str
6868

69+
parallax_origin: OrderedPair = OrderedPair(0, 0)
70+
6971
map_file: Optional[Path] = None
7072
class_: Optional[str] = None
7173
background_color: Optional[Color] = None

tests/test_data/map_tests/no_layers/expected.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
next_object_id=1,
1111
orientation="orthogonal",
1212
render_order="right-down",
13-
tiled_version="1.6.0",
13+
tiled_version="1.9.1",
1414
tile_size=common_types.Size(32, 32),
15-
version="1.6",
15+
version="1.9",
1616
background_color=common_types.Color(255, 0, 4, 255),
17+
parallax_origin=common_types.OrderedPair(10, 15),
1718
tilesets={
1819
1: tileset.Tileset(
1920
columns=8,

tests/test_data/map_tests/no_layers/map.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"nextlayerid":2,
77
"nextobjectid":1,
88
"orientation":"orthogonal",
9+
"parallaxoriginx":10,
10+
"parallaxoriginy":15,
911
"properties":[
1012
{
1113
"name":"bool property - true",
@@ -38,7 +40,7 @@
3840
"value":"Hello, World!!"
3941
}],
4042
"renderorder":"right-down",
41-
"tiledversion":"1.9.0",
43+
"tiledversion":"1.9.1",
4244
"tileheight":32,
4345
"tilesets":[
4446
{

tests/test_data/map_tests/no_layers/map.tmx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<map version="1.5" tiledversion="1.7.0" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" backgroundcolor="#ff0004" nextlayerid="2" nextobjectid="1">
2+
<map version="1.9" tiledversion="1.9.1" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" parallaxoriginx="10" parallaxoriginy="15" backgroundcolor="#ff0004" nextlayerid="2" nextobjectid="1">
33
<properties>
44
<property name="bool property - true" type="bool" value="true"/>
55
<property name="color property" type="color" value="#ff49fcff"/>

0 commit comments

Comments
 (0)