-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMover.elm
105 lines (83 loc) · 2.4 KB
/
Mover.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
module Mover exposing (Mover, tickMover, moverView)
import Trig exposing (xDelta, yDelta, normalize)
-- Mover is a generic item that has a position, direction, speed,
-- and can accelerate or turn.
--
-- We share this code among items like Ship and Bullet so we don't
-- have to reimplement those functions many times.
import Html exposing (Html)
import Svg exposing (image)
import Svg.Attributes exposing (x, y, width, height, xlinkHref, transform)
type alias Mover a =
{ a
| x : Float
, y : Float
, d :
Float
-- direction
, s :
Float
-- speed
, ts :
Float
-- top speed
, acc :
Float
-- accelerating -1 0 1
, turn :
Float
-- turning -1 0 1
}
tickMover : Float -> Mover a -> Float -> Float -> Float -> Float -> Mover a
tickMover timeDiff mover x1 y1 x2 y2 =
{ mover
| s = (accelerateMover timeDiff mover)
, d = (turnMover timeDiff mover)
, x = (moveX timeDiff mover x1 x2)
, y = (moveY timeDiff mover y1 y2)
}
moveX : Float -> Mover a -> Float -> Float -> Float
moveX timeDiff { x, s, d } x1 x2 =
clamp x1 x2 x + (xDelta timeDiff s d)
moveY : Float -> Mover a -> Float -> Float -> Float
moveY timeDiff { y, s, d } y1 y2 =
clamp y1 y2 y + (yDelta timeDiff s d)
accelerateMover : Float -> Mover a -> Float
accelerateMover timeDiff mover =
let
accFactor =
0.1
dragFactor =
0.01
in
clamp 2 mover.ts mover.s + (mover.acc * accFactor) - dragFactor
turnMover : Float -> Mover a -> Float
turnMover timeDiff mover =
normalize 0 360 mover.d + mover.turn * (5 * timeDiff)
moverView : Mover a -> String -> ( Float, Float ) -> Html msg
moverView mover img ( w, h ) =
let
angle =
toString mover.d
rx =
toString (mover.x - (w / 2))
ry =
toString (mover.y - (h / 2))
transforms =
"rotate("
++ angle
++ " "
++ (toString mover.x)
++ " "
++ (toString mover.y)
++ ")"
in
image
[ x rx
, y ry
, width (toString w)
, height (toString h)
, xlinkHref img
, transform transforms
]
[]