-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
356 lines (273 loc) · 7.17 KB
/
Main.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
module Main exposing (..)
import Random
import AnimationFrame
import Char
import Html
import Keyboard
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Time exposing (Time)
type alias Coords =
{ x : Float
, y : Float
}
type alias Player =
{ position : Coords
, velocity : Coords
}
type KeyState
= Pressed
| Unpressed
type Key
= Up
| Down
| Left
| Right
type alias Keys =
{ up : KeyState
, down : KeyState
, left : KeyState
, right : KeyState
}
type alias Asteroid =
{ position : Coords
, velocity : Coords
}
type alias Model =
{ player : Player
, keys : Keys
, asteroids : List Asteroid
, isGameOver : Bool
}
type Msg
= Noop
| KeyChange Key KeyState
| TimeStep Time
| ResetGame
| AddRoids (List Asteroid)
positionGenerator =
Random.float (-1 * mapSize) mapSize
velocityGenerator =
Random.float -5 5
asteroidGenerator : Random.Generator Asteroid
asteroidGenerator =
Random.map2 Asteroid
(Random.map2 Coords positionGenerator positionGenerator)
(Random.map2 Coords velocityGenerator velocityGenerator)
init : ( Model, Cmd Msg )
init =
( { player =
{ position = { x = 10, y = 10 }
, velocity = { x = 0, y = 0 }
}
, keys =
{ up = Unpressed
, down = Unpressed
, left = Unpressed
, right = Unpressed
}
, asteroids =
[]
, isGameOver = False
}
, Random.generate AddRoids (Random.list 5 asteroidGenerator)
)
mapSize =
100.0
viewBackground =
rect
[ x "0"
, y "0"
, width (toString mapSize)
, height (toString mapSize)
, rx "0"
, ry "0"
]
[]
viewAsteroid asteroid =
circle
[ cx (toString asteroid.position.x)
, cy (toString asteroid.position.y)
, r "5"
, Svg.Attributes.style "fill:red"
]
[]
viewAsteroids asteroids =
g [] (List.map viewAsteroid asteroids)
viewPlayer player =
circle
[ cx (toString player.position.x)
, cy (toString player.position.y)
, r "5"
, Svg.Attributes.style "fill:white"
]
[]
view : Model -> Html.Html Msg
view model =
Html.div []
[ if model.isGameOver then
Html.h1 [] [ text "GAME OVER" ]
else
svg
[ width "800"
, height "600"
, viewBox "0 0 120 120"
]
[ viewBackground
, viewAsteroids model.asteroids
, viewPlayer model.player
]
]
handleKeyChange : Key -> KeyState -> Model -> Model
handleKeyChange key keystate model =
let
keys =
model.keys
newKeys =
case key of
Up ->
{ keys | up = keystate }
Down ->
{ keys | down = keystate }
Left ->
{ keys | left = keystate }
Right ->
{ keys | right = keystate }
in
{ model | keys = newKeys }
handleTimeStep : Time -> Model -> Model
handleTimeStep diff model =
let
player =
model.player
millis =
Time.inSeconds diff
factor =
100
dampingFactor =
factor / 10
damp x =
-1 * (sign x) * dampingFactor
sign x =
if x == 0 then
x
else
x / (abs x)
a =
{ y =
case ( model.keys.up, model.keys.down ) of
( Pressed, Pressed ) ->
0
( Unpressed, Unpressed ) ->
damp model.player.velocity.y
( Pressed, _ ) ->
-1 * factor
( _, Pressed ) ->
factor
, x =
case ( model.keys.left, model.keys.right ) of
( Pressed, Pressed ) ->
0
( Unpressed, Unpressed ) ->
damp model.player.velocity.x
( Pressed, _ ) ->
-1 * factor
( _, Pressed ) ->
factor
}
maxV =
mapSize
updateVelocity v =
{ x =
(a.x * millis + v.x)
|> clamp (-1 * maxV) maxV
, y =
(a.y * millis + v.y)
|> clamp (-1 * maxV) maxV
}
updatePosition p v =
{ x = v.x * millis + p.x, y = v.y * millis + p.y }
wrapCoord p =
if p > mapSize then
p - mapSize
else if p < 0 then
p + mapSize
else
p
wrapPosition p =
{ x = wrapCoord p.x
, y = wrapCoord p.y
}
newVelocity =
updateVelocity model.player.velocity
newPosition =
updatePosition model.player.position newVelocity
|> wrapPosition
newPlayer =
{ player | velocity = newVelocity, position = newPosition }
overlaps a b =
(sqrt (((a.x - b.x) ^ 2) + ((a.y - b.y) ^ 2))) < 10
in
{ model
| player = newPlayer
, isGameOver =
List.foldl
(\a -> (||) (overlaps a.position newPosition))
model.isGameOver
model.asteroids
, asteroids =
List.map
(\a ->
{ a
| position =
updatePosition a.position a.velocity
|> wrapPosition
}
)
model.asteroids
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
KeyChange key keystate ->
handleKeyChange key keystate model ! []
TimeStep diff ->
handleTimeStep diff model ! []
ResetGame ->
init
Noop ->
model ! []
AddRoids roids ->
{ model | asteroids = roids } ! []
keyCodeToMsg : KeyState -> Keyboard.KeyCode -> Msg
keyCodeToMsg keystate keyCode =
case keyCode of
40 ->
KeyChange Down keystate
38 ->
KeyChange Up keystate
39 ->
KeyChange Right keystate
37 ->
KeyChange Left keystate
13 ->
if keystate == Pressed then
ResetGame
else
Noop
_ ->
Noop
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Keyboard.downs (keyCodeToMsg Pressed)
, Keyboard.ups (keyCodeToMsg Unpressed)
, AnimationFrame.diffs TimeStep
]
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}