-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHexagon.elm
More file actions
222 lines (165 loc) · 3.94 KB
/
Hexagon.elm
File metadata and controls
222 lines (165 loc) · 3.94 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
module Main exposing (..)
import Time exposing (..)
import List exposing (..)
import AnimationFrame
import Keyboard.Extra exposing (Key(..))
import Window
import Collage exposing (..)
import Element exposing (..)
import Color exposing (..)
import Html exposing (Html)
-- MODEL
type alias Player =
{ angle : Float }
type Direction
= Left
| Right
| Still
type alias Game =
{ player : Player
, direction : Direction
, pressedKeys : List Key
}
type Msg
= Step Time
| KeyboardMsg Keyboard.Extra.Msg
( gameWidth, gameHeight ) =
( 1024, 576 )
( halfWidth, halfHeight ) =
( gameWidth / 2, gameHeight / 2 )
( iHalfWidth, iHalfHeight ) =
( gameWidth // 2, gameHeight // 2 )
playerRadius : Float
playerRadius =
gameWidth / 10.0
playerSize : Float
playerSize =
10.0
playerSpeed : Float
playerSpeed =
0.12
bgBlack : Color
bgBlack =
rgb 20 20 20
-- UPDATE
updatePlayerAngle : Float -> Direction -> Float
updatePlayerAngle angle dir =
let
sign =
if dir == Left then
1
else if dir == Right then
-1
else
0
newAngle =
angle + toFloat sign * playerSpeed
in
if newAngle < 0 then
newAngle + 2 * pi
else if newAngle > 2 * pi then
newAngle - 2 * pi
else
newAngle
updatePlayer : Direction -> Game -> Player
updatePlayer dir { player } =
let
newAngle =
updatePlayerAngle player.angle dir
in
{ player | angle = newAngle }
{-| Updates the game state on a keyboard command
-}
onUserInput : Keyboard.Extra.Msg -> Game -> ( Game, Cmd Msg )
onUserInput keyMsg game =
let
pressedKeys =
Keyboard.Extra.update keyMsg game.pressedKeys
direction =
if (Keyboard.Extra.arrows pressedKeys).x < 0 then
Left
else if (Keyboard.Extra.arrows pressedKeys).x > 0 then
Right
else
Still
in
( { game
| pressedKeys = pressedKeys
, direction = direction
}
, Cmd.none
)
{-| Updates the game state on every frame
-}
onFrame : Time -> Game -> ( Game, Cmd Msg )
onFrame time game =
let
nextCmd =
Cmd.none
in
( { game
| player = updatePlayer game.direction game
}
, nextCmd
)
{-| Game loop: Transition from one state to the next.
-}
update : Msg -> Game -> ( Game, Cmd Msg )
update msg game =
case msg of
KeyboardMsg keyMsg ->
onUserInput keyMsg game
Step time ->
onFrame time game
-- VIEW
moveRadial : Float -> Float -> Form -> Form
moveRadial angle radius =
move ( radius * cos angle, radius * sin angle )
makePlayer : Player -> Form
makePlayer player =
let
angle =
player.angle - degrees 30
in
ngon 3 playerSize
|> filled (hsl angle 1 0.5)
|> moveRadial angle (playerRadius - playerSize)
|> rotate angle
view : Game -> Html.Html Msg
view game =
let
bg =
rect gameWidth gameHeight |> filled bgBlack
field =
makePlayer game.player
in
toHtml
<| container gameWidth gameHeight middle
<| collage gameWidth
gameHeight
[ bg
, field
]
-- SUBSCRIPTIONS
subscriptions : Game -> Sub Msg
subscriptions game =
Sub.batch
[ AnimationFrame.times (\time -> Step time)
, Sub.map KeyboardMsg Keyboard.Extra.subscriptions
]
--INIT
init : ( Game, Cmd Msg )
init =
( { player = Player (degrees 30)
, pressedKeys = []
, direction = Still
}
, Cmd.none
)
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}