summaryrefslogtreecommitdiff
blob: 7012e4b426ad0ce220268e262eecb63030a70c4e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module BattleMap.Struct.Marker exposing
   (
      Type,
      DataType,
      new,
      new_melee_attack,
      get_locations,
      set_locations,
      is_dangerous,
      get_data,
      is_in_locations,
      decoder,
      encode
   )

-- Elm -------------------------------------------------------------------------
import Set
import Json.Decode
import Json.Encode
import List

-- Battle Map ------------------------------------------------------------------
import BattleMap.Struct.Location

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias MeleeAttackZoneStruct =
   {
      character_ix : Int
   }

type alias SpawnZoneStruct =
   {
      player_ix : Int
   }

type DataType =
   MeleeAttackZone MeleeAttackZoneStruct
   | SpawnZone SpawnZoneStruct
   | None

type alias Type =
   {
      locations : (Set.Set BattleMap.Struct.Location.Ref),
      data : DataType
   }

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
decoder_internals : String -> (Json.Decode.Decoder DataType)
decoder_internals t =
   case t of
      "matk" ->
         (Json.Decode.map
            (\e -> (MeleeAttackZone e))
            (Json.Decode.map
               MeleeAttackZoneStruct
               (Json.Decode.field "cix" (Json.Decode.int))
            )
         )

      "spawn" ->
         (Json.Decode.map
            (\e -> (SpawnZone e))
            (Json.Decode.map
               SpawnZoneStruct
               (Json.Decode.field "pix" (Json.Decode.int))
            )
         )

      _ -> (Json.Decode.succeed None)

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
new : Type
new =
   {
      locations = (Set.empty),
      data = None
   }

new_melee_attack : Int -> (Set.Set BattleMap.Struct.Location.Ref) -> Type
new_melee_attack char_ix locations =
   {
      locations = locations,
      data = (MeleeAttackZone {character_ix = char_ix})
   }

get_locations : Type -> (Set.Set BattleMap.Struct.Location.Ref)
get_locations marker = marker.locations

set_locations : (Set.Set BattleMap.Struct.Location.Ref) -> Type -> Type
set_locations locations marker = {marker | locations = locations}

get_data : Type -> DataType
get_data marker = marker.data

is_in_locations : BattleMap.Struct.Location.Ref -> Type -> Bool
is_in_locations loc_ref marker = (Set.member loc_ref marker.locations)

decoder : (Json.Decode.Decoder Type)
decoder =
   (Json.Decode.map2
      Type
      (Json.Decode.field
         "l"
         (Json.Decode.map
            (Set.fromList)
            (Json.Decode.list
               (Json.Decode.map
                  (BattleMap.Struct.Location.get_ref)
                  (BattleMap.Struct.Location.decoder)
               )
            )
         )
      )
      (Json.Decode.andThen
         (decoder_internals)
         (Json.Decode.field "t" (Json.Decode.string))
      )
   )

encode : Type -> Json.Encode.Value
encode marker =
   (Json.Encode.object
      [
         (
            "l",
            (Json.Encode.list
               (\e ->
                  (BattleMap.Struct.Location.encode
                     (BattleMap.Struct.Location.from_ref e)
                  )
               )
               (Set.toList marker.locations)
            )
         ),
         (
            "d",
            (
               case marker.data of
                  SpawnZone zone ->
                     (Json.Encode.object
                        [
                           ("t", (Json.Encode.string "spawn")),
                           ("pix", (Json.Encode.int zone.player_ix))
                        ]
                     )

                  MeleeAttackZone zone ->
                     (Json.Encode.object
                        [
                           ("t", (Json.Encode.string "matk")),
                           ("cix", (Json.Encode.int zone.character_ix))
                        ]
                     )

                  None ->
                     (Json.Encode.object
                        [
                           ("t", (Json.Encode.string "none"))
                        ]
                     )
            )
         )
      ]
   )

is_dangerous : Type -> Bool
is_dangerous marker =
   case marker.data of
      (MeleeAttackZone _) -> True
      _ -> False