introducing particles

first little drafts
This commit is contained in:
nek0 2016-12-12 03:34:57 +01:00
parent 8a8a5ed066
commit 1f946624ef
4 changed files with 46 additions and 2 deletions

View file

@ -34,6 +34,7 @@ flag examples
library library
exposed-modules: Affection exposed-modules: Affection
, Affection.Draw , Affection.Draw
, Affection.Particle
, Affection.Types , Affection.Types
default-extensions: OverloadedStrings default-extensions: OverloadedStrings

View file

@ -30,6 +30,7 @@ import Foreign.Storable (peek)
import Affection.Types as A import Affection.Types as A
import Affection.Draw as A import Affection.Draw as A
import Afection.Particle as A
import qualified BABL as B import qualified BABL as B

32
src/Affection/Particle.hs Normal file
View file

@ -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_

View file

@ -13,10 +13,12 @@ module Affection.Types
, RGBA(..) , RGBA(..)
, DrawType(..) , DrawType(..)
, DrawRequest(..) , DrawRequest(..)
, SDL.WindowConfig(..) -- | Particle system
, SDL.defaultWindow , Particle(..)
-- | Convenience exports -- | Convenience exports
, liftIO , liftIO
, SDL.WindowConfig(..)
, SDL.defaultWindow
-- | GEGL reexports -- | GEGL reexports
, G.GeglRectangle(..) , G.GeglRectangle(..)
, G.GeglBuffer(..) , G.GeglBuffer(..)
@ -122,3 +124,11 @@ data DrawType
| Line -- ^ only draw the outline of the area | Line -- ^ only draw the outline of the area
{ lineWidth :: Int -- ^ Width of line in pixels { 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)