From 6ac11277d6a73daf571d6ed2272a605bcca95dae Mon Sep 17 00:00:00 2001 From: teascade Date: Tue, 16 May 2017 11:36:31 +0300 Subject: [PATCH] Clean up InventoryActions a bit, close ##26 --- Assets/Scripts/Controls/InventoryInterface.cs | 4 +- Assets/Scripts/Controls/PlayerController.cs | 8 +-- .../Scripts/Entities/SyncBases/Character.cs | 10 ---- .../Scripts/Entities/SyncBases/Inventory.cs | 23 +++++++- Assets/Scripts/Items/InventoryAction.cs | 1 + .../Scripts/Items/InventoryActionHandler.cs | 57 +++++++++++++++++++ .../Items/InventoryActionHandler.cs.meta | 12 ++++ .../Scripts/Networking/Clientside/Client.cs | 30 +++------- .../Scripts/Networking/Serverside/Server.cs | 21 +------ 9 files changed, 107 insertions(+), 59 deletions(-) create mode 100644 Assets/Scripts/Items/InventoryActionHandler.cs create mode 100644 Assets/Scripts/Items/InventoryActionHandler.cs.meta diff --git a/Assets/Scripts/Controls/InventoryInterface.cs b/Assets/Scripts/Controls/InventoryInterface.cs index 42f5548..73b0e8d 100644 --- a/Assets/Scripts/Controls/InventoryInterface.cs +++ b/Assets/Scripts/Controls/InventoryInterface.cs @@ -149,10 +149,10 @@ namespace Cyber.Controls { Item Equipped = Inventory.Equipped.GetItem(SelectedItem.Slot); if (Equipped != null && Equipped.ID == SelectedItem.ID) { Inventory.Equipped.ClearSlot(SelectedItem.Slot); - Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Unequip, (int) SelectedItem.Slot)); + Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot)); } else { Inventory.Equipped.SetSlot(SelectedItem.Slot, SelectedItem); - Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Equip, SelectedItem.ID)); + Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(SelectedItem.ID)); } } } diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs index 7e13868..ac6e6f0 100644 --- a/Assets/Scripts/Controls/PlayerController.cs +++ b/Assets/Scripts/Controls/PlayerController.cs @@ -81,12 +81,12 @@ namespace Cyber.Controls { // Don't use equipment if you're interacting with something // (ie. don't shoot at the buttons) if (Input.GetButtonDown("Use Item (R)")) { - Character.UseItemInSlot(EquipSlot.RightHand); - Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Use, (int) EquipSlot.RightHand)); + Character.Inventory.UseItemInSlot(EquipSlot.RightHand); + Client.Send(PktType.InventoryAction, Character.Inventory.ActionHandler.BuildUseItem(EquipSlot.RightHand)); } if (Input.GetButtonDown("Use Item (L)")) { - Character.UseItemInSlot(EquipSlot.LeftHand); - Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Use, (int) EquipSlot.LeftHand)); + Character.Inventory.UseItemInSlot(EquipSlot.LeftHand); + Client.Send(PktType.InventoryAction, Character.Inventory.ActionHandler.BuildUseItem(EquipSlot.LeftHand)); } } } else if (Character.Moving()) { diff --git a/Assets/Scripts/Entities/SyncBases/Character.cs b/Assets/Scripts/Entities/SyncBases/Character.cs index e988eb3..da7c899 100644 --- a/Assets/Scripts/Entities/SyncBases/Character.cs +++ b/Assets/Scripts/Entities/SyncBases/Character.cs @@ -61,16 +61,6 @@ namespace Cyber.Entities.SyncBases { } } - /// - /// Uses the item in the left hand if something is equipped. - /// - public void UseItemInSlot(EquipSlot slot) { - Item Equipped = Inventory.Equipped.GetItem(slot); - if (Equipped != null && Equipped.Action != null) { - Equipped.Action(this); - } - } - /// /// Sets the character's rotation. /// diff --git a/Assets/Scripts/Entities/SyncBases/Inventory.cs b/Assets/Scripts/Entities/SyncBases/Inventory.cs index 458e207..38a61fd 100644 --- a/Assets/Scripts/Entities/SyncBases/Inventory.cs +++ b/Assets/Scripts/Entities/SyncBases/Inventory.cs @@ -4,7 +4,6 @@ using Cyber.Items; using Cyber.Networking; using Cyber.Networking.Serverside; using System.Collections.Generic; -using UnityEngine; using UnityEngine.Networking; namespace Cyber.Entities.SyncBases { @@ -24,6 +23,13 @@ namespace Cyber.Entities.SyncBases { /// public Equipped Equipped; + /// + /// The possible component associated with this Inventory. Used in . + /// + public Character Character; + + public InventoryActionHandler ActionHandler; + /// /// Creates the Inventory-component for a game object. /// @@ -63,6 +69,16 @@ namespace Cyber.Entities.SyncBases { return new SyncHandletype(true, 10); } + /// + /// Uses the item in the left hand if something is equipped. + /// + public void UseItemInSlot(EquipSlot slot) { + Item Item = Equipped.GetItem(slot); + if (Item != null && Item.Action != null && Character != null) { + Item.Action(Character); + } + } + /// /// Deserializes the ID's and creates them in the . /// @@ -144,5 +160,10 @@ namespace Cyber.Entities.SyncBases { writer.WriteBytesFull(EquippedByteArray[2]); writer.WriteBytesFull(EquippedByteArray[3]); } + + private void Start() { + Character = GetComponent(); + ActionHandler = new InventoryActionHandler(this, Character); + } } } diff --git a/Assets/Scripts/Items/InventoryAction.cs b/Assets/Scripts/Items/InventoryAction.cs index 8a66bf9..97374af 100644 --- a/Assets/Scripts/Items/InventoryAction.cs +++ b/Assets/Scripts/Items/InventoryAction.cs @@ -20,5 +20,6 @@ namespace Cyber.Items { /// Use the item in the given slot. /// Use + } } diff --git a/Assets/Scripts/Items/InventoryActionHandler.cs b/Assets/Scripts/Items/InventoryActionHandler.cs new file mode 100644 index 0000000..6dc005f --- /dev/null +++ b/Assets/Scripts/Items/InventoryActionHandler.cs @@ -0,0 +1,57 @@ + +using Cyber.Entities.SyncBases; +using Cyber.Networking.Clientside; +using Cyber.Networking.Messages; +using UnityEngine; + +namespace Cyber.Items { + + public class InventoryActionHandler { + + private Inventory Inventory; + private Character Character; + + public InventoryActionHandler(Inventory inventory, Character character) { + Inventory = inventory; + Character = character; + } + + public InventoryActionPkt BuildUseItem(EquipSlot slot) { + return new InventoryActionPkt(InventoryAction.Use, (int) slot); + } + + public InventoryActionPkt BuildClearSlot(EquipSlot slot) { + return new InventoryActionPkt(InventoryAction.Unequip, (int) slot); + } + + public InventoryActionPkt BuildEquipItem(int itemID) { + return new InventoryActionPkt(InventoryAction.Equip, itemID); + } + + public bool HandleAction(InventoryAction action, int relatedInt) { + switch (action) { + case InventoryAction.Equip: + Item Item = ItemDB.Singleton.Get(relatedInt); + Inventory.Equipped.SetSlot(Item.Slot, Item); + return true; + case InventoryAction.Unequip: + EquipSlot Slot = (EquipSlot) relatedInt; + Inventory.Equipped.ClearSlot(Slot); + return true; + case InventoryAction.Use: + EquipSlot UseSlot = (EquipSlot) relatedInt; + Item UseItem = Inventory.Equipped.GetItem(UseSlot); + if (UseItem != null && UseItem.Action != null && Character != null && + (!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character)) { + // Item exists, it has an action, and the character + // isn't controlled by the client (no double-actions). + UseItem.Action(Character); + return true; + } + return false; + } + return false; + } + + } +} diff --git a/Assets/Scripts/Items/InventoryActionHandler.cs.meta b/Assets/Scripts/Items/InventoryActionHandler.cs.meta new file mode 100644 index 0000000..cd92c37 --- /dev/null +++ b/Assets/Scripts/Items/InventoryActionHandler.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8a53f786f46ad6746a5367819aa32187 +timeCreated: 1494922673 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/Clientside/Client.cs b/Assets/Scripts/Networking/Clientside/Client.cs index f444c64..e4a5441 100644 --- a/Assets/Scripts/Networking/Clientside/Client.cs +++ b/Assets/Scripts/Networking/Clientside/Client.cs @@ -111,11 +111,14 @@ namespace Cyber.Networking.Clientside { } /// - /// Returns the connected player. + /// Returns the connected player, or null if no client is active. /// /// The connected player. public static CConnectedPlayer GetConnectedPlayer() { - return Singleton.Player; + if (IsRunning()) { + return Singleton.Player; + } + return null; } /// @@ -279,27 +282,8 @@ namespace Cyber.Networking.Clientside { break; } - switch (InventoryActionPkt.Action) { - case InventoryAction.Equip: - Item Item = ItemDB.Singleton.Get(InventoryActionPkt.RelatedInt); - Inventory.Equipped.SetSlot(Item.Slot, Item); - break; - case InventoryAction.Unequip: - EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt; - Inventory.Equipped.ClearSlot(Slot); - break; - case InventoryAction.Use: - EquipSlot UseSlot = (EquipSlot) InventoryActionPkt.RelatedInt; - Item UseItem = Inventory.Equipped.GetItem(UseSlot); - Character Character = CurrSyncBase.GetComponent(); - if (UseItem != null && UseItem.Action != null && Character != null && - Player.Character != Character) { - // Item exists, it has an action, and the character - // isn't controlled by the client (no double-actions). - UseItem.Action(Character); - } - break; - } + Inventory.ActionHandler.HandleAction(InventoryActionPkt.Action, InventoryActionPkt.RelatedInt); + break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType); diff --git a/Assets/Scripts/Networking/Serverside/Server.cs b/Assets/Scripts/Networking/Serverside/Server.cs index 6d7ec6f..844ddea 100644 --- a/Assets/Scripts/Networking/Serverside/Server.cs +++ b/Assets/Scripts/Networking/Serverside/Server.cs @@ -250,27 +250,10 @@ namespace Cyber.Networking.Serverside { Inventory CurrInventory = Character.GetComponent(); InventoryActionPkt.SyncBaseID = CurrInventory.ID; - switch (InventoryActionPkt.Action) { - case InventoryAction.Equip: - Item Item = ItemDB.Singleton.Get(InventoryActionPkt.RelatedInt); - CurrInventory.Equipped.SetSlot(Item.Slot, Item); - break; - case InventoryAction.Unequip: - EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt; - CurrInventory.Equipped.ClearSlot(Slot); - break; - case InventoryAction.Use: - EquipSlot UseSlot = (EquipSlot) InventoryActionPkt.RelatedInt; - Item UseItem = CurrInventory.Equipped.GetItem(UseSlot); - if (UseItem != null && UseItem.Action != null && Character != null) { - // Item exists, it has an action, and the character - // isn't controlled by the client (no double-actions). - UseItem.Action(Character); - } - break; + if (CurrInventory.ActionHandler.HandleAction(InventoryActionPkt.Action, InventoryActionPkt.RelatedInt)) { + SendToAll(PktType.InventoryAction, InventoryActionPkt); } - SendToAll(PktType.InventoryAction, InventoryActionPkt); break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType);