From e722011344f7d0f448a42566428df5729779bdac Mon Sep 17 00:00:00 2001 From: Ny Bruker <robinhs@stud.ntnu.no> Date: Fri, 30 Sep 2022 15:47:40 +0200 Subject: [PATCH] Added code to drop items and handle the effects of doing that. --- MrBigsock/Assets/Code/Character.cs | 13 +++++++++++ MrBigsock/Assets/Code/Core/ICharacterStats.cs | 15 +++++++++++++ MrBigsock/Assets/Code/Item/Inventory.cs | 22 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/MrBigsock/Assets/Code/Character.cs b/MrBigsock/Assets/Code/Character.cs index 654604a1..f7564239 100644 --- a/MrBigsock/Assets/Code/Character.cs +++ b/MrBigsock/Assets/Code/Character.cs @@ -253,6 +253,19 @@ namespace BigSock { print($"[Character.TryPickUpItem()] {item.Name} NOT picked up. ({Inventory.Items.Count}/{Inventory.Cap})"); return false; } + + /* + Try to drop an item. + */ + public bool TryDropItem(IItem item) { + if(Inventory.RemoveItem(item)) { + UpdateModifiers(); + print($"[Character.TryDropItem()] {item.Name} dropped. ({Inventory.Items.Count}/{Inventory.Cap})"); + return true; + } + print($"[Character.TryDropItem()] {item.Name} NOT dropped. ({Inventory.Items.Count}/{Inventory.Cap})"); + return false; + } } /* diff --git a/MrBigsock/Assets/Code/Core/ICharacterStats.cs b/MrBigsock/Assets/Code/Core/ICharacterStats.cs index afccfaf2..fdc3f74a 100644 --- a/MrBigsock/Assets/Code/Core/ICharacterStats.cs +++ b/MrBigsock/Assets/Code/Core/ICharacterStats.cs @@ -72,6 +72,21 @@ namespace BigSock { AttackSpeed = a.AttackSpeed + b.AttackSpeed, }; } + + /* + Removes the values of one stat object from the other. + Effectively a - b. + */ + public static ICharacterStats Remove(this ICharacterStats a, ICharacterStats b) { + return new CharacterStats{ + MaxHP = a.MaxHP - b.MaxHP, + Damage = a.Damage - b.Damage, + MoveSpeed = a.MoveSpeed - b.MoveSpeed, + Knockback = a.Knockback - b.Knockback, + Range = a.Range - b.Range, + AttackSpeed = a.AttackSpeed - b.AttackSpeed, + }; + } /* Multiplies the values of 2 character stats together. diff --git a/MrBigsock/Assets/Code/Item/Inventory.cs b/MrBigsock/Assets/Code/Item/Inventory.cs index 2b8a75e1..7defaf7e 100644 --- a/MrBigsock/Assets/Code/Item/Inventory.cs +++ b/MrBigsock/Assets/Code/Item/Inventory.cs @@ -52,6 +52,8 @@ namespace BigSock.Item { if(item is PassiveItemBase passive) { Modifier = Modifier.Add(passive.Modifier); } + + // Add the listener for this item to the owner. else if(item is ConditionalItemBase conditional) { Owner.AddItemListener(conditional); } @@ -60,5 +62,25 @@ namespace BigSock.Item { return true; } + /* + Removes an item from the inventory and manages changes. + */ + public bool RemoveItem(IItem item) { + if(!Items.Remove(item)) return false; + + // Remove the passive effects from the modifier. + if(item is PassiveItemBase passive) { + Modifier = Modifier.Remove(passive.Modifier); + } + + // Remove the listener for this item from the owner. + else if(item is ConditionalItemBase conditional) { + Owner.RemoveItemListener(conditional); + } + //! Add ifs to handle the other 2 types of items. + + return true; + } + } } \ No newline at end of file -- GitLab