summaryrefslogtreecommitdiff
blob: da3b8ef04e601b10ed0cdf3f73255bfd989eaf2b (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
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 --------------------------------------------------------------------
--------------------------------------------------------------------------------
new : Int -> Int -> Type
new x y =
   {
      x = x,
      y = y
   }

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) )
      ]
   )

neighbors : Type -> (List Type)
neighbors loc =
   [
      {loc | x = (loc.x + 1)},
      {loc | x = (loc.x - 1)},
      {loc | y = (loc.y - 1)},
      {loc | y = (loc.y + 1)}
   ]

get_full_neighborhood : Type -> (List Type)
get_full_neighborhood loc =
   [
      {loc | x = (loc.x - 1), y = (loc.y - 1)},
      {loc | y = (loc.y - 1)},
      {loc | x = (loc.x + 1), y = (loc.y - 1)},
      {loc | x = (loc.x - 1)},
      {loc | x = (loc.x + 1)},
      {loc | x = (loc.x - 1), y = (loc.y + 1)},
      {loc | y = (loc.y + 1)},
      {loc | x = (loc.x + 1), y = (loc.y + 1)}
   ]