-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.elm
113 lines (85 loc) · 2.55 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
import Graphics.Element exposing (Element)
import Graphics.Collage exposing (collage)
import Types exposing (..)
import Helpers exposing (collide)
import Input exposing (input)
import Background
import Particles
import Asteroids
import Player
initialGamestate : Gamestate
initialGamestate =
{ particles = Particles.init
, lasers = Particles.init
, asteroids = Asteroids.init
, player = Player.init
, sfx = []
}
spawnParticles : Gamestate -> List Particle
spawnParticles gs =
if (List.length gs.particles) < 60
then Particles.explosion gs.player.position
else []
spawnLasers : Gamestate -> Input -> List Particle
spawnLasers gs input = []
update : Input -> Gamestate -> Gamestate
update input gs =
let
-- check if player collide with any asteroids
collided = List.any (collide 15 gs.player.position)
<| List.map .position gs.asteroids
-- generate particles for player explosion (if needed)
playerExplosion = if collided
then Particles.explosion gs.player.position
else []
-- simulate existing particles
updatedParticles = Particles.update gs.particles input.dt
explosionSfx =
if collided
then [ CrashSfx ]
else []
in
{ gs | particles = playerExplosion ++ updatedParticles
, asteroids = Asteroids.update gs.asteroids input.dt
, lasers = Particles.update gs.lasers input.dt
, player = if collided
then
Player.init
else
Player.update gs.player input
, sfx = explosionSfx
}
-- GRAPHICS --
view : Gamestate -> Element
view gamestate =
collage 400 400
[ Background.draw
, Particles.draw gamestate.particles
, Asteroids.draw gamestate.asteroids
, Player.draw gamestate.player
]
-- SIGNALS --
gamestate : Signal Gamestate
gamestate = Signal.foldp update initialGamestate input
main : Signal Element
main = Signal.map view gamestate
-- SOUND
soundSignal : Signal (List Sfx)
soundSignal =
let
nonEmpty l = not <| List.isEmpty l
in
Signal.map .sfx gamestate
|> Signal.filter nonEmpty []
port sfxPort : Signal (List String)
port sfxPort =
let
mapsound s =
case s of
LaserSfx -> "laser"
HitSfx -> "explosion"
CrashSfx -> "crash"
SpawnSfx -> "spawn"
mapsounds s = List.map mapsound s
in
Signal.map mapsounds soundSignal