summaryrefslogtreecommitdiff
blob: 600138c564bdc988a8f7f2d30b948ed2f43925fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Struct.Direction exposing (Type(..), opposite_of, to_string, decoder)

-- Elm -------------------------------------------------------------------------
import Json.Decode

-- Battlemap -------------------------------------------------------------------

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type Type =
   None
   | Left
   | Right
   | Up
   | Down

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
from_string : String -> Type
from_string str =
   case str of
      "R" -> Right
      "L" -> Left
      "U" -> Up
      "D" -> Down
      _ -> None

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
opposite_of : Type -> Type
opposite_of d =
   case d of
      Left -> Right
      Right -> Left
      Up -> Down
      Down -> Up
      None -> None

to_string : Type -> String
to_string dir =
   case dir of
      Right -> "R"
      Left -> "L"
      Up -> "U"
      Down -> "D"
      None -> "N"

decoder : (Json.Decode.Decoder Type)
decoder = (Json.Decode.map (from_string) Json.Decode.string)