-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.elm
62 lines (41 loc) · 1.21 KB
/
Player.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
module Player where
import Graphics.Collage exposing (LineStyle, Path, Form, traced, path, move, rotate, defaultLine)
import Color exposing (..)
import Types exposing (Player, Input)
import Helpers exposing (..)
-- CONSTANTS
playerAcceleration : Float
playerAcceleration = 0.15
playerRotationSpeed : Float
playerRotationSpeed = 0.07
-- INIT
init : Player
init =
{ position = (0,0)
, speed = (0,0)
, angle = 0.0
}
-- UPDATE
update : Player -> Input -> Player
update player input =
let newSpeed =
if input.dir.y > 0
then (0, playerAcceleration) |> rotvec player.angle |> addvec player.speed
else player.speed
in
{ player | angle = player.angle - (toFloat input.dir.x)*playerRotationSpeed*input.dt
, position = addvec player.position newSpeed |> wrapAround
, speed = newSpeed
}
-- GRAPHICS
playerLineStyle : LineStyle
playerLineStyle =
{ defaultLine | color = white
, width = 1
}
playerShape : Path
playerShape = path [ (0, 9), (7, -9), (0, -5), (-7, -9), (0, 9) ]
draw : Player -> Form
draw p = traced playerLineStyle playerShape
|> rotate p.angle
|> move p.position