Skip to content

Commit

Permalink
Fix item collider (RE-SS3D#1417)
Browse files Browse the repository at this point in the history
* Fix item colliders

* Fix everything

* Fish fears me

* Sausages

* Move stuff to Freeze/Unfreeze

* add comment

* Fix wrong component

* Add a comment to _container and Container

---------

Co-authored-by: Mechar418 <[email protected]>
Co-authored-by: stilnat <[email protected]>
  • Loading branch information
3 people authored Feb 25, 2024
1 parent 6d75c2f commit 6770194
Showing 1 changed file with 62 additions and 16 deletions.
78 changes: 62 additions & 16 deletions Assets/Scripts/SS3D/Systems/Inventory/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using SS3D.Systems.Inventory.Containers;
using SS3D.Systems.Inventory.Interactions;
using SS3D.Systems.Selection;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;
#if UNITY_EDITOR
Expand Down Expand Up @@ -68,16 +69,42 @@ public class Item : InteractionSource, IInteractionTarget, IWorldObjectAsset
[SyncObject]
private readonly SyncList<Trait> _traits = new();

/// <summary>
/// Where the item is stored
/// </summary>
[SyncVar]
private AttachedContainer _container;

public string Name => _name;

public ReadOnlyCollection<Trait> Traits => ((List<Trait>) _traits.Collection).AsReadOnly();

/// <summary>
/// Where the item is stored
/// </summary>
public AttachedContainer Container => _container;

private bool _initialised = false;

/// <summary>
/// All colliders, related to the item, except of colliders, related to stored items
/// </summary>
private Collider[] _nativeColliders;
/// <summary>
/// All colliders, related to the item, except of colliders, related to stored items
/// </summary>
public Collider[] NativeColliders
{
get
{
if (_nativeColliders == null)
{
_nativeColliders = GetNativeColliders();
}
return _nativeColliders;
}
set => _nativeColliders = value;
}

public WorldObjectAssetReference Asset
{
Expand Down Expand Up @@ -118,7 +145,7 @@ public Sprite ItemSprite
get => InventorySprite();
set => _sprite = value;
}

protected override void OnStart()
{
base.OnStart();
Expand All @@ -133,6 +160,23 @@ protected override void OnStart()
{
_rigidbody.isKinematic = true;
}

_nativeColliders ??= GetNativeColliders();
Debug.Log("Start " + name);
}

/// <summary>
/// Get all colliders, related to the item, except of colliders, related to stored items
/// </summary>
private Collider[] GetNativeColliders()
{
List<Collider> collidersToExcept = new();
AttachedContainer[] containers = GetComponentsInChildren<AttachedContainer>();
foreach (Item item in containers.SelectMany(container => container.Items))
{
collidersToExcept.AddRange(item.GetComponentsInChildren<Collider>());
}
return GetComponentsInChildren<Collider>().Except(collidersToExcept).ToArray();
}

public override void OnStartServer()
Expand Down Expand Up @@ -176,11 +220,7 @@ public void Freeze()
{
_rigidbody.isKinematic = true;
}
var itemCollider = GetComponent<Collider>();
if (itemCollider != null)
{
itemCollider.enabled = false;
}
ToggleCollider(false);
}

/// <summary>
Expand All @@ -194,13 +234,21 @@ public void Unfreeze()
if (IsServer)
_rigidbody.isKinematic = false;
}
var itemCollider = GetComponent<Collider>();
if (itemCollider != null)
{
itemCollider.enabled = true;
}
ToggleCollider(true);
}

/// <summary>
/// Enable or disable all colliders related to the item. Does not touch any colliders that would belong to stored items (if there are any).
/// TODO : might want to replace GetComponentsInChildren with a manual setup of the container list.
/// </summary>
[ServerOrClient]
protected virtual void ToggleCollider(bool isEnable)
{
foreach (Collider collider in NativeColliders)
{
collider.enabled = isEnable;
}
}

/// <param name="visible">Should the item be visible</param>
[ServerOrClient]
Expand Down Expand Up @@ -252,7 +300,7 @@ public bool IsInContainer()
public string Describe()
{
string traits = "";
foreach (var trait in _traits)
foreach (Trait trait in _traits)
{
traits += trait.Name + " ";
}
Expand All @@ -274,13 +322,11 @@ public bool HasTrait(Trait trait)
[Server]
public void SetContainer(AttachedContainer newContainer)
{
if (_container == newContainer)
{
return;
}
_container = newContainer;
}



// Generate preview of the same object, but without stored items.
[ServerOrClient]
public Sprite GenerateIcon()
Expand Down

0 comments on commit 6770194

Please sign in to comment.