diff --git a/Assets/Scripts/EntityPlayer.cs b/Assets/Scripts/EntityPlayer.cs index b68b814..3e5c480 100644 --- a/Assets/Scripts/EntityPlayer.cs +++ b/Assets/Scripts/EntityPlayer.cs @@ -9,11 +9,14 @@ public enum ControlMode { public class EntityPlayer : Entity { + public Inventory inventory; + protected ControlMode controlMode; protected Animator animator; protected override void Start() { base.Start(); + inventory = new Inventory(); animator = GetComponent(); controlMode = ControlMode.Move; } diff --git a/Assets/Scripts/Inventory.cs b/Assets/Scripts/Inventory.cs new file mode 100644 index 0000000..c8341c7 --- /dev/null +++ b/Assets/Scripts/Inventory.cs @@ -0,0 +1,47 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[System.Serializable] +public class ItemNotInInventoryException : System.Exception +{ + public ItemNotInInventoryException() { } + public ItemNotInInventoryException(string message) : base(message) { } + public ItemNotInInventoryException(string message, System.Exception inner) : base(message, inner) { } + protected ItemNotInInventoryException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } +} + +public class Inventory { + public List items; + + public InventoryItem GetItemByName(string name) { + foreach(InventoryItem item in items) { + if(name == item.Name) + { + return item; + } + } + return null; + } + + public void AddItem(InventoryItem item) { + //Check if it's mergeable + InventoryItem other = GetItemByName(item.Name); + if(other == null) { + items.Add(item); + } + else { + other.quantity += item.quantity; + } + } + + public void UseItem(InventoryItem item) { + if(!items.Contains(item)) throw new ItemNotInInventoryException(); + item.Use(); + if(item.quantity <= 0) { + items.Remove(item); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Inventory.cs.meta b/Assets/Scripts/Inventory.cs.meta new file mode 100644 index 0000000..e21301a --- /dev/null +++ b/Assets/Scripts/Inventory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 52aeb2895cff09140a9bc5981d3efc8e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/InventoryItem.cs b/Assets/Scripts/InventoryItem.cs new file mode 100644 index 0000000..0f19ff1 --- /dev/null +++ b/Assets/Scripts/InventoryItem.cs @@ -0,0 +1,25 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class InventoryItem { + + public int quantity = 1; + + public virtual string Name { + //Two items with the same name will stack! + get { + Debug.LogError("No name given to an item"); + return ""; + } + } + + public virtual void Use() { + Debug.LogWarning("Use not overridden"); + quantity -= 1; + } + + public InventoryItem(int quantity) { + this.quantity = quantity; + } +} \ No newline at end of file diff --git a/Assets/Scripts/InventoryItem.cs.meta b/Assets/Scripts/InventoryItem.cs.meta new file mode 100644 index 0000000..4ee8abe --- /dev/null +++ b/Assets/Scripts/InventoryItem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: af1bf08d5ae9ead4796f0b9ade785254 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: