From db95bb36402b8a27a1c9eaba398a2efffec815a7 Mon Sep 17 00:00:00 2001 From: teascade Date: Sun, 14 May 2017 22:16:20 +0300 Subject: [PATCH] Add Equipment system backend, close #24 --- Assets/Scripts/Entities/InteractionType.cs | 20 +++++- .../Scripts/Entities/SyncBases/Inventory.cs | 43 ++++++++++++ Assets/Scripts/Items/EquipSlot.cs | 25 +++++++ Assets/Scripts/Items/EquipSlot.cs.meta | 12 ++++ Assets/Scripts/Items/Equipped.cs | 66 +++++++++++++++++++ Assets/Scripts/Items/Equipped.cs.meta | 12 ++++ 6 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Items/EquipSlot.cs create mode 100644 Assets/Scripts/Items/EquipSlot.cs.meta create mode 100644 Assets/Scripts/Items/Equipped.cs create mode 100644 Assets/Scripts/Items/Equipped.cs.meta diff --git a/Assets/Scripts/Entities/InteractionType.cs b/Assets/Scripts/Entities/InteractionType.cs index 1416039..827cb90 100644 --- a/Assets/Scripts/Entities/InteractionType.cs +++ b/Assets/Scripts/Entities/InteractionType.cs @@ -6,7 +6,25 @@ namespace Cyber.Entities { /// public enum InteractionType : byte { - Activate, Deactivate, Enter, Exit + /// + /// The is clicked or otherwise activated. + /// + Activate, + + /// + /// The is called when for example the mouse is released of it. + /// + Deactivate, + + /// + /// When the is hovered on. + /// + Enter, + + /// + /// + /// + Exit } } diff --git a/Assets/Scripts/Entities/SyncBases/Inventory.cs b/Assets/Scripts/Entities/SyncBases/Inventory.cs index 28af025..369fb7d 100644 --- a/Assets/Scripts/Entities/SyncBases/Inventory.cs +++ b/Assets/Scripts/Entities/SyncBases/Inventory.cs @@ -3,6 +3,7 @@ using Cyber.Console; using Cyber.Items; using Cyber.Networking; using Cyber.Networking.Serverside; +using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; @@ -18,11 +19,17 @@ namespace Cyber.Entities.SyncBases { /// public Drive Drive; + /// + /// This entity's s. + /// + public Equipped Equipped; + /// /// Creates the Inventory-component for a game object. /// public Inventory() { Drive = new Drive(10f); + Equipped = new Equipped(); if (Server.IsRunning()) { Drive.AddItem(ItemDB.Singleton.Get(0)); Drive.AddItem(ItemDB.Singleton.Get(1)); @@ -39,6 +46,10 @@ namespace Cyber.Entities.SyncBases { for (int i = 0; i < Items.Length; i++) { Checksum ^= Items[i].ID; } + var EquippedItems = Equipped.GetEquippedList().ToArray(); + for (int i = 0; i < EquippedItems.Length; i++) { + Checksum ^= EquippedItems[i].ID; + } return Checksum; } @@ -68,6 +79,20 @@ namespace Cyber.Entities.SyncBases { foreach (int id in IDs) { Drive.AddItem(ItemDB.Singleton.Get(id)); } + + 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])); + } } /// @@ -85,6 +110,24 @@ namespace Cyber.Entities.SyncBases { writer.WriteBytesFull(ByteArray[1]); writer.WriteBytesFull(ByteArray[2]); writer.WriteBytesFull(ByteArray[3]); + + var slotList = new List(Equipped.GetEquippedDict().Keys).ConvertAll(x => (byte) x); + 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]); } } } diff --git a/Assets/Scripts/Items/EquipSlot.cs b/Assets/Scripts/Items/EquipSlot.cs new file mode 100644 index 0000000..68d804a --- /dev/null +++ b/Assets/Scripts/Items/EquipSlot.cs @@ -0,0 +1,25 @@ + +namespace Cyber.Items { + + /// + /// Represents an slot where an item can be equipped. + /// + public enum EquipSlot : byte { + + /// + /// On top of the head + /// + Hat, + + /// + /// Right hand + /// + RightHand, + + /// + /// Left hand + /// + LeftHand + + } +} diff --git a/Assets/Scripts/Items/EquipSlot.cs.meta b/Assets/Scripts/Items/EquipSlot.cs.meta new file mode 100644 index 0000000..c5359d5 --- /dev/null +++ b/Assets/Scripts/Items/EquipSlot.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 9c224eeda321b6c4ab1362602de6217b +timeCreated: 1494785848 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Items/Equipped.cs b/Assets/Scripts/Items/Equipped.cs new file mode 100644 index 0000000..c4d072d --- /dev/null +++ b/Assets/Scripts/Items/Equipped.cs @@ -0,0 +1,66 @@ + +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 new file mode 100644 index 0000000..0c0ee5c --- /dev/null +++ b/Assets/Scripts/Items/Equipped.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 1bc32847ce18a3b4987872f04887b6de +timeCreated: 1494785490 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: