Fix underflow when damaging character

Fix underflow that could happen when multiple gormlaks attack the
character in a single turn
This commit is contained in:
Griffin Smith 2019-10-06 12:59:53 -04:00
parent bf92a370a5
commit a57e36dca8
2 changed files with 8 additions and 2 deletions

View file

@ -14,7 +14,8 @@ import Xanthous.Data (Positioned(..), diffPositions, stepTowards, isUn
import Xanthous.Data.EntityMap import Xanthous.Data.EntityMap
import qualified Xanthous.Entities.Creature as Creature import qualified Xanthous.Entities.Creature as Creature
import Xanthous.Entities.Creature (Creature) import Xanthous.Entities.Creature (Creature)
import Xanthous.Entities.Character (Character, characterHitpoints) import Xanthous.Entities.Character (Character)
import qualified Xanthous.Entities.Character as Character
import qualified Xanthous.Entities.RawTypes as Raw import qualified Xanthous.Entities.RawTypes as Raw
import Xanthous.Entities (Entity(..), Brain(..), brainVia) import Xanthous.Entities (Entity(..), Brain(..), brainVia)
import Xanthous.Game.State (entities, GameState, entityIs) import Xanthous.Game.State (entities, GameState, entityIs)
@ -54,7 +55,7 @@ stepGormlak pe@(Positioned pos creature) = do
vision = Creature.visionRadius creature vision = Creature.visionRadius creature
attackCharacter = do attackCharacter = do
say ["combat", "creatureAttack"] $ object [ "creature" A..= creature ] say ["combat", "creatureAttack"] $ object [ "creature" A..= creature ]
character . characterHitpoints -= 1 character %= Character.damage 1
newtype GormlakBrain = GormlakBrain Creature newtype GormlakBrain = GormlakBrain Creature

View file

@ -8,6 +8,7 @@ module Xanthous.Entities.Character
, mkCharacter , mkCharacter
, pickUpItem , pickUpItem
, isDead , isDead
, damage
) where ) where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Xanthous.Prelude import Xanthous.Prelude
@ -71,3 +72,7 @@ isDead = (== 0) . view characterHitpoints
pickUpItem :: Item -> Character -> Character pickUpItem :: Item -> Character -> Character
pickUpItem item = inventory %~ (item <|) pickUpItem item = inventory %~ (item <|)
damage :: Word -> Character -> Character
damage amount = characterHitpoints %~ \case
n | n <= amount -> 0
| otherwise -> n - amount