Simplify creature action/impulse code

This commit is contained in:
2025-10-18 17:32:23 +01:00
parent 11c7b25fe9
commit 4542a6cea6
4 changed files with 17 additions and 19 deletions
+13 -3
View File
@@ -11,6 +11,7 @@ module Dodge.Creature.Action (
youDropItem,
) where
import Data.Foldable
import Linear
import NewInt
import Dodge.Creature.MoveType
@@ -32,11 +33,20 @@ import Dodge.SoundLogic
import Geometry
import qualified IntMapHelp as IM
import LensHelp
import Dodge.Creature.Impulse
performActions :: World -> Creature -> ([Impulse],Creature)
performActions w cr = (concat iss
, cr & crActionPlan . apAction .~ catMaybes mayas)
--performActions :: World -> Creature -> ([Impulse],Creature)
--performActions w cr = (concat iss
-- , cr & crActionPlan . apAction .~ catMaybes mayas)
-- where
-- (iss, mayas) = unzip $ map (performAction cr w) $ cr ^. crActionPlan . apAction
performActions :: Int -> World -> World
performActions cid w = foldl' (followImpulse cid)
(w & cWorld . lWorld . creatures . ix cid . crActionPlan . apAction .~ catMaybes mayas)
(concat iss)
where
cr = w ^?! cWorld . lWorld . creatures . ix cid
(iss, mayas) = unzip $ map (performAction cr w) $ cr ^. crActionPlan . apAction
type OutAction = ([Impulse], Maybe Action)
+1 -8
View File
@@ -1,11 +1,9 @@
--{-# LANGUAGE TupleSections #-}
module Dodge.Creature.Impulse (followImpulses) where
module Dodge.Creature.Impulse (followImpulse) where
import Control.Monad.State
import Data.Foldable
import Data.Maybe
import Dodge.Creature.Action
import Dodge.Creature.Impulse.Movement
import Dodge.Creature.MoveType
--import Dodge.Creature.Impulse.UseItem
@@ -21,11 +19,6 @@ import Linear
import NewInt
import System.Random
followImpulses :: Int -> World -> World
followImpulses cid w =
let (is, cr) = performActions w (w ^?! cWorld . lWorld . creatures . ix cid)
in foldl' (followImpulse cid) (w & cWorld . lWorld . creatures . ix cid .~ cr) is
-- note SwitchToItem doesn't necessarily update the root item correctly
followImpulse :: Int -> World -> Impulse -> World
followImpulse cid w imp = case imp of
+1 -2
View File
@@ -1,6 +1,5 @@
module Dodge.Creature.Update (updateCreature) where
import Dodge.Creature.Impulse
import Linear
import NewInt
import Color
@@ -48,7 +47,7 @@ updateCreature' cr =
LampCrit{} -> updateLampoid cr
BarrelCrit bt -> updateBarreloid bt cr
AvatarDead -> id
ChaseCrit {} -> \w -> crUpdate cid . followImpulses cid
ChaseCrit {} -> \w -> crUpdate cid . performActions cid
$ over (cWorld . lWorld . creatures . ix cid) (chaseCritInternal w) w
_ -> crUpdate cid
where