Skip to content

Commit 73f9d9d

Browse files
committed
Explicit int and float property types. (#80)
1 parent bdc671b commit 73f9d9d

File tree

9 files changed

+148
-2
lines changed

9 files changed

+148
-2
lines changed

pytiled_parser/parsers/json/properties.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def parse(raw_properties: List[RawProperty]) -> Properties:
4545
value = Path(cast(str, raw_property["value"]))
4646
elif raw_property["type"] == "color":
4747
value = parse_color(cast(str, raw_property["value"]))
48+
elif raw_property["type"] == "int":
49+
value = round(raw_property["value"])
4850
else:
4951
value = raw_property["value"]
5052
final[raw_property["name"]] = value

pytiled_parser/parsers/tmx/properties.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def parse(raw_properties: etree.Element) -> Properties:
2020
value = Path(value_)
2121
elif type_ == "color":
2222
value = parse_color(value_)
23-
elif type_ == "int" or type_ == "float":
23+
elif type_ == "int":
24+
value = round(float(value_))
25+
elif type_ == "float":
2426
value = float(value_)
2527
elif type_ == "bool":
2628
if value_ == "true":

pytiled_parser/properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313

1414
from .common_types import Color
1515

16-
Property = Union[float, Path, str, bool, Color]
16+
Property = Union[int, float, Path, str, bool, Color]
1717

1818
Properties = Dict[str, Property]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from pathlib import Path
2+
3+
from pytiled_parser import common_types, tiled_map, tileset
4+
5+
EXPECTED = tiled_map.TiledMap(
6+
map_file=None,
7+
infinite=False,
8+
layers=[],
9+
map_size=common_types.Size(8, 6),
10+
next_layer_id=2,
11+
next_object_id=1,
12+
orientation="orthogonal",
13+
render_order="right-down",
14+
tiled_version="1.9.1",
15+
tile_size=common_types.Size(32, 32),
16+
version="1.9",
17+
background_color=common_types.Color(255, 0, 4, 255),
18+
parallax_origin=common_types.OrderedPair(10, 15),
19+
tilesets={
20+
1: tileset.Tileset(
21+
columns=8,
22+
image=Path(Path(__file__).parent / "../../images/tmw_desert_spacing.png")
23+
.absolute()
24+
.resolve(),
25+
image_width=265,
26+
image_height=199,
27+
firstgid=1,
28+
margin=1,
29+
spacing=1,
30+
name="tile_set_image",
31+
tile_count=48,
32+
tiled_version="1.6.0",
33+
tile_height=32,
34+
tile_width=32,
35+
version="1.6",
36+
type="tileset",
37+
)
38+
},
39+
properties={
40+
"bool property - true": True,
41+
"color property": common_types.Color(73, 252, 255, 255),
42+
"file property": Path("../../../../../../var/log/syslog"),
43+
"float property": 1.23456789,
44+
"int property": 13,
45+
"broken int property": 14,
46+
"string property": "Hello, World!!",
47+
},
48+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{ "backgroundcolor":"#ff0004",
2+
"compressionlevel":0,
3+
"height":6,
4+
"infinite":false,
5+
"layers":[],
6+
"nextlayerid":2,
7+
"nextobjectid":1,
8+
"orientation":"orthogonal",
9+
"parallaxoriginx":10,
10+
"parallaxoriginy":15,
11+
"properties":[
12+
{
13+
"name":"bool property - true",
14+
"type":"bool",
15+
"value":true
16+
},
17+
{
18+
"name":"color property",
19+
"type":"color",
20+
"value":"#ff49fcff"
21+
},
22+
{
23+
"name":"file property",
24+
"type":"file",
25+
"value":"..\/..\/..\/..\/..\/..\/var\/log\/syslog"
26+
},
27+
{
28+
"name":"float property",
29+
"type":"float",
30+
"value":1.23456789
31+
},
32+
{
33+
"name":"int property",
34+
"type":"int",
35+
"value":13
36+
},
37+
{
38+
"name":"broken int property",
39+
"type":"int",
40+
"value":13.6
41+
},
42+
{
43+
"name":"string property",
44+
"type":"string",
45+
"value":"Hello, World!!"
46+
}],
47+
"renderorder":"right-down",
48+
"tiledversion":"1.9.1",
49+
"tileheight":32,
50+
"tilesets":[
51+
{
52+
"firstgid":1,
53+
"source":"tileset.json"
54+
}],
55+
"tilewidth":32,
56+
"type":"map",
57+
"version":"1.9",
58+
"width":8
59+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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">
3+
<properties>
4+
<property name="bool property - true" type="bool" value="true"/>
5+
<property name="color property" type="color" value="#ff49fcff"/>
6+
<property name="file property" type="file" value="../../../../../../var/log/syslog"/>
7+
<property name="float property" type="float" value="1.23456789"/>
8+
<property name="int property" type="int" value="13"/>
9+
<property name="string property" value="Hello, World!!"/>
10+
<property name="broken int property" type="int" value="13.6"/>
11+
</properties>
12+
<tileset firstgid="1" source="tileset.tsx"/>
13+
</map>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{ "columns":8,
2+
"image":"..\/..\/images\/tmw_desert_spacing.png",
3+
"imageheight":199,
4+
"imagewidth":265,
5+
"margin":1,
6+
"name":"tile_set_image",
7+
"spacing":1,
8+
"tilecount":48,
9+
"tiledversion":"1.9.0",
10+
"tileheight":32,
11+
"tilewidth":32,
12+
"type":"tileset",
13+
"version":"1.8"
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<tileset version="1.6" tiledversion="1.6.0" name="tile_set_image" tilewidth="32" tileheight="32" spacing="1" margin="1" tilecount="48" columns="8">
3+
<image source="../../images/tmw_desert_spacing.png" width="265" height="199"/>
4+
</tileset>

tests/test_map.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for maps"""
2+
23
import importlib.util
34
import os
45
from pathlib import Path
@@ -20,10 +21,12 @@
2021
MAP_TESTS / "embedded_tileset",
2122
MAP_TESTS / "template",
2223
MAP_TESTS / "cross_format_tileset",
24+
MAP_TESTS / "special_do_not_resave_from_tiled",
2325
]
2426

2527
JSON_INVALID_TILESET = MAP_TESTS / "json_invalid_tileset"
2628

29+
2730
def fix_object(my_object):
2831
my_object.coordinates = OrderedPair(
2932
round(my_object.coordinates[0], 3), round(my_object.coordinates[1], 3)
@@ -90,6 +93,7 @@ def test_map_integration(parser_type, map_test):
9093
fix_map(casted_map)
9194
assert casted_map == expected.EXPECTED
9295

96+
9397
def test_json_invalid_tileset():
9498
raw_map_path = JSON_INVALID_TILESET / "map.json"
9599

0 commit comments

Comments
 (0)