diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-04-25 23:14:15 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-04-25 23:14:15 +0100 |
commit | 5c316c38b5125a040c76109357933b2fe2c37721 (patch) | |
tree | 5fa00595b534513a68735b5ca4a1433839d8e3c6 | |
parent | d5545c94b84fc77438280007beb72792b70dd5a3 (diff) | |
download | space-plat-hs-5c316c38b5125a040c76109357933b2fe2c37721.tar.gz space-plat-hs-5c316c38b5125a040c76109357933b2fe2c37721.zip |
Reading direction property from entities
-rw-r--r-- | data/map1.json | 37 | ||||
-rw-r--r-- | 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 |