Files
loop/src/Dodge/Spark.hs
T

107 lines
2.7 KiB
Haskell

module Dodge.Spark (
updateSpark,
colSpark,
colSparkRandDir,
createBarrelSpark,
randColDirSpark,
randSpark,
) where
import Dodge.Damage
import Color
import Control.Monad.State
import Dodge.Data.World
import Dodge.WorldEvent.ThingsHit
import Geometry
import LensHelp
import System.Random
updateSpark :: World -> Spark -> (World, Maybe Spark)
updateSpark w sk
| magV (_skVel sk) < 1 = (w, Nothing)
| otherwise = case thingHit sp ep w of
Nothing -> (w, Just $ sk & skPos .~ ep & skVel .*.*~ 0.9 & skOldPos .~ sp)
Just (hp, hthing) ->
( sparkDam sk sp ep (hp, hthing) w
, Just $ sk & skPos .~ hp & skOldPos .~ sp & skVel .~ 0
)
where
sp = _skPos sk
ep = sp +.+ _skVel sk
sparkDam ::
Spark ->
Point2 ->
Point2 ->
(Point2, Either Creature Wall) ->
World ->
World
sparkDam sk sp ep mayEiCrWl = case mayEiCrWl of
(hitp, eicrwl) -> damageCrWl (thedam hitp) eicrwl
where
thedam hitp = Damage (_skDamageType sk) 1 sp hitp ep NoDamageEffect
createBarrelSpark :: Point2 -> Float -> Int -> World -> World
createBarrelSpark pos dir colid = colSparkRandDir 0.1 (numColor colid) pos dir
colSpark :: Color -> Point2 -> Float -> World -> World
colSpark = colSparkRandDir 0.7
randColDirSpark ::
State StdGen Color ->
State StdGen Float ->
Point2 ->
World ->
World
randColDirSpark randcol randdir pos w =
w
& randGen .~ g
& cWorld . lWorld . sparks
.:~ Spark
{ _skVel = rotateV dir (V2 5 0)
, _skColor = col
, _skPos = pos
, _skOldPos = pos
, _skWidth = 1
, _skDamageType = SPARKING
}
where
((col, dir), g) = (`runState` _randGen w) $ do
c <- randcol
d <- randdir
return (c, d)
randSpark ::
DamageType ->
State StdGen Float ->
State StdGen Color ->
State StdGen Float ->
Point2 ->
World ->
World
randSpark dt randspeed randcol randdir pos w =
w
& randGen .~ g
& cWorld . lWorld . sparks
.:~ Spark
{ _skVel = rotateV dir (V2 speed 0)
, _skColor = col
, _skPos = pos
, _skOldPos = pos
, _skWidth = 1
, _skDamageType = dt
}
where
((col, dir, speed), g) = (`runState` _randGen w) $ do
c <- randcol
d <- randdir
x <- randspeed
return (c, d, x)
colSparkRandDir :: Float -> Color -> Point2 -> Float -> World -> World
colSparkRandDir a col pos dir =
randColDirSpark
(return col)
(state $ randomR (dir - a, dir + a))
pos