Make thingsHit use streaming

This commit is contained in:
2022-06-26 12:13:31 +01:00
parent f47059ae9b
commit 5ba0ca9f9c
8 changed files with 49 additions and 61 deletions
@@ -5,15 +5,17 @@ import Dodge.Particle.Update
import Geometry
import LensHelp
import qualified Data.IntMap.Strict as IM
import Streaming
import qualified Streaming.Prelude as S
expireAndDamage :: (Particle -> Point2 -> [Damage])
-> Particle
-> [(Point2, Either Creature Wall)]
-> Stream (Of (Point2, Either Creature Wall)) Identity ()
-> World
-> (World, Maybe Particle)
expireAndDamage fdm bt hitThings w = case hitThings of
[] -> (w, mvPt bt)
(x:_) -> (doDamages fdm x bt w, destroyAt (fst x) bt)
expireAndDamage fdm bt things w = case runIdentity $ S.head_ things of
Nothing -> (w, mvPt bt)
Just x -> (doDamages fdm x bt w, destroyAt (fst x) bt)
doDamages :: (Particle -> Point2 -> [Damage])
-> (Point2, Either Creature Wall)
+6 -4
View File
@@ -6,6 +6,8 @@ import Geometry
--import LensHelp
import Data.Bifunctor
import Streaming
import qualified Streaming.Prelude as S
penetrate
:: ((Point2,Either Creature Wall) -> Particle -> Maybe Particle)
@@ -13,11 +15,11 @@ penetrate
-> (Particle -> Point2 -> [Damage]) -- | damages when penetrating
-> (Particle -> Point2 -> [Damage]) -- | damages when not penetrating
-> Particle
-> [(Point2, Either Creature Wall)]
-> Stream (Of (Point2, Either Creature Wall)) Identity ()
-> World
-> (World, Maybe Particle)
penetrate t pendam stopdam pt hitThings w = case hitThings of
[] -> (w, mvPt pt)
(x:xs) -> case t x pt of
penetrate t pendam stopdam pt hitThings w = case runIdentity $ S.uncons hitThings of
Nothing -> (w, mvPt pt)
Just (x,xs) -> case t x pt of
Nothing -> expireAndDamage stopdam pt hitThings w
Just pt' -> first (doDamages pendam x pt) $ penetrate t pendam stopdam pt' xs w