From 8b579e010b121b539ad35d2088b8fdb286dd6a9e Mon Sep 17 00:00:00 2001 From: stilnat Date: Thu, 27 Jul 2023 11:04:54 +0200 Subject: [PATCH] clean and doc --- .../Systems/Inventory/Containers/Cloth.cs | 4 ++ .../Systems/Inventory/Containers/ClothType.cs | 9 ++--- .../Inventory/Containers/ClothedBodyPart.cs | 3 ++ .../Inventory/Containers/ClothesDisplayer.cs | 39 ++++++++++--------- .../Inventory/Containers/ContainerView.cs | 12 ++++-- .../Inventory/Containers/ContainerViewer.cs | 36 ++++++----------- .../Systems/Inventory/Containers/Hands.cs | 7 +++- .../Inventory/Containers/HumanInventory.cs | 16 ++++++++ .../Interactions/ViewContainerInteraction.cs | 2 +- .../SS3D/Systems/Inventory/UI/DummySlot.cs | 5 +++ ...ementAlt.cs => InventoryDisplayElement.cs} | 0 ...s.meta => InventoryDisplayElement.cs.meta} | 0 12 files changed, 80 insertions(+), 53 deletions(-) rename Assets/Scripts/SS3D/Systems/Inventory/UI/{InventoryDisplayElementAlt.cs => InventoryDisplayElement.cs} (100%) rename Assets/Scripts/SS3D/Systems/Inventory/UI/{InventoryDisplayElementAlt.cs.meta => InventoryDisplayElement.cs.meta} (100%) diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/Cloth.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/Cloth.cs index a75c4e98ca..8f0308d792 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/Cloth.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/Cloth.cs @@ -5,6 +5,10 @@ namespace SS3D.Systems.Inventory.Containers { + /// + /// Script to put on cloth, which are gameobjects going to the clothes slots. + /// In the future, should be the folded models, and could contain a reference to the worn mesh version (and maybe torn mesh version, stuff like that...) + /// public class Cloth : MonoBehaviour { [SerializeField] diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothType.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothType.cs index ce5cca8488..c67ccf0ae3 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothType.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothType.cs @@ -1,9 +1,8 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -namespace SS3D.Systems.Inventory.Containers +namespace SS3D.Systems.Inventory.Containers { + /// + /// List of different types of clothes, mostly useful to decide what can go where. + /// public enum ClothType { ShoeLeft, diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothedBodyPart.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothedBodyPart.cs index 437f2308b7..8760ae0b25 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothedBodyPart.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothedBodyPart.cs @@ -4,6 +4,9 @@ namespace SS3D.Systems.Inventory.Containers { + /// + /// Used on bodypart of entities to indicate which type of clothes can be worn on them. + /// public class ClothedBodyPart : MonoBehaviour { [SerializeField] diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothesDisplayer.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothesDisplayer.cs index 7542ce87aa..0c25c453cc 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothesDisplayer.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothesDisplayer.cs @@ -9,21 +9,11 @@ using UnityEditor; using FishNet.Object; -// nouvel item -// add item -// item has a cloth type -// find network object with same clothtype -// add new clothdisplay data using this clothtype and item to display - -// remove item -// item has cloth type -// find network object with same clothtype -// remove cloth display data using this cloth type and item to display. namespace SS3D.Systems.Inventory.Containers { /// - /// Display cloth on player for all clients. + /// Display clothes on player for all clients. /// public class ClothesDisplayer : NetworkActor { @@ -42,18 +32,17 @@ public ClothDisplayData(NetworkObject bodyPart, Item clothToDisplay) public Item _clothToDisplay; } + /// + /// The inventory containing the player's clothing slots. + /// public HumanInventory _inventory; - // The root game objects for clothes - public Transform ClothesRoot; - - + /// + /// Synced list of body part associated to the clothes on them. + /// [SyncObject] private readonly SyncList _clothedBodyParts = new SyncList(); - [SerializeField] - private List startingClothedBodyPart; - public override void OnStartServer() { base.OnStartServer(); @@ -66,7 +55,9 @@ public override void OnStartClient() _clothedBodyParts.OnChange += ClothedBodyPartsOnChange; } - + /// + /// Callback when the syncedList _clothedBodyParts changes. Update the displayed clothes on the player. + /// private void ClothedBodyPartsOnChange(SyncListOperation op, int index, ClothDisplayData oldData, ClothDisplayData newData, bool asServer) { @@ -75,6 +66,7 @@ private void ClothedBodyPartsOnChange(SyncListOperation op, int index, switch (op) { + // Show the new cloth on the player case SyncListOperation.Add: NetworkObject newBodyPart = newData._bodyPart; Item newItem = newData._clothToDisplay; @@ -87,6 +79,7 @@ private void ClothedBodyPartsOnChange(SyncListOperation op, int index, renderer.sharedMesh = newItem.gameObject.GetComponentInChildren().sharedMesh; break; + // Stop displaying cloth on the player case SyncListOperation.RemoveAt: NetworkObject oldBodyPart = oldData._bodyPart; oldBodyPart.gameObject.SetActive(false); @@ -123,6 +116,10 @@ public void HandleContainerContentChanged(Container container, IEnumerable } } + /// + /// Adds a cloth to the synced list, making a few checks to find where to add it, if possible. + /// + /// The item to add, it should have a Cloth component on it. [Server] private void AddCloth(Item item) { @@ -143,6 +140,10 @@ private void AddCloth(Item item) } } + /// + /// Remove a cloth from the synced list, check if it's there before removing. + /// + /// The item to add, it should have a Cloth component on it. [Server] private void RemoveCloth(Item item) { diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerView.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerView.cs index 8d1dc8ec85..2ce62354e3 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerView.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerView.cs @@ -12,11 +12,19 @@ namespace SS3D.Systems.Inventory.UI { + /// + /// Add and remove UIs for containers. + /// public class ContainerView : View { - + /// + /// The script handling logic regarding when to remove and add container UIs. + /// private ContainerViewer containerViewer; + /// + /// List of displayed containers on the player screen. + /// private readonly List _containerDisplays = new(); /// @@ -24,8 +32,6 @@ public class ContainerView : View /// public GameObject ContainerUiPrefab; - // Maybe HandsUI should only handle selected hand highlight and inventory UI - // should handle setting up containers to UI. public void Setup(ContainerViewer viewer) { containerViewer = viewer; diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerViewer.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerViewer.cs index bb6d9ef8d9..9338696f95 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerViewer.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerViewer.cs @@ -15,7 +15,7 @@ namespace SS3D.Systems.Inventory.Containers { /// - /// This handles displaying and removing containers UI that are not part of the inventory of the player, such as a toolbox. + /// This handles displaying and removing containers UI that are not part of the inventory of the player, such as a toolbox's container. /// public class ContainerViewer : NetworkActor { @@ -32,7 +32,7 @@ public class ContainerViewer : NetworkActor /// private readonly List _displayedContainers = new(); - // Reference to the player human inventory. + // Reference to the player's inventory. public HumanInventory inventory; public override void OnStartClient() @@ -81,7 +81,7 @@ private void SetOpenState(GameObject containerObject, bool state) } /// - /// Does this have a specific container ? + /// Does the player have the UI opened for a specific container ? /// public bool HasContainer(AttachedContainer container) { @@ -97,19 +97,13 @@ public bool CanModifyContainer(AttachedContainer container) [TargetRpc] private void TargetOpenContainer(NetworkConnection target, AttachedContainer container) { - InvokeContainerOpened(container); - } - - private void InvokeContainerOpened(AttachedContainer container) - { - OnContainerOpened?.Invoke(container); - } - + OnContainerOpened?.Invoke(container); + } /// /// Make this inventory open an container. /// - public void OpenContainer(AttachedContainer attachedContainer) + public void ShowContainerUI(AttachedContainer attachedContainer) { attachedContainer.Container.AddObserver(GetComponent()); _displayedContainers.Add(attachedContainer); @@ -122,9 +116,9 @@ public void OpenContainer(AttachedContainer attachedContainer) } /// - /// Removes a container from this inventory. + /// Removes a container from the list of visible containers UI. /// - public void CloseContainer(AttachedContainer container) + public void CloseContainerUI(AttachedContainer container) { container.Container.RemoveObserver(GetComponent()); if (_displayedContainers.Remove(container)) @@ -157,7 +151,7 @@ private void HandleUpdate(ref EventContext context, in UpdateEvent updateEvent) continue; } - CloseContainer(attachedContainer); + CloseContainerUI(attachedContainer); i--; } @@ -167,20 +161,14 @@ private void HandleUpdate(ref EventContext context, in UpdateEvent updateEvent) [ServerRpc] public void CmdContainerClose(AttachedContainer container) { - CloseContainer(container); + CloseContainerUI(container); } - [TargetRpc] private void TargetCloseContainer(NetworkConnection target, AttachedContainer container) { - InvokeContainerClosed(container); - } - - private void InvokeContainerClosed(AttachedContainer container) - { - OnContainerClosed?.Invoke(container); - } + OnContainerClosed?.Invoke(container); + } } } diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/Hands.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/Hands.cs index ed382adb30..e1112b21e2 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/Hands.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/Hands.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using FishNet.Object; using SS3D.Core; @@ -15,6 +15,11 @@ namespace SS3D.Systems.Inventory.Containers { + + /// + /// Handle selections of the hands, holding stuff, using tools, and interacting.. + /// Should probably have some of this code in independent hand components, to allow hands to not be usable after loosing one. + /// [RequireComponent(typeof(HumanInventory))] public class Hands : InteractionSource, IToolHolder, IInteractionRangeLimit, IInteractionOriginProvider { diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Containers/HumanInventory.cs b/Assets/Scripts/SS3D/Systems/Inventory/Containers/HumanInventory.cs index d708046003..987cd6af17 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Containers/HumanInventory.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Containers/HumanInventory.cs @@ -25,21 +25,30 @@ namespace SS3D.Systems.Inventory.Containers /// public class HumanInventory : NetworkActor { + /// + /// List of containers present on the player, meaning, in the player HUD, shown as slots. + /// [SyncObject] private readonly SyncList ContainersOnPlayer = new(); + public delegate void InventoryContainerModifiedEventHandler(AttachedContainer container); public delegate void ContainerContentsEventHandler(Container container, IEnumerable oldItems, IEnumerable newItems, ContainerChangeType type); public delegate void Notify(); + // When a container is added to this inventory public event InventoryContainerModifiedEventHandler OnInventoryContainerAdded; + // When a container is removed from this inventory public event InventoryContainerModifiedEventHandler OnInventoryContainerRemoved; + // When the content of a container in this inventory changes public event ContainerContentsEventHandler OnContainerContentChanged; + // When the inventory is done doing its setup public event Notify OnInventorySetUp; + // reference to the component allowing to display out of inventory containers. public ContainerViewer containerViewer; public List Containers => ContainersOnPlayer.Collection.ToList(); @@ -183,6 +192,10 @@ private void RemoveContainer(AttachedContainer container) container.OnAttachedContainerDisabled -= RemoveContainer; } + /// + /// Try to add a container to this inventory, check first if not already added. + /// TODO: Should also check if it's the kind of container that can go in inventory. + /// [Server] public bool TryAddContainer(AttachedContainer container) { @@ -194,6 +207,9 @@ public bool TryAddContainer(AttachedContainer container) return false; } + /// + /// Try to remove a container already present in this inventory. + /// [Server] public bool TryRemoveContainer(AttachedContainer container) { diff --git a/Assets/Scripts/SS3D/Systems/Inventory/Interactions/ViewContainerInteraction.cs b/Assets/Scripts/SS3D/Systems/Inventory/Interactions/ViewContainerInteraction.cs index 445806487c..74be450c30 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/Interactions/ViewContainerInteraction.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/Interactions/ViewContainerInteraction.cs @@ -59,7 +59,7 @@ public override bool Start(InteractionEvent interactionEvent, InteractionReferen { var containerViewer = interactionEvent.Source.GetComponentInTree(); - containerViewer.OpenContainer(AttachedContainer); + containerViewer.ShowContainerUI(AttachedContainer); return false; } diff --git a/Assets/Scripts/SS3D/Systems/Inventory/UI/DummySlot.cs b/Assets/Scripts/SS3D/Systems/Inventory/UI/DummySlot.cs index a908cc9202..59536fda4c 100644 --- a/Assets/Scripts/SS3D/Systems/Inventory/UI/DummySlot.cs +++ b/Assets/Scripts/SS3D/Systems/Inventory/UI/DummySlot.cs @@ -4,6 +4,11 @@ namespace SS3D.Systems.Inventory.UI { + /// + /// Component only useful to indicate a slot is a dummy slot. + /// Dummy slots are invisible slots, only useful to take place in the UI. + /// This is necessary for working with stuff like grid layouts. + /// public class DummySlot : MonoBehaviour { diff --git a/Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElementAlt.cs b/Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElement.cs similarity index 100% rename from Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElementAlt.cs rename to Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElement.cs diff --git a/Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElementAlt.cs.meta b/Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElement.cs.meta similarity index 100% rename from Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElementAlt.cs.meta rename to Assets/Scripts/SS3D/Systems/Inventory/UI/InventoryDisplayElement.cs.meta