From 6c1669d1cc6c8d3311f3df14ffcfec60d8678d0e Mon Sep 17 00:00:00 2001 From: teascade Date: Tue, 16 May 2017 20:35:38 +0300 Subject: [PATCH] Revamp inventory, make it into a 1D array --- Assets/Scripts/Controls/EquipmentInterface.cs | 2 +- Assets/Scripts/Controls/InventoryInterface.cs | 13 +- .../Scripts/Entities/SyncBases/Inventory.cs | 90 +++------- Assets/Scripts/Items/Drive.cs | 155 ++++++++++++++++-- Assets/Scripts/Items/DriveInterface.cs | 116 ------------- Assets/Scripts/Items/Equipped.cs | 66 -------- Assets/Scripts/Items/Equipped.cs.meta | 12 -- .../Scripts/Items/InventoryActionHandler.cs | 13 +- Assets/Scripts/Items/Slot.cs | 30 ++++ .../{DriveInterface.cs.meta => Slot.cs.meta} | 4 +- 10 files changed, 209 insertions(+), 292 deletions(-) delete mode 100644 Assets/Scripts/Items/DriveInterface.cs delete mode 100644 Assets/Scripts/Items/Equipped.cs delete mode 100644 Assets/Scripts/Items/Equipped.cs.meta create mode 100644 Assets/Scripts/Items/Slot.cs rename Assets/Scripts/Items/{DriveInterface.cs.meta => Slot.cs.meta} (76%) diff --git a/Assets/Scripts/Controls/EquipmentInterface.cs b/Assets/Scripts/Controls/EquipmentInterface.cs index 58370ab..5ecd552 100644 --- a/Assets/Scripts/Controls/EquipmentInterface.cs +++ b/Assets/Scripts/Controls/EquipmentInterface.cs @@ -48,7 +48,7 @@ namespace Cyber.Controls { private void Update() { if (Time.time - LastUpdateTime >= 1f / UpdateFrequency) { - Dictionary Equips = Inventory.Equipped.GetEquippedDict(); + Dictionary Equips = Inventory.Drive.GetEquippedItems(); // Empty all slots for (int i = 0; i < Slots.Length; i++) { Slots[i].Mesh.mesh = null; diff --git a/Assets/Scripts/Controls/InventoryInterface.cs b/Assets/Scripts/Controls/InventoryInterface.cs index 73b0e8d..5cceeb0 100644 --- a/Assets/Scripts/Controls/InventoryInterface.cs +++ b/Assets/Scripts/Controls/InventoryInterface.cs @@ -143,16 +143,15 @@ namespace Cyber.Controls { } if (Input.GetButtonDown("Equip")) { // Selected index was already this => equip (double-clicked) - Item SelectedItem = Inventory.Drive.Interface.GetItemAt(ItemGridSelectedIndex % (int) ItemGridDimensions.x, - ItemGridSelectedIndex / (int) ItemGridDimensions.y); + Item SelectedItem = Inventory.Drive.GetItemAt(ItemGridSelectedIndex); if (SelectedItem != null) { - Item Equipped = Inventory.Equipped.GetItem(SelectedItem.Slot); + Item Equipped = Inventory.Drive.GetSlot(SelectedItem.Slot); if (Equipped != null && Equipped.ID == SelectedItem.ID) { - Inventory.Equipped.ClearSlot(SelectedItem.Slot); + Inventory.Drive.UnequipSlot(SelectedItem.Slot); Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot)); } else { - Inventory.Equipped.SetSlot(SelectedItem.Slot, SelectedItem); - Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(SelectedItem.ID)); + Inventory.Drive.EquipItem(ItemGridSelectedIndex); + Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(ItemGridSelectedIndex)); } } } @@ -206,7 +205,7 @@ namespace Cyber.Controls { for (int x = 0; x < ItemGridDimensions.x; x++) { // Find the item and mesh int i = x + y * (int) ItemGridDimensions.x; - Item Item = Inventory.Drive.Interface.GetItemAt(x, y); + Item Item = Inventory.Drive.GetItemAt(x + y * (int) ItemGridDimensions.y); Mesh Mesh = null; if (Item != null) { Mesh = MeshDB.GetMesh(Item.ModelID); diff --git a/Assets/Scripts/Entities/SyncBases/Inventory.cs b/Assets/Scripts/Entities/SyncBases/Inventory.cs index 1351830..88e904e 100644 --- a/Assets/Scripts/Entities/SyncBases/Inventory.cs +++ b/Assets/Scripts/Entities/SyncBases/Inventory.cs @@ -4,6 +4,7 @@ using Cyber.Items; using Cyber.Networking; using Cyber.Networking.Serverside; using System.Collections.Generic; +using UnityEngine; using UnityEngine.Networking; namespace Cyber.Entities.SyncBases { @@ -18,11 +19,6 @@ namespace Cyber.Entities.SyncBases { /// public Drive Drive; - /// - /// This entity's s. - /// - public Equipped Equipped; - /// /// The possible component associated with this Inventory. Used in . /// @@ -38,7 +34,6 @@ namespace Cyber.Entities.SyncBases { /// public Inventory() { Drive = new Drive(10f); - Equipped = new Equipped(); if (Server.IsRunning()) { Drive.AddItem(ItemDB.Singleton.Get(0)); Drive.AddItem(ItemDB.Singleton.Get(1)); @@ -51,15 +46,11 @@ namespace Cyber.Entities.SyncBases { /// /// A checksum of the IDs of the items public override int GenerateChecksum() { - var Items = Drive.GetItems().ToArray(); + var Slots = Drive.GetSlots(); int Checksum = 0; - for (int i = 0; i < Items.Length; i++) { + for (int i = 0; i < Slots.Length; i++) { // Times with primes and sprinkle some i to spice up the stew - Checksum += (Items[i].ID + 1) * 509 * (i + 1) * 53; - } - var EquippedItems = Equipped.GetEquippedList().ToArray(); - for (int i = 0; i < EquippedItems.Length; i++) { - Checksum += (EquippedItems[i].ID + 1) * 859 * (i + 1) * 97; + Checksum += (((Slots[i].Item != null) ? Slots[i].Item.ID : i) + 1) * 509 * (i + 1) * 53 + (Slots[i].Equipped ? 1789 : 431); } return Checksum; } @@ -76,7 +67,7 @@ namespace Cyber.Entities.SyncBases { /// Uses the item in the left hand if something is equipped. /// public void UseItemInSlot(EquipSlot slot) { - Item Item = Equipped.GetItem(slot); + Item Item = Drive.GetSlot(slot); if (Item != null && Item.Action != null && Character != null) { Item.Action(Character); } @@ -95,29 +86,15 @@ namespace Cyber.Entities.SyncBases { ByteArray[3] = reader.ReadBytesAndSize(); int[] IDs = NetworkHelper.DeserializeIntArray(ByteArray); + byte[] Equippeds = reader.ReadBytesAndSize(); + Drive.Clear(); - foreach (int id in IDs) { - Drive.AddItem(ItemDB.Singleton.Get(id)); - } - - bool ReceivedSlots = reader.ReadBoolean(); - if (!ReceivedSlots) { - Equipped.ClearAllEquipped(); - return; - } - - byte[] Slots = reader.ReadBytesAndSize(); - - byte[][] EquippedIdsBytes = new byte[4][]; - EquippedIdsBytes[0] = reader.ReadBytesAndSize(); - EquippedIdsBytes[1] = reader.ReadBytesAndSize(); - EquippedIdsBytes[2] = reader.ReadBytesAndSize(); - EquippedIdsBytes[3] = reader.ReadBytesAndSize(); - int[] EquippedIds = NetworkHelper.DeserializeIntArray(EquippedIdsBytes); - - Equipped.ClearAllEquipped(); - for (int i = 0; i < Slots.Length; i++) { - Equipped.SetSlot((EquipSlot) Slots[i], ItemDB.Singleton.Get(EquippedIds[i])); + for (int i = 0; i < IDs.Length; i++) { + int ID = IDs[i]; + if (ID >= 0) { + Drive.AddItemToIndex(ItemDB.Singleton.Get(ID), i); + Drive.GetSlots()[i].Equipped = (Equippeds[i] == 1 ? true : false); + } } } @@ -126,11 +103,19 @@ namespace Cyber.Entities.SyncBases { /// /// public override void Serialize(NetworkWriter writer) { - var Items = Drive.GetItems(); - int[] IDs = new int[Items.Count]; - for (int i = 0; i < Items.Count; i++) { - IDs[i] = Items[i].ID; + Slot[] Slots = Drive.GetSlots(); + + int[] IDs = new int[Slots.Length]; + byte[] Equippeds = new byte[Slots.Length]; + for (int i = 0; i < Slots.Length; i++) { + if (Slots[i].Item == null) { + IDs[i] = -1; + } else { + IDs[i] = Slots[i].Item.ID; + } + Equippeds[i] = (byte) ((Slots[i].Equipped) ? 1 : 0); } + byte[][] ByteArray = NetworkHelper.SerializeIntArray(IDs); writer.WriteBytesFull(ByteArray[0]); @@ -138,30 +123,7 @@ namespace Cyber.Entities.SyncBases { writer.WriteBytesFull(ByteArray[2]); writer.WriteBytesFull(ByteArray[3]); - var slotList = new List(Equipped.GetEquippedDict().Keys).ConvertAll(x => (byte) x); - - if (slotList.Count > 0) { - writer.Write(true); - } else { - writer.Write(false); - } - - slotList.Sort((a, b) => { - return b - a; - }); - - var idList = new List(); - slotList.ForEach(x => { - idList.Add(Equipped.GetItem((EquipSlot) x).ID); - }); - - writer.WriteBytesFull(slotList.ToArray()); - - byte[][] EquippedByteArray = NetworkHelper.SerializeIntArray(idList.ToArray()); - writer.WriteBytesFull(EquippedByteArray[0]); - writer.WriteBytesFull(EquippedByteArray[1]); - writer.WriteBytesFull(EquippedByteArray[2]); - writer.WriteBytesFull(EquippedByteArray[3]); + writer.WriteBytesFull(Equippeds); } private void Start() { diff --git a/Assets/Scripts/Items/Drive.cs b/Assets/Scripts/Items/Drive.cs index 3d5fdf1..3b1d0ca 100644 --- a/Assets/Scripts/Items/Drive.cs +++ b/Assets/Scripts/Items/Drive.cs @@ -8,25 +8,19 @@ namespace Cyber.Items { /// public class Drive { - private List Items = new List(); + private Slot[] Slots = new Slot[0]; /// /// The capacity of the drive, meaning how much stuff can this drive contain. /// public readonly float Capacity; - /// - /// The interface-class of this Drive. - /// - public DriveInterface Interface; - /// /// Creates a drive with given capacity. Capacity cannot be changed after this. /// /// Capacity of the drive public Drive(float capacity) { Capacity = capacity; - Interface = new DriveInterface(this); } /// @@ -35,8 +29,10 @@ namespace Cyber.Items { /// The totam weight public float TotalWeight() { float sum = 0; - foreach (Item item in Items) { - sum += item.Weight; + foreach (Slot slot in Slots) { + if (slot.Item != null) { + sum += slot.Item.Weight; + } } return sum; } @@ -54,7 +50,7 @@ namespace Cyber.Items { /// /// public void Clear() { - Items.Clear(); + Slots = new Slot[0]; } /// @@ -65,21 +61,50 @@ namespace Cyber.Items { if (item.Weight > FreeSpace()) { return false; } - Interface.AddNewItem(Items.Count); - Items.Add(item); + bool foundEmpty = false; + for (int i = 0; i < Slots.Length; i++) { + if (Slots[i].Item == null) { + Slots[i] = new Slot(item, false); + foundEmpty = true; + } + } + if (!foundEmpty) { + // Add space and set the item there + IncreaseCapacity(1); + Slots[Slots.Length - 1] = new Slot(item, false); + } return true; } + /// + /// Tries to add an item to a specific slot. Returns false if slot already occupied. + /// + /// Item to add. + /// Index in the inventory. + /// Weather the slot was empty or not. + public bool AddItemToIndex(Item item, int idx) { + if (idx < 0) { + return false; + } + if (GetItemAt(idx) == null) { + if (Slots.Length > idx) { + Slots[idx].Item = item; + } else { + IncreaseCapacity(idx - Slots.Length + 1); + Slots[idx].Item = item; + } + return true; + } + return false; + } + /// /// Gets the item at the given index, or null if there is nothing. /// /// The index of the desired item /// The item or null if nothing was found. - public Item GetItem(int idx) { - if (idx < 0 || idx >= Items.Count) { - return null; - } - return Items[idx]; + public Item GetItemAt(int idx) { + return GetSlotAt(idx).Item; } /// @@ -87,8 +112,104 @@ namespace Cyber.Items { /// /// public List GetItems() { + List Items = new List(); + foreach (Slot slot in Slots) { + if (slot.Item != null) { + Items.Add(slot.Item); + } + } return Items; } + /// + /// Gets the item at the given slot. Loops through all items and checks if there is an item equipped at the given slot and returns it if there is. + /// + /// The desired slot + /// The item at the slot, or null if no items + public Item GetSlot(EquipSlot slot) { + Item Item = null; + foreach (Slot s in Slots) { + if (s.Item != null && s.Item.Slot == slot && s.Equipped) { + Item = s.Item; + break; + } + } + return Item; + } + + /// + /// Returns all the equipped items in a dictionary. + /// + /// The dictionary of items. + public Dictionary GetEquippedItems() { + Dictionary Items = new Dictionary(); + foreach (Slot s in Slots) { + if (s.Item != null && s.Equipped) { + Items.Add(s.Item.Slot, s.Item); + } + } + return Items; + } + + /// + /// Attempt to equip the item at the given index. + /// + /// Equip the iten at the given slot + /// Weather the item could be equipped or if there already exists something on that equip slot. + public bool EquipItem(int idx) { + Slot Slot = GetSlotAt(idx); + if (Slot.Item != null && GetSlot(Slot.Item.Slot) == null) { + Slots[idx].Equipped = true; + return true; + } + return false; + } + + /// + /// Unequips the item at the given inventory index. + /// + /// The index to unequip. + public void UnequipItem(int idx) { + Slot Slot = GetSlotAt(idx); + if (Slot.Item != null) { + Slots[idx].Equipped = false; + } + } + + /// + /// Unequip all items that are in this slot. + /// + /// The slot of the desired item to be unequipped. + public void UnequipSlot(EquipSlot equipSlot) { + for (int i = 0; i < Slots.Length; i++) { + if (Slots[i].Item != null && Slots[i].Item.Slot == equipSlot) { + Slots[i].Equipped = false; + } + } + } + + /// + /// Simply returns the slots-array. The fastest way to get all of inventory if needed. + /// + /// The Slot-structs + public Slot[] GetSlots() { + return Slots; + } + + private void IncreaseCapacity(int moreCapacity) { + Slot[] NewSlots = new Slot[Slots.Length + moreCapacity]; + for (int i = 0; i < Slots.Length; i++) { + NewSlots[i] = Slots[i]; + } + Slots = NewSlots; + } + + private Slot GetSlotAt(int idx) { + if (idx < 0 || idx >= Slots.Length) { + return new Slot(null, false); + } + return Slots[idx]; + } + } } diff --git a/Assets/Scripts/Items/DriveInterface.cs b/Assets/Scripts/Items/DriveInterface.cs deleted file mode 100644 index 2081515..0000000 --- a/Assets/Scripts/Items/DriveInterface.cs +++ /dev/null @@ -1,116 +0,0 @@ - -using UnityEngine; - -namespace Cyber.Items { - - /// - /// The interface, which contains a grid of indices of items in the . Use to get items in the interface. - /// - public class DriveInterface { - - /// - /// Width of the interface. - /// - public const int Width = 7; - - /// - /// Minimun height of the interface. - /// - public const int MinHeight = 4; - - private int[,] ItemGrid; - - private Drive Drive; - - /// - /// Creates a Drive interface for a . - /// - /// - public DriveInterface(Drive drive) { - Drive = drive; - ItemGrid = CreateEmptyGrid(Width, 4); - } - - /// - /// Returns the item at the specified coordinate on the interface. Returns null if invalid or empty coordinate. - /// - /// The x-coordinate - /// The y-coordinate - /// The item or null - public Item GetItemAt(int x, int y) { - if (y < 0 || x < 0 || y >= GetHeight() || x >= GetWidth() || - ItemGrid[y, x] == -1) { - return null; - } else { - return Drive.GetItem(ItemGrid[y, x]); - } - } - - /// - /// Gets the Width of the interface, or simply . - /// - /// - public int GetWidth() { - return Width; - } - - /// - /// Gets the current height of the interface - /// - /// - public int GetHeight() { - return ItemGrid.GetLength(0); - } - - /// - /// Updates the height of the interface, adding new rows or deleting old useless ones. - /// - public void UpdateHeight() { - int RequiredHeight = MinHeight; - for (int y = MinHeight; y < GetHeight(); y++) { - for (int x = 0; x < Width; x++) { - if (GetItemAt(x, y) == null || (x == Width - 1 && y == GetHeight() - 1)) { - RequiredHeight = y; - } - } - } - - int[,] Temp = CreateEmptyGrid(Width, RequiredHeight + 1); - for (int y = 0; y < RequiredHeight - 1; y++) { - for (int x = 0; x < Width; x++) { - if (GetItemAt(x, y) != null) { - Temp[y, x] = Drive.GetItems().IndexOf(GetItemAt(x, y)); - } - } - } - - ItemGrid = Temp; - } - - /// - /// Adds a new item to the grid. The idx in the parameter is the idx of the item in the drive. - /// - /// - public void AddNewItem(int idx) { - UpdateHeight(); - for (int y = 0; y < GetHeight(); y++) { - for (int x = 0; x < Width; x++) { - if (GetItemAt(x, y) == null) { - ItemGrid[y, x] = idx; - return; - } - } - } - } - - private int[,] CreateEmptyGrid(int width, int height) { - int[,] Grid = new int[height, width]; - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - Grid[y, x] = -1; - } - } - return Grid; - } - } -} diff --git a/Assets/Scripts/Items/Equipped.cs b/Assets/Scripts/Items/Equipped.cs deleted file mode 100644 index c4d072d..0000000 --- a/Assets/Scripts/Items/Equipped.cs +++ /dev/null @@ -1,66 +0,0 @@ - -using System.Collections.Generic; - -namespace Cyber.Items { - - /// - /// Represents the equipped items at given slots. - /// - public class Equipped { - - Dictionary EquippedItems = new Dictionary(); - - /// - /// Inserts an item here, marking it as 'equipped'. - /// - /// The slot to equip the item to. - /// The item to equip. - public void SetSlot(EquipSlot slot, Item item) { - EquippedItems[slot] = item; - } - - /// - /// Empties the desired slot of any items. - /// - /// The slot to empty. - public void ClearSlot(EquipSlot slot) { - EquippedItems.Remove(slot); - } - - /// - /// Returns the item at the given slot, or null if no item at the slot was found. - /// - /// - /// - public Item GetItem(EquipSlot slot) { - if (EquippedItems.ContainsKey(slot)) { - return EquippedItems[slot]; - } - return null; - } - - /// - /// Returns a dictionary of all equipped items. - /// - /// Dictionary of equipped items. - public Dictionary GetEquippedDict() { - return EquippedItems; - } - - /// - /// Returns a list of all items that are generally equipped. - /// - /// List of equipped items. - public List GetEquippedList() { - return new List(EquippedItems.Values); - } - - /// - /// Clears all equipped items, removing them from their slots. - /// - public void ClearAllEquipped() { - EquippedItems.Clear(); - } - - } -} diff --git a/Assets/Scripts/Items/Equipped.cs.meta b/Assets/Scripts/Items/Equipped.cs.meta deleted file mode 100644 index 0c0ee5c..0000000 --- a/Assets/Scripts/Items/Equipped.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 1bc32847ce18a3b4987872f04887b6de -timeCreated: 1494785490 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Items/InventoryActionHandler.cs b/Assets/Scripts/Items/InventoryActionHandler.cs index ddc584b..e41169a 100644 --- a/Assets/Scripts/Items/InventoryActionHandler.cs +++ b/Assets/Scripts/Items/InventoryActionHandler.cs @@ -44,10 +44,10 @@ namespace Cyber.Items { /// /// Builds a packet. /// - /// The item ID to equip. + /// The item index to equip. /// - public InventoryActionPkt BuildEquipItem(int itemID) { - return new InventoryActionPkt(InventoryAction.Equip, itemID); + public InventoryActionPkt BuildEquipItem(int itemIdx) { + return new InventoryActionPkt(InventoryAction.Equip, itemIdx); } /// @@ -59,16 +59,15 @@ namespace Cyber.Items { public bool HandleAction(InventoryAction action, int relatedInt) { switch (action) { case InventoryAction.Equip: - Item Item = ItemDB.Singleton.Get(relatedInt); - Inventory.Equipped.SetSlot(Item.Slot, Item); + Inventory.Drive.EquipItem(relatedInt); return true; case InventoryAction.Unequip: EquipSlot Slot = (EquipSlot) relatedInt; - Inventory.Equipped.ClearSlot(Slot); + Inventory.Drive.UnequipSlot(Slot); return true; case InventoryAction.Use: EquipSlot UseSlot = (EquipSlot) relatedInt; - Item UseItem = Inventory.Equipped.GetItem(UseSlot); + Item UseItem = Inventory.Drive.GetSlot(UseSlot); if (UseItem != null && UseItem.Action != null && Character != null && (!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character)) { // Item exists, it has an action, and the character diff --git a/Assets/Scripts/Items/Slot.cs b/Assets/Scripts/Items/Slot.cs new file mode 100644 index 0000000..e4f70ee --- /dev/null +++ b/Assets/Scripts/Items/Slot.cs @@ -0,0 +1,30 @@ + +namespace Cyber.Items { + + /// + /// Represents a slot which contains an item and a bool weather the item is equipped or not. + /// + public struct Slot { + + /// + /// The item that this slot holds. If this is null, the slot is empty. + /// + public Item Item; + + /// + /// Weather this item is equipped or not. + /// + public bool Equipped; + + /// + /// Creates a slot. + /// + /// Item in the slot, or null if the slot is empty. + /// Weather this slot is equipped or not. + public Slot(Item item, bool equipped) { + Item = item; + Equipped = equipped; + } + + } +} diff --git a/Assets/Scripts/Items/DriveInterface.cs.meta b/Assets/Scripts/Items/Slot.cs.meta similarity index 76% rename from Assets/Scripts/Items/DriveInterface.cs.meta rename to Assets/Scripts/Items/Slot.cs.meta index 1770724..e211ec9 100644 --- a/Assets/Scripts/Items/DriveInterface.cs.meta +++ b/Assets/Scripts/Items/Slot.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: fc91d98b617f041da8ff205f4252ed06 -timeCreated: 1494705607 +guid: ca2662d3bcd2ddf4d94a410f907d028d +timeCreated: 1494949326 licenseType: Free MonoImporter: serializedVersion: 2