-
Notifications
You must be signed in to change notification settings - Fork 0
/
ld29.elm
411 lines (331 loc) · 12.1 KB
/
ld29.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
module Ld29 where
import Keyboard
import Time
import Graphics.Collage exposing (..)
import Graphics.Element exposing (show,Element)
import Color exposing (..)
import Text
--MODELS AND INPUTS
type alias Input = {space:Bool, dx:Int, dy:Int, dt:Time.Time}
type alias GameObject a = {a | x:Float, y:Float, vx:Float, vy:Float, angle:Float, rev:Bool}
type alias Player = GameObject {}
type EnemyType = Small | Medium | Large
type alias Enemy = GameObject {size:EnemyType}
type alias Bullet = GameObject {age:Float,active:Bool}
type GameState = Play | Win | Lose | Begin | Victory
type Collision = LeftRight | TopBottom
type alias Surface a = GameObject a -> GameObject a
type SurfaceType = Rectangle | Cylinder | Torus | Mobius | Klein | Chaosphere
type alias Game = {state:GameState, player:Player, surface:SurfaceType, enemies:List Enemy, bullet:Bullet}
defaultGame : Game
defaultGame = { state = Begin,
player = defaultPlayer,
surface = Rectangle,
enemies = defaultEnemies,
bullet = defaultBullet}
defaultPlayer : Player
defaultPlayer = {x = 0, y = 0, vx = 0, vy = 0, angle = 0, rev = False}
defaultEnemy1 : Enemy
defaultEnemy1 = {x = 50, y = 50, vx = 50, vy = 50, angle = 0, rev = False, size = Large}
defaultEnemy2 : Enemy
defaultEnemy2 = {defaultEnemy1 | y = -50, vy = -50}
defaultEnemy3 : Enemy
defaultEnemy3 = {defaultEnemy2 | x = -50, vx = -50}
defaultEnemy4 : Enemy
defaultEnemy4 = {defaultEnemy3 | y = 50, vy = 50}
defaultEnemies : List Enemy
defaultEnemies = [defaultEnemy1, defaultEnemy2, defaultEnemy3, defaultEnemy4]
testEnemies : List Enemy
testEnemies = [defaultEnemy1]
defaultBullet : Bullet
defaultBullet = {x = 0, y = 0, vx = 0, vy = 0, angle = 0, rev = False, age = 0,active = False}
delta : Signal Time.Time
delta = Signal.map Time.inSeconds <| Time.fps 60
input : Signal Input
input =
let s = Keyboard.space
x = Signal.map .x Keyboard.arrows
y = Signal.map .y Keyboard.arrows
in Signal.map4 Input s x y delta
--Constants
thrustFactor : Float
thrustFactor = 2
outerXMax : Float
outerXMax = 380
innerXMax : Float
innerXMax = 335
outerYMax : Float
outerYMax = 280
innerYMax : Float
innerYMax = 235
--UPDATE SECTION
--Helper functions
doV : List Collision -> Bool
doV cols = List.any (\n -> n == TopBottom) cols
doH : List Collision -> Bool
doH cols = List.any (\n -> n == LeftRight) cols
reflectAngle : Bool -> Float -> Float
reflectAngle yaxis a =
if yaxis then -a
else 180 - a
findDistance : Float -> Float -> Float -> Float -> Float
findDistance ax ay bx by =
let x' = ax - bx
y' = ay - by
in sqrt (x'^2 + y'^2)
enemySize : EnemyType -> Float
enemySize t =
case t of
Small -> 18
Medium -> 30
Large -> 45
nextSurface : SurfaceType -> SurfaceType
nextSurface s =
case s of
Rectangle -> Cylinder
Cylinder -> Mobius
Mobius -> Torus
Torus -> Klein
Klein -> Chaosphere
Chaosphere -> Rectangle
getSurface : SurfaceType -> Surface a
getSurface s =
case s of
Rectangle -> rectangle
Cylinder -> cylinder
Mobius -> mobius
Torus -> torus
Klein -> klein
Chaosphere -> chaosphere
--Surface definitions
rectangle : Surface a
rectangle = collideRect [LeftRight,TopBottom]
cylinder : Surface a
cylinder = collideCyl [LeftRight] << collideRect [TopBottom]
mobius : Surface a
mobius = collideMobius [LeftRight] << collideRect [TopBottom]
torus : Surface a
torus = collideCyl [LeftRight,TopBottom]
klein : Surface a
klein = collideMobius [LeftRight] << collideCyl [TopBottom]
chaosphere : Surface a
chaosphere = collideMobius [LeftRight,TopBottom]
--Border collision functions
collideRect : List Collision -> Surface a
collideRect cols ({x,y,vx,vy,angle,rev} as p) =
let collidingH = abs x >= innerXMax
collidingV = abs y >= innerYMax
doH' = collidingH && doH cols
doV' = collidingV && doV cols
in {p | vx = if doH' then -vx else vx
, vy = if doV' then -vy else vy
, x = if doH' then clamp -innerXMax innerXMax x else x
, y = if doV' then clamp -innerYMax innerYMax y else y }
collideCyl : List Collision -> Surface a
collideCyl cols ({x,y,vx,vy,angle,rev} as p) =
let collidingH = abs x >= outerXMax
collidingV = abs y >= outerYMax
in {p | x = if collidingH && doH cols then -x else x
, y = if collidingV && doV cols then -y else y }
collideMobius : List Collision -> Surface a
collideMobius cols ({x,y,vx,vy,angle,rev} as p) =
let collidingH = abs x >= outerXMax
collidingV = abs y >= outerYMax
doV' = collidingV && doV cols
doH' = collidingH && doH cols
doMove = doH' || doV'
in {p | x = if doMove then -x else x
, y = if doMove then -y else y
, vx = if doV' then -vx else vx
, vy = if doH' then -vy else vy
, angle = if doMove then reflectAngle doV' angle else angle
, rev = if doMove then not rev else rev}
--Object collision functions
checkCollision : GameObject a -> Enemy -> Bool
checkCollision obj e = findDistance e.x e.y obj.x obj.y <= enemySize e.size
mapPlayerCollisions : Player -> List Enemy -> Bool
mapPlayerCollisions p es =
let bools = List.map (checkCollision p) es
in List.any (\ n -> n) bools
mapBulletCollisions : Bullet -> List Enemy -> (List Enemy,Bool)
mapBulletCollisions b es =
let bools = List.map (checkCollision b) es
b' = List.any (\ n -> n) bools
es' = List.concat <| List.map handleBulletCollision <| List.map2 (,) es bools
in (es',b')
handleBulletCollision : (Enemy,Bool) -> List Enemy
handleBulletCollision (e,b) =
if b && e.size == Small then
[]
else if b then
splitEnemy e
else
[e]
splitEnemy : Enemy -> List Enemy
splitEnemy e =
let newsize = if e.size == Large then Medium else Small
dx = if e.vx > 0 then 15 else -15
dy = if e.vy > 0 then 15 else -15
e1 = {e | vx = e.vx + dx
, vy = e.vy - dy
, size = newsize }
e2 = {e | vx = e.vx - dx
, vy = e.vy + dy
, size = newsize }
in [e1,e2]
--Movement functions
applyThrust : Bool -> Float -> Player -> Player
applyThrust active dt ({x,y,vx,vy,angle,rev} as p) =
let
vxA = if active
then thrustFactor*(sin (degrees angle))
else 0
vyA = if active
then thrustFactor*(cos (degrees angle))
else 0
vx' = clamp -200 200 (vx - vxA)
vy' = clamp -200 200 (vy + vyA)
in {p | vx = vx'
, vy = vy'
, x = movePlayer dt x vx -outerXMax outerXMax
, y = movePlayer dt y vy -outerYMax outerYMax }
enemyMovement : Float -> Enemy -> Enemy
enemyMovement dt e =
{ e| x = movePlayer dt e.x e.vx -outerXMax outerXMax
, y = movePlayer dt e.y e.vy -outerYMax outerYMax }
movePlayer : Float -> Float -> Float -> Float -> Float -> Float
movePlayer dt x vx xmin xmax = clamp xmin xmax (x + vx * dt)
--Update functions
createBullet : Player -> Bullet
createBullet ({x,y,vx,vy,angle,rev} as p) =
let vx' = -(sin (degrees angle) * 200)
vy' = cos (degrees angle) * 200
in {x = p.x, y = p.y, vx = vx', vy = vy', angle = p.angle, rev = p.rev, age = 0, active = True}
stepBullet : Surface {age:Float,active:Bool} -> Input -> Bullet -> Bullet
stepBullet surface ({space,dx,dy,dt} as i) ({x,y,vx,vy,angle,rev,age,active} as b) =
let active' = active && age <= 2
age' = if active' then age + dt else 0
b' = { b | x = movePlayer dt x vx -outerXMax outerXMax
, y = movePlayer dt y vy -outerYMax outerYMax }
b'' = surface b'
in {b'' | age = age'
, active = active'}
stepEnemy : Surface {size:EnemyType} -> Input -> Enemy -> Enemy
stepEnemy surface ({space,dx,dy,dt} as i) ({x,y,vx,vy,angle,rev,size} as e) =
let angle' = if rev then angle + (dt * 50) else angle - (dt * 50)
e' = enemyMovement dt e
e'' = surface e'
in { e'' | angle = angle', size = size}
stepPlayer : Surface {} -> Input -> Player -> Player
stepPlayer surface ({space,dx,dy,dt} as i) ({x,y,vx,vy,angle,rev} as p) =
let p' = surface <| applyThrust (dy == 1) dt p
dx' = if rev then -dx else dx
in {p' | angle = p'.angle - (toFloat dx' * dt * 100)}
stepGame : Input -> Game -> Game
stepGame ({space,dx,dy,dt} as i) ({state,player,surface,enemies,bullet} as g) =
let stuck = abs player.x == outerXMax && abs player.y == outerYMax
p' = if stuck then {player | x = 0, y =0 } else player
b' = if not bullet.active && space && state == Play
then createBullet player
else bullet
bulletCollision = if bullet.active then mapBulletCollisions bullet enemies else (enemies,False)
b'' = if not (snd bulletCollision) then b'
else { b' | active = False }
e' = fst bulletCollision
playerCollision = mapPlayerCollisions p' e'
state' = if List.length e' == 0 then Win
else if playerCollision then Lose
else state
p'' = if state' == Play then stepPlayer (getSurface surface) i p'
else if state' /= Play && space then defaultPlayer
else p'
e'' = if state' == Play then List.map (stepEnemy (getSurface surface) i) e'
else if state' /= Play && space then defaultEnemies
else e'
b''' = if state' == Play then stepBullet (getSurface surface) i b'' else { b'' | active = False }
nextSurf = nextSurface surface
surface' = if state' == Win && space then nextSurf else surface
state'' = if state' /= Play && space then Play
else if state' == Win && surface' == Chaosphere then Victory
else state'
in {g | player = p''
, enemies = e''
, bullet = b'''
, surface = surface'
, state = state'' }
gameState : Signal Game
gameState = Signal.foldp stepGame defaultGame input
--DRAWING SECTION
background : Form
background = rect 800 600 |> filled black
foreground : Form
foreground = group
[ rect 50 600 |> filled white
|> move (-375,0)
, rect 50 600 |> filled white
|> move (375, 0)
, rect 800 50 |> filled white
|> move (0,-275)
, rect 800 50 |> filled white
|> move (0, 275)
]
drawPlayer : Bool -> Form
drawPlayer rev =
let x' = if rev then 4 else -4
in group
[ circle 4 |> filled darkBlue |> move (x',-6)
, circle 4 |> filled red |> move (-x',-6)
, polygon [(0,15),(-10,-10),(10,-10)] |> outlined (solid white)
]
drawBullet : Form
drawBullet = circle 5 |> filled white
drawEnemy : Enemy -> Form
drawEnemy e =
let e' = case e.size of
Large -> ngon 12 35 |> outlined (solid white)
Medium -> ngon 6 25 |> outlined (solid white)
Small -> ngon 3 10 |> outlined (solid white)
in e' |> move (e.x,e.y) |> rotate (degrees e.angle)
drawEnemies : List Enemy -> Form
drawEnemies = group << List.map drawEnemy
prettyStats : List Float -> Form
prettyStats = formatText << toString << List.map truncate
formatText : String -> Form
formatText = toForm << Graphics.Element.centered << (Text.color white) << Text.fromString
drawVictory : Form
drawVictory = "Congratulations! You have conquered every surface!"
|> formatText
drawInstruction : Form
drawInstruction = "Press SPACE to start. Control with the arrow keys"
|> formatText
drawGame : Game -> Element
drawGame ({state,player,surface,enemies,bullet} as game) =
let bullet' = if not bullet.active
then toForm (Graphics.Element.spacer 1 1)
else drawBullet |> move (bullet.x, bullet.y)
v' = if state == Victory
then drawVictory |> move (0,100)
else toForm (Graphics.Element.spacer 1 1)
i' = if state == Begin
then drawInstruction |> move (0,100)
else toForm (Graphics.Element.spacer 1 1)
in collage 800 600
[ background
, drawPlayer player.rev
|> move (player.x, player.y)
|> rotate (degrees player.angle)
, drawEnemies enemies
, bullet'
, v'
, i'
--, prettyStats [player.x,player.y,player.vx,player.vy]
-- |> move (0, -100)
, toString state |> formatText
|> move (0,200)
, toString surface |> formatText
|> move (0,-200)
, foreground
]
--MAIN GAME LOOP
main : Signal Element
main = Signal.map drawGame gameState