From cb1fa592ab8551e1485a7f98f9a3df76438fe2f4 Mon Sep 17 00:00:00 2001 From: Mason Mackaman Date: Fri, 30 Oct 2020 18:40:10 -0400 Subject: [PATCH] Refactor Equipment --- src/Game/Data/Item/Equipment.purs | 275 +++++++++++++++++++++++------- src/Game/Data/Item/Hands.purs | 82 +++++++++ 2 files changed, 293 insertions(+), 64 deletions(-) create mode 100644 src/Game/Data/Item/Hands.purs diff --git a/src/Game/Data/Item/Equipment.purs b/src/Game/Data/Item/Equipment.purs index a82d367..292acf3 100644 --- a/src/Game/Data/Item/Equipment.purs +++ b/src/Game/Data/Item/Equipment.purs @@ -1,7 +1,6 @@ module Game.Data.Item.Equipment where import Prelude - import Data.Argonaut.Decode (class DecodeJson) import Data.Argonaut.Decode.Generic.Rep (genericDecodeJson) import Data.Argonaut.Encode (class EncodeJson) @@ -12,44 +11,179 @@ import Data.Generic.Rep.Show (genericShow) import Game.Data.Experience (Level) import Game.Data.Stats (Stats) +data Equipment + = Head Head + | Body Body + | Hands Hands + | Feet Feet + | HandItem HandItem -data Equipment = - Helmet EquipmentProps - | ChestPlate EquipmentProps - | Gloves EquipmentProps - | LeggGuards EquipmentProps - | Shoes EquipmentProps - | LongSword WeaponProps - | GreatSword WeaponProps - | Dagger WeaponProps - | Bow WeaponProps +data HandItem + = MainHandItem MainHandItem + | OffHandItem OffHandItem + | EitherHandItem EitherHandItem + | TwoHandedItem TwoHandedItem + +derive instance genericHandItem :: Generic HandItem _ + +derive instance eqHandItem :: Eq HandItem + +instance showHandItem :: Show HandItem where + show = genericShow + +instance encodeJsonHandItem :: EncodeJson HandItem where + encodeJson = genericEncodeJson + +instance decodeJsonHandItem :: DecodeJson HandItem where + decodeJson = genericDecodeJson + +data Head + = Helmet EquipmentProps + +derive instance genericHead :: Generic Head _ + +derive instance eqHead :: Eq Head + +instance showHead :: Show Head where + show = genericShow + +instance encodeJsonHead :: EncodeJson Head where + encodeJson = genericEncodeJson + +instance decodeJsonHead :: DecodeJson Head where + decodeJson = genericDecodeJson + +data Body + = ChestPlate EquipmentProps + +derive instance genericBody :: Generic Body _ + +derive instance eqBody :: Eq Body + +instance showBody :: Show Body where + show = genericShow + +instance encodeJsonBody :: EncodeJson Body where + encodeJson = genericEncodeJson + +instance decodeJsonBody :: DecodeJson Body where + decodeJson = genericDecodeJson + +data Hands + = Gloves EquipmentProps + +derive instance genericHands :: Generic Hands _ + +derive instance eqHands :: Eq Hands + +instance showHands :: Show Hands where + show = genericShow + +instance encodeJsonHands :: EncodeJson Hands where + encodeJson = genericEncodeJson + +instance decodeJsonHands :: DecodeJson Hands where + decodeJson = genericDecodeJson + +data Feet + = Shoes EquipmentProps + +derive instance genericFeet :: Generic Feet _ + +derive instance eqFeet :: Eq Feet + +instance showFeet :: Show Feet where + show = genericShow + +instance encodeJsonFeet :: EncodeJson Feet where + encodeJson = genericEncodeJson + +instance decodeJsonFeet :: DecodeJson Feet where + decodeJson = genericDecodeJson + +data MainHandItem + = LongSword WeaponProps | Staff WeaponProps - | Shield ShieldProps - -type EquipmentProps = { - name :: String, - description :: String, - stats :: Stats, - levelRequirement :: Level -} - -type WeaponProps = { - name :: String, - description :: String, - stats :: Stats, - levelRequirement :: Level, - damage :: Int -} - -type ShieldProps = { - name :: String, - description :: String, - stats :: Stats, - levelRequirement :: Level, - block :: Int -} - -derive instance genericEquipment:: Generic Equipment _ + +derive instance genericMainHandItem :: Generic MainHandItem _ + +derive instance eqMainHandItem :: Eq MainHandItem + +instance showMainHandItem :: Show MainHandItem where + show = genericShow + +instance encodeJsonMainHandItem :: EncodeJson MainHandItem where + encodeJson = genericEncodeJson + +instance decodeJsonMainHandItem :: DecodeJson MainHandItem where + decodeJson = genericDecodeJson + +data OffHandItem + = Shield ShieldProps + +derive instance genericOffHandItem :: Generic OffHandItem _ + +derive instance eqOffHandItem :: Eq OffHandItem + +instance showOffHandItem :: Show OffHandItem where + show = genericShow + +instance encodeJsonOffHandItem :: EncodeJson OffHandItem where + encodeJson = genericEncodeJson + +instance decodeJsonOffHandItem :: DecodeJson OffHandItem where + decodeJson = genericDecodeJson + +data EitherHandItem + = Dagger WeaponProps + +derive instance genericEitherHandItem :: Generic EitherHandItem _ + +derive instance eqEitherHandItem :: Eq EitherHandItem + +instance showEitherHandItem :: Show EitherHandItem where + show = genericShow + +instance encodeJsonEitherHandItem :: EncodeJson EitherHandItem where + encodeJson = genericEncodeJson + +instance decodeJsonEitherHandItem :: DecodeJson EitherHandItem where + decodeJson = genericDecodeJson + +data TwoHandedItem + = GreatSword WeaponProps + | Bow WeaponProps + +derive instance genericTwoHandedItem :: Generic TwoHandedItem _ + +derive instance eqTwoHandedItem :: Eq TwoHandedItem + +instance showTwoHandedItem :: Show TwoHandedItem where + show = genericShow + +instance encodeJsonTwoHandedItem :: EncodeJson TwoHandedItem where + encodeJson = genericEncodeJson + +instance decodeJsonTwoHandedItem :: DecodeJson TwoHandedItem where + decodeJson = genericDecodeJson + +type BaseProps + = ( name :: String + , description :: String + , stats :: Stats + , levelRequirement :: Level + ) + +type EquipmentProps + = { | BaseProps } + +type WeaponProps + = { damage :: Int | BaseProps } + +type ShieldProps + = { block :: Int | BaseProps } + +derive instance genericEquipment :: Generic Equipment _ instance showEquipment :: Show Equipment where show = genericShow @@ -64,32 +198,45 @@ instance decodeJsonEquipment :: DecodeJson Equipment where decodeJson a = genericDecodeJson a levelRequirementOf :: Equipment -> Level -levelRequirementOf item = - case item of - Helmet armor' -> armor'.levelRequirement - ChestPlate armor' -> armor'.levelRequirement - Gloves armor' -> armor'.levelRequirement - LeggGuards armor' -> armor'.levelRequirement - Shoes armor' -> armor'.levelRequirement - LongSword weapon' -> weapon'.levelRequirement - GreatSword weapon' -> weapon'.levelRequirement - Dagger weapon' -> weapon'.levelRequirement - Bow weapon' -> weapon'.levelRequirement - Staff weapon' -> weapon'.levelRequirement - Shield shield' -> shield'.levelRequirement +levelRequirementOf = case _ of + Head h -> case h of + Helmet r -> r.levelRequirement + Body b -> case b of + ChestPlate r -> r.levelRequirement + Hands h -> case h of + Gloves r -> r.levelRequirement + Feet f -> case f of + Shoes r -> r.levelRequirement + HandItem h -> case h of + MainHandItem m -> case m of + LongSword r -> r.levelRequirement + Staff r -> r.levelRequirement + OffHandItem o -> case o of + Shield r -> r.levelRequirement + EitherHandItem e -> case e of + Dagger r -> r.levelRequirement + TwoHandedItem t -> case t of + GreatSword r -> r.levelRequirement + Bow r -> r.levelRequirement statsOf :: Equipment -> Stats -statsOf item = - case item of - Helmet armor' -> armor'.stats - ChestPlate armor' -> armor'.stats - Gloves armor' -> armor'.stats - LeggGuards armor' -> armor'.stats - Shoes armor' -> armor'.stats - LongSword weapon' -> weapon'.stats - GreatSword weapon' -> weapon'.stats - Dagger weapon' -> weapon'.stats - Bow weapon' -> weapon'.stats - Staff weapon' -> weapon'.stats - Shield shield' -> shield'.stats - +statsOf item = case item of + Head h -> case h of + Helmet r -> r.stats + Body b -> case b of + ChestPlate r -> r.stats + Hands h -> case h of + Gloves r -> r.stats + Feet f -> case f of + Shoes r -> r.stats + HandItem h -> case h of + MainHandItem m -> case m of + LongSword r -> r.stats + Staff r -> r.stats + OffHandItem o -> case o of + Shield r -> r.stats + EitherHandItem e -> case e of + Dagger r -> r.stats + TwoHandedItem t -> case t of + GreatSword r -> r.stats + Bow r -> r.stats diff --git a/src/Game/Data/Item/Hands.purs b/src/Game/Data/Item/Hands.purs new file mode 100644 index 0000000..5769f74 --- /dev/null +++ b/src/Game/Data/Item/Hands.purs @@ -0,0 +1,82 @@ +module Game.Data.Item.Hands where + +import Prelude +import Data.Either (Either(..)) +import Data.Either.Nested (type (\/)) +import Game.Data.Item.Equipment + ( HandItem(..) + , MainHandItem + , OffHandItem + , EitherHandItem + , TwoHandedItem + ) + +data Wielding + = None + | MainHand (MainHandItem \/ EitherHandItem) + | OffHand (OffHandItem \/ EitherHandItem) + | BothHands (MainHandItem \/ EitherHandItem) (OffHandItem \/ EitherHandItem) + | TwoHanded TwoHandedItem + +data EitherMode + = Main + | Off + | Max + +equip :: Boolean -> EitherMode -> HandItem -> Wielding -> Wielding +equip moveEither eitherMode item wielding = case item, wielding of + MainHandItem i, None -> MainHand $ Left i + OffHandItem i, None -> OffHand $ Left i + EitherHandItem i, None -> case eitherMode of + Main -> MainHand $ Right i + Off -> OffHand $ Right i + Max -> MainHand $ Right i + MainHandItem i, MainHand m -> case m of + Left _ -> MainHand $ Left i + Right e -> + if moveEither then + BothHands (Left i) (Right e) + else + MainHand $ Left i + OffHandItem i, MainHand m -> BothHands m $ Left i + EitherHandItem i, MainHand m -> case eitherMode of + Main -> case m of + Left _ -> MainHand $ Right i + Right e -> + if moveEither then + BothHands (Right i) (Right e) + else + MainHand $ Right i + Off -> BothHands m $ Right i + Max -> BothHands m $ Right i + MainHandItem m, OffHand o -> BothHands (Left m) o + OffHandItem i, OffHand o -> case o of + Left _ -> OffHand $ Left i + Right e -> + if moveEither then + BothHands (Right e) (Left i) + else + OffHand $ Left i + EitherHandItem i, OffHand o -> case eitherMode of + Main -> BothHands (Right i) o + Off -> case o of + Left _ -> OffHand $ Right i + Right e -> + if moveEither then + BothHands (Right e) (Right i) + else + OffHand $ Right i + Max -> BothHands (Right i) o + MainHandItem i, BothHands _ o -> BothHands (Left i) o + OffHandItem i, BothHands m _ -> BothHands m $ (Left i) + EitherHandItem i, BothHands m o -> case eitherMode of + Main -> BothHands (Right i) o + Off -> BothHands m (Right i) + Max -> BothHands (Right i) o + MainHandItem i, TwoHanded _ -> MainHand $ Left i + OffHandItem i, TwoHanded _ -> OffHand $ Left i + EitherHandItem i, TwoHanded _ -> case eitherMode of + Main -> MainHand $ Right i + Off -> OffHand $ Right i + Max -> MainHand $ Right i + TwoHandedItem i, _ -> TwoHanded i