From 604319850c4b875fb8cdb9c1e7ca34f013993f20 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Thu, 25 Nov 2021 10:14:00 -0500 Subject: [PATCH] feat(gs/xanthous): Creatures drop their items when they die When a creature is killed, it now drops all the contents of its inventory on the ground. Change-Id: Ie95c397308ba2c9861f10e58b99c91c9cc759c56 --- users/grfn/xanthous/src/Xanthous/App.hs | 16 ++++++++++++++-- .../xanthous/src/Xanthous/Entities/Creature.hs | 2 +- users/grfn/xanthous/src/Xanthous/Util.hs | 10 +++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/users/grfn/xanthous/src/Xanthous/App.hs b/users/grfn/xanthous/src/Xanthous/App.hs index b29614383..a25183395 100644 --- a/users/grfn/xanthous/src/Xanthous/App.hs +++ b/users/grfn/xanthous/src/Xanthous/App.hs @@ -48,17 +48,18 @@ import Xanthous.Game.Draw (drawGame) import Xanthous.Game.Prompt hiding (Fire) import qualified Xanthous.Messages as Messages import Xanthous.Random -import Xanthous.Util (removeVectorIndex) +import Xanthous.Util (removeVectorIndex, useListOf) import Xanthous.Util.Inflection (toSentence) import Xanthous.Physics (throwDistance, bluntThrowDamage) import Xanthous.Data.EntityMap.Graphics (lineOfSight) import Xanthous.Data.EntityMap (EntityID) -------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- import Xanthous.Entities.Common ( InventoryPosition, describeInventoryPosition, backpack , wieldableItem, wieldedItems, wielded, itemsWithPosition , removeItemFromPosition, asWieldedItem, inRightHand - , wieldedItem + , wieldedItem, items ) import qualified Xanthous.Entities.Character as Character import Xanthous.Entities.Character hiding (pickUpItem) @@ -462,7 +463,18 @@ damageCreature (creatureID, creature) dam = do if Creature.isDead creature' then do say ["combat", "killed"] msgParams + floorItems <- useListOf + $ entities + . ix creatureID + . positioned + . _SomeEntity @Creature + . inventory + . items + mCreaturePos <- preuse $ entities . ix creatureID . position entities . at creatureID .= Nothing + for_ mCreaturePos $ \creaturePos -> + entities . EntityMap.atPosition creaturePos + %= (<> fromList (SomeEntity <$> floorItems)) else entities . ix creatureID . positioned .= SomeEntity creature' pure creature' diff --git a/users/grfn/xanthous/src/Xanthous/Entities/Creature.hs b/users/grfn/xanthous/src/Xanthous/Entities/Creature.hs index 3af2cafe3..3ea610795 100644 --- a/users/grfn/xanthous/src/Xanthous/Entities/Creature.hs +++ b/users/grfn/xanthous/src/Xanthous/Entities/Creature.hs @@ -42,7 +42,7 @@ import Xanthous.Data import Xanthous.Data.Entities import Xanthous.Entities.Creature.Hippocampus import Xanthous.Util.QuickCheck (GenericArbitrary(..)) -import Xanthous.Entities.Common (Inventory) +import Xanthous.Entities.Common (Inventory, HasInventory(..)) -------------------------------------------------------------------------------- data Creature = Creature diff --git a/users/grfn/xanthous/src/Xanthous/Util.hs b/users/grfn/xanthous/src/Xanthous/Util.hs index 6678cffe6..f918340f0 100644 --- a/users/grfn/xanthous/src/Xanthous/Util.hs +++ b/users/grfn/xanthous/src/Xanthous/Util.hs @@ -34,7 +34,7 @@ module Xanthous.Util , times, times_, endoTimes -- * State utilities - , modifyK, modifyKL + , modifyK, modifyKL, useListOf -- * Type-level programming utils , KnownBool(..) @@ -311,6 +311,14 @@ modifyK k = get >>= k >>= put modifyKL :: MonadState s m => LensLike m s s a b -> (a -> m b) -> m () modifyKL l k = get >>= traverseOf l k >>= put +-- | Use a list of all the targets of a 'Fold' in the current state +-- +-- @@ +-- evalState (useListOf folded) === toList +-- @@ +useListOf :: MonadState s m => Getting (Endo [a]) s a -> m [a] +useListOf = gets . toListOf + -------------------------------------------------------------------------------- -- | A newtype wrapper around 'Char' whose 'Enum' and 'Bounded' instances only