Skip to content

Commit

Permalink
Display explosions when pirates get hit
Browse files Browse the repository at this point in the history
Now that we have access to time diffs, I can animate an explosion when
pirates get hit.
  • Loading branch information
JoelQ committed Feb 28, 2018
1 parent d31d7a5 commit 28e7641
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
76 changes: 74 additions & 2 deletions Bullet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Bullet
, fireFrom
, move
, impact
, keepExploding
, isDead
)

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

type Status
= Flying
| Exploding Time
| Impacted


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

move : Time -> Bullet -> Bullet
move diff bullet =
case bullet.status of
Flying ->
moveTheBullet diff bullet

_ ->
bullet


moveTheBullet : Time -> Bullet -> Bullet
moveTheBullet diff bullet =
let
distanceToNextPoint =
Coordinate.distance bullet.position bullet.target
Expand All @@ -63,7 +75,20 @@ move diff bullet =

impact : Bullet -> Bullet
impact bullet =
{ bullet | status = Impacted }
{ bullet | status = Exploding 0 }


keepExploding : Time -> Bullet -> Bullet
keepExploding diff bullet =
case bullet.status of
Exploding timeSoFar ->
if ceiling (timeSoFar / explosionSpeed) > 3 then
{ bullet | status = Impacted }
else
{ bullet | status = Exploding (timeSoFar + diff) }

_ ->
bullet


isDead : Bullet -> Bool
Expand All @@ -77,9 +102,56 @@ speedPerSecond =


toEntity : Bullet -> Entity
toEntity { position } =
toEntity { position, status } =
case status of
Exploding diff ->
if ceiling (diff / explosionSpeed) == 1 then
explosionPhase1 position diff
else if ceiling (diff / explosionSpeed) == 2 then
explosionPhase2 position diff
else
explosionPhase3 position diff

_ ->
regularBulletEntity position


regularBulletEntity : Coordinate.Global -> Entity
regularBulletEntity position =
{ position = position
, width = 10
, height = 10
, imagePath = "images/cannon-ball.png"
}


explosionPhase1 : Coordinate.Global -> Time -> Entity
explosionPhase1 position timeSoFar =
{ position = position
, width = 50
, height = 50
, imagePath = "images/explosion1.png"
}


explosionPhase2 : Coordinate.Global -> Time -> Entity
explosionPhase2 position timeSoFar =
{ position = position
, width = 75
, height = 75
, imagePath = "images/explosion2.png"
}


explosionPhase3 : Coordinate.Global -> Time -> Entity
explosionPhase3 position timeSoFar =
{ position = position
, width = 50
, height = 50
, imagePath = "images/explosion3.png"
}


explosionSpeed : Time
explosionSpeed =
Time.second / 20
6 changes: 6 additions & 0 deletions Game.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Game
, shoot
, detectCollisions
, eliminateDead
, processExplosions
, viewIntroPhase
, viewTowerPlacementPhase
, viewAttackPhase
Expand Down Expand Up @@ -249,6 +250,11 @@ eliminateDead game =
}


processExplosions : Time -> GameState -> GameState
processExplosions diff ({ bullets } as gameState) =
{ gameState | bullets = List.map (Bullet.keepExploding diff) bullets }



-- COLLISION DETECTION

Expand Down
1 change: 1 addition & 0 deletions Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ update msg gamePhase =
|> Game.applyMovement diff
|> Game.shoot diff
|> Game.detectCollisions
|> Game.processExplosions diff
|> Game.eliminateDead
|> Game.checkForGameEnd
|> withNoCmd
Expand Down
Binary file added images/explosion1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/explosion2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/explosion3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 28e7641

Please sign in to comment.