-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroids.elm
385 lines (301 loc) · 10.1 KB
/
asteroids.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import Random exposing (Seed, Generator)
import Time exposing (Time)
import Keyboard
import Text
import Graphics.Collage exposing (outlined, collage, ngon, rect, filled, move,
Form, solid, group, toForm, polygon, rotate,
circle)
import Graphics.Element as Element exposing (Element)
import Color
import Signal
import Window
-- TODO
-- - Random new asteroids
-- - Explosions (with physics!)
-- - Better asteroid fragmentation/change of velocity for new small asteroids
-- MODEL
type State = Start | Play | Pause | Over
(gameWidth, gameHeight) = (600, 600)
(halfWidth, halfHeight) = (gameWidth / 2, gameHeight / 2)
type alias Mover a =
{ a |
location : (Float, Float), -- (x, y)
velocity: (Float, Float) -- (magnitude, direction)
}
type alias Player =
Mover { facing : Float, accelerating : Bool, cooldown : Int }
type AsteroidSize =
Small | Medium | Large
type alias Asteroid =
Mover { size : AsteroidSize }
type alias Bullet =
Mover { ttl : Float }
type alias Game =
{ state : State
, player : Player
, asteroids : List Asteroid
, bullets : List Bullet
}
rotationSpeed = -0.3
accelerationCoefficient = 40
bulletSpeed = 300
randomCoordinate : Generator (Float, Float)
randomCoordinate =
Random.pair (Random.float -halfWidth halfWidth) (Random.float -halfHeight halfHeight)
randomVelocity : Generator (Float, Float)
randomVelocity =
Random.pair (Random.float -200 200) (Random.map degrees <| Random.float 0 360)
randomSize : Generator AsteroidSize
randomSize =
Random.map (\i -> case i of
1 -> Small
2 -> Medium
_ -> Large) (Random.int 1 3)
asteroid : (Float, Float) -> (Float, Float) -> AsteroidSize -> Asteroid
asteroid randLoc randV randSize =
{location = randLoc, velocity = randV, size = randSize}
randomAsteroid : Generator Asteroid
randomAsteroid =
Random.map3 asteroid randomCoordinate randomVelocity randomSize
timeSeed : Time -> Seed
timeSeed t = (Random.initialSeed << round) t
newGame : Game
newGame =
{ state = Start
, player = { location = (0, 0)
, facing = 0
, accelerating = False
, velocity = (0, 0)
, cooldown = 0 }
, asteroids = []
, bullets = []
}
type alias Input =
{ delta : Time
, pauseKey : Bool
, fireKey : Bool
, arrows : {x : Int, y : Int}
}
-- UPDATE
wrapScalar : Float -> Float -> Float
wrapScalar x max =
if x > max then
-max
else if x < -max then
max
else
x
physicsUpdate : Time -> Mover a -> Mover a
physicsUpdate t ({location, velocity} as obj) =
let dt = t
(x, y) = location
(r, theta) = velocity
(dy, dx) = (r * sin theta, r * cos theta)
newX = x + dt * dx
newY = y + dt * dy
in
{ obj |
location = (wrapScalar newX halfWidth,
wrapScalar newY halfHeight)
}
randomAsteroids : Seed -> List Asteroid
randomAsteroids seed =
let (result, _) = Random.generate (Random.list 5 randomAsteroid) seed
in
result
addVelocities : (Float, Float) -> (Float, Float) -> (Float, Float)
addVelocities v1 v2 =
let (x1, y1) = Debug.log "x1, y1" <| fromPolar v1
(x2, y2) = Debug.log "x2, y2" <| fromPolar v2
in
toPolar (x1 + x2, y1 + y2)
updatePlayer : Input -> Game -> Game
updatePlayer {delta, arrows} ({player} as game) =
let newFacing = player.facing + rotationSpeed * (toFloat arrows.x)
deltaV = (delta * accelerationCoefficient, newFacing)
accelerating = if arrows.y == 1 then True else False
setNewFields = (\player ->
{ player |
facing = newFacing,
velocity = if accelerating then
addVelocities player.velocity deltaV
else
player.velocity,
accelerating = accelerating
})
in
{ game |
player = physicsUpdate delta (setNewFields player)
}
newBullet : Game -> Bullet
newBullet {player} =
{ location = player.location
, velocity = (bulletSpeed, player.facing)
, ttl = 1.5
}
addBulletIfNeeded : Input -> Game -> Game
addBulletIfNeeded input ({player} as game) =
let cooldown = game.player.cooldown
decrementCooldown = (\player ->
if player.cooldown > 0 then
{ player |
cooldown = player.cooldown - 1
}
else
player)
in
if input.fireKey == True && cooldown == 0 then
{ game |
bullets = newBullet game :: game.bullets,
player = { player | cooldown = 5}
}
else
{ game | player = decrementCooldown game.player }
near : Float -> (Float, Float) -> (Float, Float) -> Bool
near radius (x1, y1) (x2, y2) =
sqrt ((x1 - x2)^2 + (y1 - y2)^2) < radius
playerHasCollided : Game -> Bool
playerHasCollided ({asteroids, player} as game) =
List.any (\a -> near 20 player.location a.location) asteroids
updateState : Game -> Game
updateState game =
{ game | state = if playerHasCollided game then Over else game.state }
rotateAsteroidCourse : Float -> Asteroid -> Asteroid
rotateAsteroidCourse rotation ({velocity} as asteroid) =
let (speed, angle) = velocity
newAngle = angle + rotation
in
{ asteroid |
velocity = (speed, newAngle)
}
fragmentAsteroid : Asteroid -> List Asteroid
fragmentAsteroid a =
let childAsteroids = (\newSize ->
[rotateAsteroidCourse -15 { a | size = newSize},
rotateAsteroidCourse 1 {a | size = newSize}])
in
case a.size of
Small -> []
Medium -> childAsteroids Small
Large -> childAsteroids Medium
objectShouldBeRemoved : List (Mover a) -> Mover b -> Bool
objectShouldBeRemoved targets object =
List.any (\target ->
(near 20 object.location target.location)) targets
fragmentShotAsteroids : Game -> Game
fragmentShotAsteroids game =
let collidedAsteroids =
List.filter (objectShouldBeRemoved game.bullets) game.asteroids
uncollidedAsteroids =
List.filter (not << (objectShouldBeRemoved game.bullets)) game.asteroids
in
{ game |
asteroids =
uncollidedAsteroids ++ List.concatMap fragmentAsteroid collidedAsteroids,
bullets =
List.filter (not << (objectShouldBeRemoved game.asteroids)) game.bullets
}
updateAsteroids : Time -> Game -> Game
updateAsteroids delta game =
{ game |
asteroids = game.asteroids
|> List.map (physicsUpdate delta)
} |> fragmentShotAsteroids
updateBullets : Time -> Game -> Game
updateBullets delta game =
let decrementTtl = (\bullet -> { bullet | ttl = bullet.ttl - delta })
in
{ game |
bullets = game.bullets
|> (List.map <| (physicsUpdate delta >> decrementTtl))
|> List.filter (\bullet -> bullet.ttl > 0)
}
update : Input -> Game -> Game
update ({delta, pauseKey, arrows} as input) ({player} as game) =
case game.state of
Start -> { game |
asteroids = randomAsteroids (timeSeed delta),
state = Pause
}
Over -> if pauseKey then newGame else game
Pause -> if pauseKey then {game | state = Play} else game
Play -> updateState game
|> updateAsteroids delta
|> updateBullets delta
|> updatePlayer input
|> addBulletIfNeeded input
-- VIEW
renderBullets : List Bullet -> List Form
renderBullets bullets =
List.map (\b -> circle 3 |> filled Color.white |> move b.location) bullets
renderAsteroid : Asteroid -> Form
renderAsteroid asteroid =
let size = case asteroid.size of
Small -> 7
Medium -> 14
Large -> 20
in
outlined (solid Color.white) (ngon 5 size) |> move asteroid.location
renderThrust : Player -> Maybe Form
renderThrust {facing, accelerating} =
case accelerating of
True ->
Just (group [(filled Color.orange (polygon [(-10, 5),
(-18, 0),
(-10, -5)])),
(filled Color.yellow (polygon [(-10, 5),
(-15, 0),
(-10, -5)])),
(filled Color.lightBlue (polygon [(-10, 5),
(-13, 0),
(-10, -5)]))])
False ->
Nothing
renderShip : Player -> Form
renderShip ({location, facing} as player) =
let ship = filled Color.white (polygon [(-10, -10), (15, 0), (-10, 10)])
shipWithThrust = case renderThrust player of
Just thrust -> group [ship, thrust]
Nothing -> ship
in
shipWithThrust
|> move location
|> rotate facing
view : (Int, Int) -> Game -> Element
view (w, h) game =
let state = txt identity (if game.state == Play then
""
else if game.state == Pause then
"Paused (ENTER to start)"
else "Game Over")
in
Element.container w h Element.middle <|
collage gameWidth gameHeight
-- Background
[ rect gameWidth gameHeight |> filled Color.black
-- Paused indicator
, toForm state |> move (0, 0)
-- Player's ship
, renderShip game.player
-- Asteroids
, group (List.map renderAsteroid game.asteroids)
, group (renderBullets game.bullets)]
txt f string =
Text.fromString string
|> Text.color Color.white
|> Text.monospace
|> f
|> Element.leftAligned
-- SIGNALS
main = Signal.map2 view Window.dimensions gameState
gameState : Signal Game
gameState = Signal.foldp update newGame input
delta = Signal.map Time.inSeconds (Time.fps 35)
input : Signal Input
input =
Signal.sampleOn delta <|
Signal.map4 Input
delta
Keyboard.enter
Keyboard.space
Keyboard.arrows