summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2019-03-15 18:16:55 +0100
committernsensfel <SpamShield0@noot-noot.org>2019-03-15 18:16:55 +0100
commit6678cfe464ed9ee595f4f3dd7398dec1416454c9 (patch)
tree2700668874e13a81ec7467dcf26a1d246caa23ff /src/shared/battle-map/BattleMap/Struct/Location.elm
parent24efb898f526e0aa02a0e15b74436da8ba166cac (diff)
[Broken] Starting a code refactoring...
Diffstat (limited to 'src/shared/battle-map/BattleMap/Struct/Location.elm')
-rw-r--r--src/shared/battle-map/BattleMap/Struct/Location.elm70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/shared/battle-map/BattleMap/Struct/Location.elm b/src/shared/battle-map/BattleMap/Struct/Location.elm
new file mode 100644
index 0000000..3443150
--- /dev/null
+++ b/src/shared/battle-map/BattleMap/Struct/Location.elm
@@ -0,0 +1,70 @@
+module BattleMap.Struct.Location exposing (..)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+import Json.Decode.Pipeline
+
+import Json.Encode
+
+-- Battle Map ------------------------------------------------------------------
+import BattleMap.Struct.Direction
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+type alias Type =
+ {
+ x : Int,
+ y : Int
+ }
+
+type alias Ref = (Int, Int)
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+neighbor : BattleMap.Struct.Direction.Type -> Type -> Type
+neighbor dir loc =
+ case dir of
+ BattleMap.Struct.Direction.Right -> {loc | x = (loc.x + 1)}
+ BattleMap.Struct.Direction.Left -> {loc | x = (loc.x - 1)}
+ BattleMap.Struct.Direction.Up -> {loc | y = (loc.y - 1)}
+ BattleMap.Struct.Direction.Down -> {loc | y = (loc.y + 1)}
+ BattleMap.Struct.Direction.None -> loc
+
+get_ref : Type -> Ref
+get_ref l =
+ (l.x, l.y)
+
+from_ref : Ref -> Type
+from_ref (x, y) =
+ {x = x, y = y}
+
+dist : Type -> Type -> Int
+dist loc_a loc_b =
+ (
+ (abs (loc_a.x - loc_b.x))
+ +
+ (abs (loc_a.y - loc_b.y))
+ )
+
+decoder : (Json.Decode.Decoder Type)
+decoder =
+ (Json.Decode.succeed
+ Type
+ |> (Json.Decode.Pipeline.required "x" Json.Decode.int)
+ |> (Json.Decode.Pipeline.required "y" Json.Decode.int)
+ )
+
+encode : Type -> Json.Encode.Value
+encode loc =
+ (Json.Encode.object
+ [
+ ( "x", (Json.Encode.int loc.x) ),
+ ( "y", (Json.Encode.int loc.y) )
+ ]
+ )