diff --git a/affection.cabal b/affection.cabal index c5fc553..a57e18b 100644 --- a/affection.cabal +++ b/affection.cabal @@ -34,6 +34,7 @@ flag examples library exposed-modules: Affection , Affection.Draw + , Affection.Particle , Affection.Types default-extensions: OverloadedStrings diff --git a/src/Affection.hs b/src/Affection.hs index 8352afa..cddccae 100644 --- a/src/Affection.hs +++ b/src/Affection.hs @@ -30,6 +30,7 @@ import Foreign.Storable (peek) import Affection.Types as A import Affection.Draw as A +import Afection.Particle as A import qualified BABL as B diff --git a/src/Affection/Particle.hs b/src/Affection/Particle.hs new file mode 100644 index 0000000..1acea04 --- /dev/null +++ b/src/Affection/Particle.hs @@ -0,0 +1,32 @@ +-- | This module introduces a simple particle system to Affection +module Affection.Particle + ( updateParticles + , drawParticles + ) where + +import Affection.Types + +-- This function updates particles through a specified function. Particle ageing +-- and death is being handled by 'updateParticles' itself and does not need to +-- bother you. +updateParticles + :: Double -- ^ Elapsed time in seconds + -> (Double -> Particle -> Particle) -- ^ Update function for a single 'Particle' + -- This Function should take the elapsed time + -- in seconds and the initial particle as arguments. + -> [Particle] -- ^ List of 'Particle's to be processed + -> [Particle] -- ^ resulting list of particles +updateParticles _ _ [] = [] +updateParticles time funct (p:ps) = + if particleLife p - time < 0 + then + updateParticles time funct ps + else + (funct time $ p { particleLife = particleLife p - time }) : + updateparticles time funct ps + +drawParticles + :: (Particle -> Affection us ()) + -> [Particle] + -> Affection us () +drawParticles = mapM_ diff --git a/src/Affection/Types.hs b/src/Affection/Types.hs index 28c34c9..20bf577 100644 --- a/src/Affection/Types.hs +++ b/src/Affection/Types.hs @@ -13,10 +13,12 @@ module Affection.Types , RGBA(..) , DrawType(..) , DrawRequest(..) - , SDL.WindowConfig(..) - , SDL.defaultWindow + -- | Particle system + , Particle(..) -- | Convenience exports , liftIO + , SDL.WindowConfig(..) + , SDL.defaultWindow -- | GEGL reexports , G.GeglRectangle(..) , G.GeglBuffer(..) @@ -122,3 +124,11 @@ data DrawType | Line -- ^ only draw the outline of the area { lineWidth :: Int -- ^ Width of line in pixels } + +-- | A single particle +data Particle = Particle + { particleLife :: Double -- ^ Time to live in seconds + , particlePosition :: (Int, Int) -- ^ Position of particle on canvas + , particleRotation :: Double -- ^ Particle rotation + , particleVelocity :: (Int, Int) -- ^ particle velocity as vector of pixels per second + } deriving (Show, Eq)