-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tabletop.elm
97 lines (70 loc) · 1.67 KB
/
Tabletop.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
module Tabletop exposing (..)
import Svg exposing (rect, Svg)
import Svg.Attributes exposing (..)
{-| The Tabletop module exposes types and functions related to the
structure of the game board as well as positioning and movement on the
board.
-}
type alias Tabletop =
{ width : Int
, height : Int
}
type alias Position =
( Float, Float )
posX : Position -> Float
posX ( x', _ ) =
x'
posY : Position -> Float
posY ( _, y' ) =
y'
positionFromMouseCoords : ( Int, Int ) -> Float -> Position
positionFromMouseCoords ( x, y ) scale =
let
transform : Int -> Float
transform a =
(toFloat a) / scale
x' =
transform x
y' =
transform y
in
( x', y' )
type alias Inch =
Int
distance : Position -> Position -> Float
distance ( x1, y1 ) ( x2, y2 ) =
let
y' =
(y1 - y2) ^ 2
x' =
(x1 - x2) ^ 2
in
(x' + y') |> sqrt
positionFromDirection : Position -> Position -> Float -> Position
positionFromDirection start end len =
let
h =
distance start end
x' =
(posX end) - (posX start)
y' =
(posY end) - (posY start)
angle =
acos <| y' / h
co =
if x' > 0 then
1
else
-1
in
( (posX start) + (sin angle * len) * co
, (posY start) + (cos angle * len)
)
view : Tabletop -> List (Svg msg) -> Svg msg
view tabletop children =
rect
[ width (tabletop.width |> toString)
, height (tabletop.height |> toString)
, fill "red"
]
children