From 04c36bb355ec012950a4aea474bb112585a85075 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Mon, 1 Jun 2020 20:09:05 -0400 Subject: [PATCH] feature: implemented properties casting for TiledObjects --- pytiled_parser/properties.py | 5 +++-- pytiled_parser/tiled_object.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pytiled_parser/properties.py b/pytiled_parser/properties.py index 54728fd..4507f75 100644 --- a/pytiled_parser/properties.py +++ b/pytiled_parser/properties.py @@ -3,10 +3,11 @@ from typing import Dict, List, NamedTuple, Optional, Union from .common_types import Color -RawProperties = List[Dict[str, Union[str, bool, int, float]]] - Property = Union[int, float, Path, str, bool, Color] +RawProperties = List[Dict[str, Property]] + + Properties = Dict[str, Property] diff --git a/pytiled_parser/tiled_object.py b/pytiled_parser/tiled_object.py index 2cf2192..b05527f 100644 --- a/pytiled_parser/tiled_object.py +++ b/pytiled_parser/tiled_object.py @@ -5,8 +5,8 @@ from typing import Callable, Dict, List, Mapping, Optional, Union import attr from typing_extensions import TypedDict -from .properties import Properties, RawProperties from .common_types import Color, OrderedPair, Size +from .properties import Properties, RawProperties from .template import Template @@ -43,7 +43,7 @@ class TiledObject: name: Optional[str] = None type: Optional[str] = None - properties: Optional[Properties] = None + properties: Properties = {} template: Optional[Template] = None @@ -190,7 +190,9 @@ def _get_common_attributes(raw_tiled_object: RawTiledObject) -> TiledObject: coordinates = OrderedPair(x=raw_tiled_object["x"], y=raw_tiled_object["y"]) visible = raw_tiled_object["visible"] - common_attributes = TiledObject(id=id_, coordinates=coordinates, visible=visible) + common_attributes = TiledObject( + id=id_, coordinates=coordinates, visible=visible, properties={} + ) # optional attributes if any([raw_tiled_object.get("width"), raw_tiled_object.get("height")]): @@ -222,7 +224,10 @@ def _get_common_attributes(raw_tiled_object: RawTiledObject) -> TiledObject: common_attributes.type = raw_tiled_object["type"] if raw_tiled_object.get("properties"): - raise NotImplementedError + for prop in raw_tiled_object["properties"]: + name = str(prop["name"]) + value = prop["value"] + common_attributes.properties[name] = prop["value"] if raw_tiled_object.get("template"): raise NotImplementedError