Simplify block/wall destruction
This uses a right fold, so might be slower than a strict left fold
This commit is contained in:
@@ -13,14 +13,23 @@ import Dodge.Zoning.Base
|
||||
import Geometry.Data
|
||||
import qualified IntMapHelp as IM
|
||||
|
||||
--updateWallDamages :: World -> World
|
||||
--updateWallDamages w =
|
||||
-- let (is, w') = IM.foldlWithKey' f (mempty, w) (w ^. cWorld . lWorld . wallDamages)
|
||||
-- in updateEdgesWall is w' & cWorld . lWorld . wallDamages .~ IM.empty
|
||||
-- where
|
||||
-- f isw k dams = fromMaybe isw $ do
|
||||
-- wl <- isw ^? _2 . cWorld . lWorld . walls . ix k
|
||||
-- return $ foldl' (flip (`damageWall` wl)) isw dams
|
||||
|
||||
updateWallDamages :: World -> World
|
||||
updateWallDamages w =
|
||||
let (is, w') = IM.foldlWithKey' f (mempty, w) (w ^. cWorld . lWorld . wallDamages)
|
||||
in updateEdgesWall is w' & cWorld . lWorld . wallDamages .~ IM.empty
|
||||
updateWallDamages w = set (cWorld . lWorld . wallDamages) mempty
|
||||
. uncurry updateEdgesWall $ foldrM f w
|
||||
(IM.toList $ w ^. cWorld . lWorld . wallDamages)
|
||||
where
|
||||
f isw k dams = fromMaybe isw $ do
|
||||
wl <- isw ^? _2 . cWorld . lWorld . walls . ix k
|
||||
return $ foldl' (flip (`damageWall` wl)) isw dams
|
||||
f (k,dams) w' = fromMaybe (mempty,w') $ do
|
||||
wl <- w' ^? cWorld . lWorld . walls . ix k
|
||||
return $ foldrM (damageWall wl) w' dams
|
||||
|
||||
updateEdgesWall :: S.Set Int2 -> World -> World
|
||||
updateEdgesWall is w =
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
{- | Controls a walls response to external damage.
|
||||
- Indestructible walls may still produce sparks, dust etc
|
||||
-}
|
||||
module Dodge.Wall.Damage (damageWall) where
|
||||
module Dodge.Wall.Damage (damageWall
|
||||
) where
|
||||
|
||||
import Linear
|
||||
import Data.Maybe
|
||||
@@ -22,19 +23,32 @@ import Dodge.Data.World
|
||||
import LensHelp
|
||||
import qualified Data.Set as S
|
||||
|
||||
-- maybeDestroyDoor should rather happen during the door mechanism update
|
||||
damageWall :: Damage -> Wall -> (S.Set Int2,World) -> (S.Set Int2,World)
|
||||
damageWall dt wl (is,w) = case _wlStructure wl of
|
||||
MachinePart mcid -> (,) is $ w' & cWorld . lWorld . machines . ix mcid . mcDamage .:~ dt
|
||||
---- maybeDestroyDoor should rather happen during the door mechanism update
|
||||
--damageWall :: Damage -> Wall -> (S.Set Int2,World) -> (S.Set Int2,World)
|
||||
--damageWall dt wl (is,w) = case _wlStructure wl of
|
||||
-- MachinePart mcid -> (,) is $ w' & cWorld . lWorld . machines . ix mcid . mcDamage .:~ dt
|
||||
-- BlockPart blid ->
|
||||
-- w' & cWorld . lWorld . blocks . ix blid . blHP -~ dmam
|
||||
-- & maybeDestroyBlock blid
|
||||
-- & _1 <>~ is
|
||||
-- DoorPart drid _ ->
|
||||
-- w' & cWorld . lWorld . doors . ix drid . drHP -~ dmam
|
||||
-- & maybeDestroyDoor drid
|
||||
-- & _1 <>~ is
|
||||
-- _ -> (is, w')
|
||||
-- where
|
||||
-- (dmam,w') = damMatSideEffect dt (_wlMaterial wl) (Right wl) w
|
||||
|
||||
damageWall :: Wall -> Damage ->World -> (S.Set Int2,World)
|
||||
damageWall wl dt w = case _wlStructure wl of
|
||||
MachinePart mcid -> (,) mempty $ w' & cWorld . lWorld . machines . ix mcid . mcDamage .:~ dt
|
||||
BlockPart blid ->
|
||||
w' & cWorld . lWorld . blocks . ix blid . blHP -~ dmam
|
||||
& maybeDestroyBlock blid
|
||||
& _1 <>~ is
|
||||
DoorPart drid _ ->
|
||||
w' & cWorld . lWorld . doors . ix drid . drHP -~ dmam
|
||||
& maybeDestroyDoor drid
|
||||
& _1 <>~ is
|
||||
_ -> (is, w')
|
||||
_ -> (mempty, w')
|
||||
where
|
||||
(dmam,w') = damMatSideEffect dt (_wlMaterial wl) (Right wl) w
|
||||
|
||||
|
||||
Reference in New Issue
Block a user