packages feed

edge-0.9.0.2: AfterEffect/MineExplosion.hs

module AfterEffect.MineExplosion where

import Data.WrapAround ( WP, WM )
import Animation ( Audible(..), Animation(..), procOneTimeSnd )
import Updating ( Transient(..), InternallyUpdating(..) )
import qualified Moving as M ( Locatable(..) )
import Graphics.Gloss.Data.Picture
    ( Picture(Blank, Color, Scale, Text) )
import Graphics.Gloss.Data.Color ( white )
import Sound.ALUT ( Source, stop )
import Data.Maybe ( isNothing, fromMaybe, fromJust )
import ResourceTracker ( RT, getImage )
import GHC.Float ()
import Common ( Time )

data MineExplosion = MineExplosion { center :: WP
                                   , timeRemaining :: Time
                                   , sndSrc :: Maybe Source
                                   , wmap :: WM
                                   , rt :: RT
                                   , animClock :: Time
                                   }

new a b c = MineExplosion { center = c
                          , timeRemaining = 3
                          , sndSrc = Nothing
                          , wmap = b
                          , rt = a
                          , animClock = 0
                          }

instance Animation MineExplosion where
  image s t =
    if animClock s < 0.1
      then fromMaybe (Color white (Scale 0.10 0.10 (Text "boom!")))
                     (getImage (rt s) "mine-explosion.bmp")
      else Blank

instance M.Locatable MineExplosion where
  center = center

instance Transient MineExplosion where

  expired' s = if timeRemaining s <= 0 then Just [] else Nothing

instance InternallyUpdating MineExplosion where

  preUpdate s t = s { timeRemaining = timeRemaining s - t }

  postUpdate s t = s { animClock = animClock s + t }

instance Audible MineExplosion where

  processAudio s a =
    procOneTimeSnd
      s sndSrc a rt "explosion.wav" 0.5 wmap (\x y -> x { sndSrc = y })

  terminateAudio s = if isNothing (sndSrc s)
                          then return s
                          else do stop [fromJust (sndSrc s)]
                                  return s