From 5c316c38b5125a040c76109357933b2fe2c37721 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 25 Apr 2023 23:14:15 +0100 Subject: Reading direction property from entities --- data/map1.json | 37 +++++++++++++++++++++++++++++++++---- src/Game/Map.hs | 32 +++++++++++++++++--------------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/data/map1.json b/data/map1.json index d68295c..a43c2e8 100644 --- a/data/map1.json +++ b/data/map1.json @@ -179,7 +179,13 @@ { "height":16, "id":20, - "name":"Slime-l", + "name":"Slime", + "properties":[ + { + "name":"flip", + "type":"bool", + "value":true + }], "rotation":0, "type":"", "visible":true, @@ -201,7 +207,13 @@ { "height":24, "id":33, - "name":"Shooter-l", + "name":"Shooter", + "properties":[ + { + "name":"flip", + "type":"bool", + "value":true + }], "rotation":0, "type":"", "visible":true, @@ -256,13 +268,30 @@ { "height":24, "id":38, - "name":"Tracker-l", + "name":"Tracker", "rotation":0, "type":"", "visible":true, "width":16, "x":496, "y":136 + }, + { + "height":24, + "id":39, + "name":"Tracker", + "properties":[ + { + "name":"flip", + "type":"bool", + "value":true + }], + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":448, + "y":136 }], "opacity":1, "type":"objectgroup", @@ -271,7 +300,7 @@ "y":0 }], "nextlayerid":6, - "nextobjectid":39, + "nextobjectid":40, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.7.2", diff --git a/src/Game/Map.hs b/src/Game/Map.hs index 2a85dbe..5faf0f4 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -56,6 +56,11 @@ data JsonMapData = JsonMapData } deriving (Show) +data ObjectBoolProperty = ObjectBoolProperty + { name :: String, + value :: Bool + } + instance JSON Tileset where showJSON = undefined readJSON (JSArray [JSObject obj]) = do @@ -77,6 +82,13 @@ instance JSON Layer where _ -> Error "unsupported layer type" readJSON _ = mzero +getObjectPropertyDir :: JSObject JSValue -> Result Dir +getObjectPropertyDir obj = case get_field obj "properties" of + Just (JSArray [JSObject prop]) -> do + p <- ObjectBoolProperty <$> valFromObj "name" prop <*> valFromObj "value" prop + pure $ if p.name == "flip" && p.value then DirLeft else DirRight + _ -> pure DirRight + instance JSON Object where showJSON = undefined readJSON (JSObject obj) = @@ -86,25 +98,15 @@ instance JSON Object where Just "Battery" -> BatteryEntity <$> valFromObj "x" obj <*> valFromObj "y" obj Just "Slime" -> - SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight + SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> getObjectPropertyDir obj Just "Robot" -> - RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight + RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> getObjectPropertyDir obj Just "Shooter" -> - ShooterEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight + ShooterEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> getObjectPropertyDir obj Just "Runner" -> - RunnerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight + RunnerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> getObjectPropertyDir obj Just "Tracker" -> - TrackerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight - Just "Slime-l" -> - SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft - Just "Robot-l" -> - RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft - Just "Shooter-l" -> - ShooterEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft - Just "Runner-l" -> - RunnerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft - Just "Tracker-l" -> - TrackerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft + TrackerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> getObjectPropertyDir obj Just (JSString (JSONString s)) -> Error $ "unsupported entity " ++ show s e -> Error $ "unsupported entity in " ++ show e readJSON _ = mzero -- cgit v1.2.3