Skip to content

Commit 6770194

Browse files
Fix item collider (#1417)
* 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]>
1 parent 6d75c2f commit 6770194

File tree

1 file changed

+62
-16
lines changed
  • Assets/Scripts/SS3D/Systems/Inventory/Items

1 file changed

+62
-16
lines changed

Assets/Scripts/SS3D/Systems/Inventory/Items/Item.cs

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using SS3D.Systems.Inventory.Containers;
1414
using SS3D.Systems.Inventory.Interactions;
1515
using SS3D.Systems.Selection;
16+
using System.Linq;
1617
using UnityEngine;
1718
using UnityEngine.Serialization;
1819
#if UNITY_EDITOR
@@ -68,16 +69,42 @@ public class Item : InteractionSource, IInteractionTarget, IWorldObjectAsset
6869
[SyncObject]
6970
private readonly SyncList<Trait> _traits = new();
7071

72+
/// <summary>
73+
/// Where the item is stored
74+
/// </summary>
7175
[SyncVar]
7276
private AttachedContainer _container;
7377

7478
public string Name => _name;
7579

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

82+
/// <summary>
83+
/// Where the item is stored
84+
/// </summary>
7885
public AttachedContainer Container => _container;
7986

8087
private bool _initialised = false;
88+
89+
/// <summary>
90+
/// All colliders, related to the item, except of colliders, related to stored items
91+
/// </summary>
92+
private Collider[] _nativeColliders;
93+
/// <summary>
94+
/// All colliders, related to the item, except of colliders, related to stored items
95+
/// </summary>
96+
public Collider[] NativeColliders
97+
{
98+
get
99+
{
100+
if (_nativeColliders == null)
101+
{
102+
_nativeColliders = GetNativeColliders();
103+
}
104+
return _nativeColliders;
105+
}
106+
set => _nativeColliders = value;
107+
}
81108

82109
public WorldObjectAssetReference Asset
83110
{
@@ -118,7 +145,7 @@ public Sprite ItemSprite
118145
get => InventorySprite();
119146
set => _sprite = value;
120147
}
121-
148+
122149
protected override void OnStart()
123150
{
124151
base.OnStart();
@@ -133,6 +160,23 @@ protected override void OnStart()
133160
{
134161
_rigidbody.isKinematic = true;
135162
}
163+
164+
_nativeColliders ??= GetNativeColliders();
165+
Debug.Log("Start " + name);
166+
}
167+
168+
/// <summary>
169+
/// Get all colliders, related to the item, except of colliders, related to stored items
170+
/// </summary>
171+
private Collider[] GetNativeColliders()
172+
{
173+
List<Collider> collidersToExcept = new();
174+
AttachedContainer[] containers = GetComponentsInChildren<AttachedContainer>();
175+
foreach (Item item in containers.SelectMany(container => container.Items))
176+
{
177+
collidersToExcept.AddRange(item.GetComponentsInChildren<Collider>());
178+
}
179+
return GetComponentsInChildren<Collider>().Except(collidersToExcept).ToArray();
136180
}
137181

138182
public override void OnStartServer()
@@ -176,11 +220,7 @@ public void Freeze()
176220
{
177221
_rigidbody.isKinematic = true;
178222
}
179-
var itemCollider = GetComponent<Collider>();
180-
if (itemCollider != null)
181-
{
182-
itemCollider.enabled = false;
183-
}
223+
ToggleCollider(false);
184224
}
185225

186226
/// <summary>
@@ -194,13 +234,21 @@ public void Unfreeze()
194234
if (IsServer)
195235
_rigidbody.isKinematic = false;
196236
}
197-
var itemCollider = GetComponent<Collider>();
198-
if (itemCollider != null)
199-
{
200-
itemCollider.enabled = true;
201-
}
237+
ToggleCollider(true);
202238
}
203239

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

205253
/// <param name="visible">Should the item be visible</param>
206254
[ServerOrClient]
@@ -252,7 +300,7 @@ public bool IsInContainer()
252300
public string Describe()
253301
{
254302
string traits = "";
255-
foreach (var trait in _traits)
303+
foreach (Trait trait in _traits)
256304
{
257305
traits += trait.Name + " ";
258306
}
@@ -274,13 +322,11 @@ public bool HasTrait(Trait trait)
274322
[Server]
275323
public void SetContainer(AttachedContainer newContainer)
276324
{
277-
if (_container == newContainer)
278-
{
279-
return;
280-
}
281325
_container = newContainer;
282326
}
283327

328+
329+
284330
// Generate preview of the same object, but without stored items.
285331
[ServerOrClient]
286332
public Sprite GenerateIcon()

0 commit comments

Comments
 (0)