-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.elm
112 lines (90 loc) · 1.99 KB
/
Location.elm
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
module Location exposing (Location, move, isAdjacentTile, getDirection)
import Direction exposing (..)
type alias Location =
{ x : Int
, y : Int
}
getAbs : Int -> Int -> Int
getAbs a b =
if a < b then
b - a
else
a - b
isAdjacentTile : Location -> Location -> Bool
isAdjacentTile loc1 loc2 =
let
distX =
getAbs loc1.x loc2.x
distY =
getAbs loc1.y loc2.y
in
distX <= 1 && distY <= 1 && not (distX == 0 && distY == 0)
getDirection : Location -> Location -> Direction
getDirection loc1 loc2 =
let
x1 =
loc1.x
x2 =
loc2.x
y1 =
loc1.y
y2 =
loc2.y
in
if y1 > y2 then
if x1 < x2 then
NORTHEAST
else if x1 > x2 then
NORTHWEST
else
NORTH
else if y1 < y2 then
if x1 < x2 then
SOUTHEAST
else if x1 > x2 then
SOUTHWEST
else
SOUTH
else if x1 < x2 then
EAST
else
WEST
move : Direction -> Location -> Location
move dir loc =
case dir of
NORTH ->
{ loc
| y = loc.y - 1
}
SOUTH ->
{ loc
| y = loc.y + 1
}
WEST ->
{ loc
| x = loc.x - 1
}
EAST ->
{ loc
| x = loc.x + 1
}
NORTHWEST ->
{ loc
| x = loc.x - 1
, y = loc.y - 1
}
SOUTHEAST ->
{ loc
| x = loc.x + 1
, y = loc.y + 1
}
NORTHEAST ->
{ loc
| x = loc.x + 1
, y = loc.y - 1
}
SOUTHWEST ->
{ loc
| x = loc.x - 1
, y = loc.y + 1
}