/
Show.elm
318 lines (269 loc) · 8.59 KB
/
Show.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
module Show (..) where
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Color exposing (..)
import Random
import Time
import Text
import String
import Helpers exposing (..)
import Definitions exposing (..)
import Types exposing (..)
view : Game -> Element
view g =
case g of
Start ->
collage
gameWidth
gameHeight
[ background
, titleText
]
Level level ->
collage
gameWidth
gameHeight
[ background
, showShots <| level.shots
, showPlayer level.accelerating <| level.player
, showAsteroids <| level.asteroids
, move ( -360, 285 ) << toForm << leftAligned << Text.color white << Text.fromString << String.concat <| [ "Lives: ", toString level.lives ]
, move ( -360, 265 ) << toForm << leftAligned << Text.color white << Text.fromString << String.concat <| [ "Score: ", toString level.score ]
]
GameOver gameOver ->
collage
gameWidth
gameHeight
[ background
, endText gameOver.level gameOver.score
]
Pause pause ->
collage
gameWidth
gameHeight
[ background
, pauseText pause.level pause.score
]
End end ->
collage
gameWidth
gameHeight
[ background
, finishedText end.score
]
polygonPlayer : List ( Float, Float )
polygonPlayer =
[ ( 0, -10 ), ( 10, -15 ), ( 0, 15 ), ( -10, -15 ) ]
polygonPlayerAcc : List ( Float, Float )
polygonPlayerAcc =
polygonPlayer ++ [ ( -4, -12 ), ( 0, -15 ), ( 4, -12 ), ( 0, -15 ), ( -4, -12 ) ]
polygonAsteroid : List (List ( Float, Float ))
polygonAsteroid =
[ [ ( 2, 4 ), ( 4, 4 ), ( 6, 6 ), ( 10, 2 ), ( 8, -6 ), ( 4, -6 ), ( 0, -10 ), ( -4, -10 ), ( -10, -2 ), ( -8, 4 ), ( -2, 4 ), ( -4, 8 ), ( 2, 8 ) ]
, [ ( 0, 10 ), ( 2, 6 ), ( 6, 8 ), ( 8, 4 ), ( 6, 2 ), ( 10, 0 ), ( 6, -8 ), ( 2, -10 ), ( -2, -8 ), ( -6, -8 ), ( -10, -2 ), ( -10, 0 ), ( -8, 9 ) ]
]
polygonAsteroid_length = List.length polygonAsteroid
{- cornerShow : Form -> Form
cornerShow =
\f ->
group <<
List.map
(\(x,y) ->
move (x,y) f
) <|
[ (0, 0)
, (0, gameHeight)
, (0, -gameHeight)
, (gameWidth, 0)
, (gameWidth, gameHeight)
, (gameWidth, -gameHeight)
, (-gameWidth, 0)
, (-gameWidth, gameHeight)
, (-gameWidth, -gameHeight)
]
-}
{-
Similar to cornerCollision from the module Collision this function is needed to show items on the edges and in the corners of the playing field.
It uses a similar approach to this problem of our field not really being a torus but unlike cornerCollision it draws only those instances of objects that are somewhat close to the visible field.
This makes the function even more ugly ( ;) ) but seems to improve the performance a bit in cases with many objects. No exensive testing was done though.
A stupid, cornerCollision-like version can be found above.
-}
cornerShow : ( Float, Float ) -> Float -> Form -> Form
cornerShow ( x, y ) r f =
group
<< List.map
(\( x, y ) ->
move ( x - gameWidth / 2, y - gameHeight / 2 ) f
)
<< List.filter
(\( x, y ) ->
List.any identity
<< List.map
(\( x, y ) ->
x >= 0 && x <= gameWidth && y >= 0 && y <= gameHeight
)
<| [ ( x + r, y + r )
, ( x + r, y - r )
, ( x - r, y + r )
, ( x - r, y - r )
]
)
<| [ ( x, y )
, ( x, y + gameHeight )
, ( x, y - gameHeight )
, ( x + gameWidth, y )
, ( x + gameWidth, y + gameHeight )
, ( x + gameWidth, y - gameHeight )
, ( x - gameWidth, y )
, ( x - gameWidth, y + gameHeight )
, ( x - gameWidth, y - gameHeight )
]
-- Visual representation of the player through a polygon
showPlayer : Bool -> Player -> Form
showPlayer accelerating p =
let
player' = if accelerating then polygonPlayerAcc else polygonPlayer
player = polygon player'
in
cornerShow ( p.x, p.y ) playerRadius
<< rotate (0 - p.angle)
<< group
<< List.map
(\f ->
f player
)
<| [ filled black
-- to hide items BEHIND this one
, outlined (solid white)
]
-- Visual representation of an Asteroid through one of multiple polygons.
showAsteroids : List Asteroid -> Form
showAsteroids =
group
<< List.map showAsteroid
showAsteroid : Asteroid -> Form
showAsteroid a =
let
asteroid = polygon << List.map (\(x,y) -> ( (toFloat a.size + 1) * x, (toFloat a.size + 1) * y)) <| index polygonAsteroid (a.form % polygonAsteroid_length) []
in
cornerShow ( a.x, a.y ) (toFloat a.size + 1 * asteroidRadius)
<< rotate a.angle
<< group
<< List.map
(\f ->
f asteroid
)
<| [ filled black
-- to hide items BEHIND this one
, outlined (solid white)
]
-- No polygons for shots, they are simple, small circles
showShots : List Shot -> Form
showShots =
let
shot = outlined (solid white) <| circle shotRadius
in
group
<< List.map
(\s ->
cornerShow ( s.x, s.y ) shotRadius shot
)
-- Initially I wanted to make a dynamic background (e.g. through 100 random, small circles). This is not possible as seemingly 100 objects are too much to handle for the engine...
-- Instead I use a static background graphic
background : Form
background =
toForm
<| image gameWidth gameHeight "graphics/background.png"
titleText : Form
titleText =
group
[ move ( 0, 50 )
<< scale 4
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<| "Asteroids"
, move ( 0, 0 )
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<| "in Elm"
, move ( 0, -250 )
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<| "Press W to accelerate, A and D to turn and Space to fire.\nPress Space to start."
]
pauseText : Int -> Int -> Form
pauseText l s =
group
[ move ( 0, 50 )
<< scale 4
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<| "Level complete."
, move ( 0, -40 )
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<< String.concat
<| [ "Level "
, toString l
, " finished.\nPress Space to continue."
, "\nYour score is "
, toString s
, "!"
]
]
endText : Int -> Int -> Form
endText l s =
group
[ move ( 0, 50 )
<< scale 4
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<| "Game Over!"
, move ( 0, -40 )
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<< String.concat
<| [ "You reached level "
, toString l
, " and score "
, toString s
, ".\nPress Space to restart."
]
]
finishedText : Int -> Form
finishedText s =
group
[ move ( 0, 50 )
<< scale 4
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<| "You won!"
, move ( 0, -40 )
<< toForm
<< centered
<< Text.color white
<< Text.fromString
<< String.concat
<| [ "You finished all levels."
, "\nYour score is "
, toString s
, "!"
--, "\nPress Space to restart."
]
]