Skip to content

Commit 28e7641

Browse files
committed
Display explosions when pirates get hit
Now that we have access to time diffs, I can animate an explosion when pirates get hit.
1 parent d31d7a5 commit 28e7641

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

Bullet.elm

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Bullet
66
, fireFrom
77
, move
88
, impact
9+
, keepExploding
910
, isDead
1011
)
1112

@@ -16,6 +17,7 @@ import Time exposing (Time)
1617

1718
type Status
1819
= Flying
20+
| Exploding Time
1921
| Impacted
2022

2123

@@ -41,6 +43,16 @@ fireTowards targetPosition bullet =
4143

4244
move : Time -> Bullet -> Bullet
4345
move diff bullet =
46+
case bullet.status of
47+
Flying ->
48+
moveTheBullet diff bullet
49+
50+
_ ->
51+
bullet
52+
53+
54+
moveTheBullet : Time -> Bullet -> Bullet
55+
moveTheBullet diff bullet =
4456
let
4557
distanceToNextPoint =
4658
Coordinate.distance bullet.position bullet.target
@@ -63,7 +75,20 @@ move diff bullet =
6375

6476
impact : Bullet -> Bullet
6577
impact bullet =
66-
{ bullet | status = Impacted }
78+
{ bullet | status = Exploding 0 }
79+
80+
81+
keepExploding : Time -> Bullet -> Bullet
82+
keepExploding diff bullet =
83+
case bullet.status of
84+
Exploding timeSoFar ->
85+
if ceiling (timeSoFar / explosionSpeed) > 3 then
86+
{ bullet | status = Impacted }
87+
else
88+
{ bullet | status = Exploding (timeSoFar + diff) }
89+
90+
_ ->
91+
bullet
6792

6893

6994
isDead : Bullet -> Bool
@@ -77,9 +102,56 @@ speedPerSecond =
77102

78103

79104
toEntity : Bullet -> Entity
80-
toEntity { position } =
105+
toEntity { position, status } =
106+
case status of
107+
Exploding diff ->
108+
if ceiling (diff / explosionSpeed) == 1 then
109+
explosionPhase1 position diff
110+
else if ceiling (diff / explosionSpeed) == 2 then
111+
explosionPhase2 position diff
112+
else
113+
explosionPhase3 position diff
114+
115+
_ ->
116+
regularBulletEntity position
117+
118+
119+
regularBulletEntity : Coordinate.Global -> Entity
120+
regularBulletEntity position =
81121
{ position = position
82122
, width = 10
83123
, height = 10
84124
, imagePath = "images/cannon-ball.png"
85125
}
126+
127+
128+
explosionPhase1 : Coordinate.Global -> Time -> Entity
129+
explosionPhase1 position timeSoFar =
130+
{ position = position
131+
, width = 50
132+
, height = 50
133+
, imagePath = "images/explosion1.png"
134+
}
135+
136+
137+
explosionPhase2 : Coordinate.Global -> Time -> Entity
138+
explosionPhase2 position timeSoFar =
139+
{ position = position
140+
, width = 75
141+
, height = 75
142+
, imagePath = "images/explosion2.png"
143+
}
144+
145+
146+
explosionPhase3 : Coordinate.Global -> Time -> Entity
147+
explosionPhase3 position timeSoFar =
148+
{ position = position
149+
, width = 50
150+
, height = 50
151+
, imagePath = "images/explosion3.png"
152+
}
153+
154+
155+
explosionSpeed : Time
156+
explosionSpeed =
157+
Time.second / 20

Game.elm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Game
1111
, shoot
1212
, detectCollisions
1313
, eliminateDead
14+
, processExplosions
1415
, viewIntroPhase
1516
, viewTowerPlacementPhase
1617
, viewAttackPhase
@@ -249,6 +250,11 @@ eliminateDead game =
249250
}
250251

251252

253+
processExplosions : Time -> GameState -> GameState
254+
processExplosions diff ({ bullets } as gameState) =
255+
{ gameState | bullets = List.map (Bullet.keepExploding diff) bullets }
256+
257+
252258

253259
-- COLLISION DETECTION
254260

Main.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ update msg gamePhase =
5252
|> Game.applyMovement diff
5353
|> Game.shoot diff
5454
|> Game.detectCollisions
55+
|> Game.processExplosions diff
5556
|> Game.eliminateDead
5657
|> Game.checkForGameEnd
5758
|> withNoCmd

images/explosion1.png

2.11 KB
Loading

images/explosion2.png

1.66 KB
Loading

images/explosion3.png

854 Bytes
Loading

0 commit comments

Comments
 (0)