Skip to content

Commit

Permalink
clean and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Jul 27, 2023
1 parent 53501a8 commit 8b579e0
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 53 deletions.
4 changes: 4 additions & 0 deletions Assets/Scripts/SS3D/Systems/Inventory/Containers/Cloth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

namespace SS3D.Systems.Inventory.Containers
{
/// <summary>
/// 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...)
/// </summary>
public class Cloth : MonoBehaviour
{
[SerializeField]
Expand Down
9 changes: 4 additions & 5 deletions Assets/Scripts/SS3D/Systems/Inventory/Containers/ClothType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SS3D.Systems.Inventory.Containers
namespace SS3D.Systems.Inventory.Containers
{
/// <summary>
/// List of different types of clothes, mostly useful to decide what can go where.
/// </summary>
public enum ClothType
{
ShoeLeft,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace SS3D.Systems.Inventory.Containers
{
/// <summary>
/// Used on bodypart of entities to indicate which type of clothes can be worn on them.
/// </summary>
public class ClothedBodyPart : MonoBehaviour
{
[SerializeField]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Display cloth on player for all clients.
/// Display clothes on player for all clients.
/// </summary>
public class ClothesDisplayer : NetworkActor
{
Expand All @@ -42,18 +32,17 @@ public ClothDisplayData(NetworkObject bodyPart, Item clothToDisplay)
public Item _clothToDisplay;
}

/// <summary>
/// The inventory containing the player's clothing slots.
/// </summary>
public HumanInventory _inventory;

// The root game objects for clothes
public Transform ClothesRoot;


/// <summary>
/// Synced list of body part associated to the clothes on them.
/// </summary>
[SyncObject]
private readonly SyncList<ClothDisplayData> _clothedBodyParts = new SyncList<ClothDisplayData>();

[SerializeField]
private List<NetworkObject> startingClothedBodyPart;

public override void OnStartServer()
{
base.OnStartServer();
Expand All @@ -66,7 +55,9 @@ public override void OnStartClient()
_clothedBodyParts.OnChange += ClothedBodyPartsOnChange;
}


/// <summary>
/// Callback when the syncedList _clothedBodyParts changes. Update the displayed clothes on the player.
/// </summary>
private void ClothedBodyPartsOnChange(SyncListOperation op, int index,
ClothDisplayData oldData, ClothDisplayData newData, bool asServer)
{
Expand All @@ -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;
Expand All @@ -87,6 +79,7 @@ private void ClothedBodyPartsOnChange(SyncListOperation op, int index,
renderer.sharedMesh = newItem.gameObject.GetComponentInChildren<MeshFilter>().sharedMesh;
break;

// Stop displaying cloth on the player
case SyncListOperation.RemoveAt:
NetworkObject oldBodyPart = oldData._bodyPart;
oldBodyPart.gameObject.SetActive(false);
Expand Down Expand Up @@ -123,6 +116,10 @@ public void HandleContainerContentChanged(Container container, IEnumerable<Item>
}
}

/// <summary>
/// Adds a cloth to the synced list, making a few checks to find where to add it, if possible.
/// </summary>
/// <param name="item"> The item to add, it should have a Cloth component on it.</param>
[Server]
private void AddCloth(Item item)
{
Expand All @@ -143,6 +140,10 @@ private void AddCloth(Item item)
}
}

/// <summary>
/// Remove a cloth from the synced list, check if it's there before removing.
/// </summary>
/// <param name="item">The item to add, it should have a Cloth component on it.</param>
[Server]
private void RemoveCloth(Item item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@

namespace SS3D.Systems.Inventory.UI
{
/// <summary>
/// Add and remove UIs for containers.
/// </summary>
public class ContainerView : View
{

/// <summary>
/// The script handling logic regarding when to remove and add container UIs.
/// </summary>
private ContainerViewer containerViewer;

/// <summary>
/// List of displayed containers on the player screen.
/// </summary>
private readonly List<ContainerDisplay> _containerDisplays = new();

/// <summary>
/// The prefab for a container display
/// </summary>
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;
Expand Down
36 changes: 12 additions & 24 deletions Assets/Scripts/SS3D/Systems/Inventory/Containers/ContainerViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace SS3D.Systems.Inventory.Containers
{
/// <summary>
/// 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.
/// </summary>
public class ContainerViewer : NetworkActor
{
Expand All @@ -32,7 +32,7 @@ public class ContainerViewer : NetworkActor
/// </summary>
private readonly List<AttachedContainer> _displayedContainers = new();

// Reference to the player human inventory.
// Reference to the player's inventory.
public HumanInventory inventory;

public override void OnStartClient()
Expand Down Expand Up @@ -81,7 +81,7 @@ private void SetOpenState(GameObject containerObject, bool state)
}

/// <summary>
/// Does this have a specific container ?
/// Does the player have the UI opened for a specific container ?
/// </summary>
public bool HasContainer(AttachedContainer container)
{
Expand All @@ -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);
}

/// <summary>
/// Make this inventory open an container.
/// </summary>
public void OpenContainer(AttachedContainer attachedContainer)
public void ShowContainerUI(AttachedContainer attachedContainer)
{
attachedContainer.Container.AddObserver(GetComponent<Entity>());
_displayedContainers.Add(attachedContainer);
Expand All @@ -122,9 +116,9 @@ public void OpenContainer(AttachedContainer attachedContainer)
}

/// <summary>
/// Removes a container from this inventory.
/// Removes a container from the list of visible containers UI.
/// </summary>
public void CloseContainer(AttachedContainer container)
public void CloseContainerUI(AttachedContainer container)
{
container.Container.RemoveObserver(GetComponent<Entity>());
if (_displayedContainers.Remove(container))
Expand Down Expand Up @@ -157,7 +151,7 @@ private void HandleUpdate(ref EventContext context, in UpdateEvent updateEvent)
continue;
}

CloseContainer(attachedContainer);
CloseContainerUI(attachedContainer);
i--;
}

Expand All @@ -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);
}

}
}
7 changes: 6 additions & 1 deletion Assets/Scripts/SS3D/Systems/Inventory/Containers/Hands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using FishNet.Object;
using SS3D.Core;
Expand All @@ -15,6 +15,11 @@

namespace SS3D.Systems.Inventory.Containers
{

/// <summary>
/// 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.
/// </summary>
[RequireComponent(typeof(HumanInventory))]
public class Hands : InteractionSource, IToolHolder, IInteractionRangeLimit, IInteractionOriginProvider
{
Expand Down
16 changes: 16 additions & 0 deletions Assets/Scripts/SS3D/Systems/Inventory/Containers/HumanInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,30 @@ namespace SS3D.Systems.Inventory.Containers
/// </summary>
public class HumanInventory : NetworkActor
{
/// <summary>
/// List of containers present on the player, meaning, in the player HUD, shown as slots.
/// </summary>
[SyncObject]
private readonly SyncList<AttachedContainer> ContainersOnPlayer = new();


public delegate void InventoryContainerModifiedEventHandler(AttachedContainer container);
public delegate void ContainerContentsEventHandler(Container container, IEnumerable<Item> oldItems, IEnumerable<Item> 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<AttachedContainer> Containers => ContainersOnPlayer.Collection.ToList();
Expand Down Expand Up @@ -183,6 +192,10 @@ private void RemoveContainer(AttachedContainer container)
container.OnAttachedContainerDisabled -= RemoveContainer;
}

/// <summary>
/// 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.
/// </summary>
[Server]
public bool TryAddContainer(AttachedContainer container)
{
Expand All @@ -194,6 +207,9 @@ public bool TryAddContainer(AttachedContainer container)
return false;
}

/// <summary>
/// Try to remove a container already present in this inventory.
/// </summary>
[Server]
public bool TryRemoveContainer(AttachedContainer container)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override bool Start(InteractionEvent interactionEvent, InteractionReferen
{
var containerViewer = interactionEvent.Source.GetComponentInTree<ContainerViewer>();

containerViewer.OpenContainer(AttachedContainer);
containerViewer.ShowContainerUI(AttachedContainer);

return false;
}
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/SS3D/Systems/Inventory/UI/DummySlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace SS3D.Systems.Inventory.UI
{
/// <summary>
/// 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.
/// </summary>
public class DummySlot : MonoBehaviour
{

Expand Down

0 comments on commit 8b579e0

Please sign in to comment.