-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
281 lines (246 loc) · 8.35 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
module Main where
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Color exposing (..)
import Time exposing (..)
import Keyboard
import Mouse
import Window
import Debug
import Array
import Text exposing (Text)
import Util exposing (..)
import Ship exposing (Ship, initShip)
import Shot exposing (Shot, initShot)
-- CONSTANTS
shotMinY = -1
shotSize = { x = 0.005, y = 0.02 }
shotMaxY = 1
shotMoveRatio = 0.2
enemyMoveRatio = 0.02
playerMoveRatio = 0.3
shootDelay = 500 -- ms
waveDelay = 10000 -- ms
shipsPerWave = 8
translucentGray = rgba 0 0 0 0.5
groundLevel = 0.03
playerHeight = 0.03
initDifficulty = 10
difficultyStep = 10
-- MODEL
type GameState = Splash | Playing | GameOver
type alias World =
{ player : Ship
, enemies : List Ship
, shots : List Shot
, untilNextShot : Float
, untilNextWave : Float
, score : Int
, state : GameState
, difficulty : Int
}
-- initial values for records
initPlayer =
{ initShip | pos <- { x = 0, y = groundLevel + playerHeight/2 }
, size <- { x = 0.07, y = playerHeight } }
initWorld : World
initWorld =
{ player = initPlayer
, enemies = initEnemies initDifficulty
, difficulty = initDifficulty
, shots = []
, untilNextShot = 0
, untilNextWave = 0
, score = 0
, state = Splash
}
initEnemies : Int -> List Ship
initEnemies value =
let range = Array.toList (Array.initialize shipsPerWave identity)
createEnemy = (\n ->
{ initShip | pos <- { x = -0.5 + toFloat n / shipsPerWave, y = 1.025 }
, vel <- { x = 0, y = -(enemyMoveRatio * (1 + toFloat value / 100)) }
, size <- { x = 0.05, y = 0.025 }
, value <- value
})
in List.map createEnemy range
-- UPDATE
updatePlayer : (Float, Keys) -> Ship -> Ship
updatePlayer (dt, keys) ship =
let newVel = { x = toFloat keys.x * playerMoveRatio, y = 0 }
isShooting = keys.y > 0
in ship
|> Ship.applyPhysics dt
|> Ship.updateShooting isShooting
|> Ship.updateVel newVel
-- get tuple of given Ship, and the Shots that hit it (often an empty list)
hitTuple : List (Shot, Rectangle) -> (Ship, Rectangle) -> (Ship, List Shot)
hitTuple shotRectTuples shipRectTuple =
let (ship, shipR) = shipRectTuple
hits = shotRectTuples
|> List.map ( \(shot, shotR) ->
{ hit=overlap shotR shipR
, shot=shot } )
|> List.filter .hit
shots = List.map .shot hits
in (ship, shots)
-- get list of Ships that have collided, and the Shots they collided with
findCollisions : List Shot -> List Ship -> List (Ship, List Shot)
findCollisions shots ships =
let shotRectTuples =
List.map (\s -> (s, {center=s.pos, size=shotSize})) shots
shipRectTuples =
List.map (\s -> (s, {center=s.pos, size=s.size})) ships
in shipRectTuples
|> List.map (hitTuple shotRectTuples)
|> List.filter (\(ship, shots) -> List.length shots > 0)
updateEnemies : Float -> List Ship -> List Ship -> List Ship
updateEnemies dt enemies hitShips =
enemies
|> List.filter (\s -> not (List.member s hitShips))
|> List.map (Ship.applyPhysics dt)
playerShoot : Ship -> Float -> List Shot -> List Shot
playerShoot s untilNextShot shots =
let sp = s.pos
position = { sp | y <- sp.y + s.size.y / 2 }
in if s.shooting && untilNextShot <= 0
then { initShot | pos <- position
, vel <- { x = 0, y = 2 * shotMoveRatio } } :: shots
else shots
updateShots : Float -> List Shot -> List Shot -> List Shot
updateShots dt shots hitShots =
shots
|> List.filter (\s -> not (List.member s hitShots))
|> List.map (Shot.applyPhysics dt)
|> List.filter (\shot -> shot.pos.y < shotMaxY &&
shot.pos.y > shotMinY)
-- take dt, `Keys` and `World`, return next state
updatePlay : (Float, Keys) -> World -> World
updatePlay (dt, keys) world =
let dt' = dt / 1000
dead = List.any (\s -> s.pos.y <= groundLevel) world.enemies
state = if dead then GameOver else Playing
player = updatePlayer (dt', keys) world.player
collisions = findCollisions world.shots world.enemies
hitShips = List.map fst collisions
hitShots = List.concatMap snd collisions
enemies' = updateEnemies dt' world.enemies hitShips
shots' = playerShoot player world.untilNextShot world.shots
untilNextShot =
if List.length shots' == List.length world.shots
then world.untilNextShot - dt
else shootDelay
(enemies, untilNextWave, difficulty) =
if world.untilNextWave > 0
then ( enemies'
, world.untilNextWave - dt
, world.difficulty )
else ( enemies' ++ (initEnemies world.difficulty)
, waveDelay
, world.difficulty + difficultyStep )
shots = updateShots dt' shots' hitShots
score = List.foldl (\s m -> m + s.value) world.score hitShips
debug = Debug.watch "World" world
in { world | player <- player
, enemies <- enemies
, shots <- shots
, untilNextShot <- untilNextShot
, untilNextWave <- untilNextWave
, difficulty <- difficulty
, state <- state
, score <- score }
-- if Mouse is pressed, go to Playing state
updateWait : Bool -> World -> World
updateWait mouseDown world =
if not mouseDown
then world
else { initWorld | state <- Playing }
update : (Float, Keys, Bool) -> World -> World
update (dt, keys, mouseDown) world =
case world.state of
Splash -> updateWait mouseDown world
Playing -> updatePlay (dt, keys) world
GameOver -> updateWait mouseDown world
-- RENDER
fromBottom : Float -> Form -> Form
fromBottom r form =
move (0, r * -0.5) form
renderShot : Float -> Shot -> Form
renderShot r shot =
let (x, y) = (shot.pos.x, shot.pos.y)
in rect (r * shotSize.x) (r * shotSize.y)
|> filled translucentGray
|> fromBottom r
|> move (x * r, y * r)
renderShip : Float -> Ship -> List Form
renderShip r ship =
let (x, y) = (ship.pos.x, ship.pos.y)
(w, h) = (ship.size.x, ship.size.y)
dir = if ship.vel.y < 0 then -1 else 1
fillAndMove = \(xo, yo) f -> f
|> filled (rgb 128 128 128)
|> fromBottom r
|> move ((x + xo) * r, (y + yo) * r)
bodyH = h / 3
dotR = bodyH / 2
body =
rect (r * w) (r * bodyH) |> fillAndMove (0, dir * -dotR)
dots =
[ circle (r * dotR) |> fillAndMove (0, 0)
, circle (r * dotR) |> fillAndMove (-w/2, dir * -dotR)
, circle (r * dotR) |> fillAndMove (w/2, dir * -dotR)
]
in body :: dots
renderGame : (Float, Float) -> World -> List Form
renderGame (w', h') world =
let r = h'
bg =
rect w' h'
|> filled lightGray
ground =
rect w' (r * groundLevel * 2)
|> filled (rgb 70 40 80)
|> fromBottom r
player = renderShip r world.player
shots =
List.map (renderShot r) world.shots
enemies =
List.concatMap (renderShip r) world.enemies
in bg :: ground :: player ++ shots ++ enemies
formatText : String -> Form
formatText s =
Text.fromString s
|> Text.color black
|> Text.height 20
|> centered
|> toForm
splashText : Form
splashText =
formatText "Click to start.\nArrow keys to play."
gameoverText : Int -> Form
gameoverText score =
"Score: " ++ toString score ++ "\nClick to restart."
|> formatText
scoreText : Int -> (Float, Float) -> Form
scoreText score (w, h) =
"Score: " ++ toString score
|> formatText
|> move (80 - w/2, h/2 - 20)
render : (Int, Int) -> World -> Element
render (w, h) world =
let (w', h') = (toFloat w, toFloat h)
gameForms = renderGame (w', h') world
in case world.state of
Splash -> collage w h (gameForms ++ [splashText])
Playing -> collage w h (gameForms ++ [scoreText world.score (w', h')])
GameOver -> collage w h (gameForms ++ [gameoverText world.score])
-- SIGNALS
inputSignal : Signal (Float, Keys, Bool)
inputSignal =
let delta = fps 30
tuples = Signal.map3 (,,) delta Keyboard.arrows Mouse.isDown
in Signal.sampleOn delta tuples
world : Signal World
world = Signal.foldp update initWorld inputSignal
main : Signal Element
main = Signal.map2 render Window.dimensions world