summaryrefslogtreecommitdiff
blob: 34431505bab65e7d403e79807ad133e35d42f946 (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
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) )
      ]
   )