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
exposed-modules: Affection
, Affection.Draw
, Affection.Particle
, Affection.Types
default-extensions: OverloadedStrings

View File

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

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(..)
, 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)