2024-10-30 19:48:58 +03:00
|
|
|
|
using SWB.Base;
|
|
|
|
|
using SWB.Shared;
|
2024-10-30 19:01:58 +03:00
|
|
|
|
|
2024-10-30 19:48:58 +03:00
|
|
|
|
namespace Sandbox;
|
2024-10-30 19:01:58 +03:00
|
|
|
|
public class Inventory : Component, IInventory
|
|
|
|
|
{
|
|
|
|
|
[Sync] public NetList<GameObject> Items { get; set; } = new();
|
|
|
|
|
[Sync] public new GameObject Active { get; set; }
|
|
|
|
|
|
2024-10-30 19:48:58 +03:00
|
|
|
|
Kal _player;
|
2024-10-30 19:01:58 +03:00
|
|
|
|
|
|
|
|
|
protected override void OnAwake()
|
|
|
|
|
{
|
2024-10-30 19:48:58 +03:00
|
|
|
|
_player = Components.Get<Kal>();
|
2024-10-30 19:01:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add( GameObject gameObject, bool makeActive = false )
|
|
|
|
|
{
|
|
|
|
|
if ( !Has( gameObject ) )
|
|
|
|
|
{
|
|
|
|
|
Items.Add( gameObject );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( makeActive )
|
|
|
|
|
SetActive( gameObject );
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ( gameObject.Components.TryGet<IInventoryItem>( out var item ) )
|
|
|
|
|
{
|
|
|
|
|
item.OnCarryStop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 19:48:58 +03:00
|
|
|
|
public void Disarm()
|
|
|
|
|
{
|
|
|
|
|
foreach ( var item in Items )
|
|
|
|
|
{
|
|
|
|
|
if ( item.Enabled && item.IsValid )
|
|
|
|
|
{
|
|
|
|
|
IInventoryItem iItem = item.GetComponent<Weapon>();
|
|
|
|
|
iItem.OnCarryStop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Active = null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 19:01:58 +03:00
|
|
|
|
public GameObject AddClone( GameObject gamePrefab, bool makeActive = true )
|
|
|
|
|
{
|
2024-10-30 19:48:58 +03:00
|
|
|
|
var gameObject = gamePrefab.Clone( _player.GameObject, _player.WorldPosition, _player.WorldRotation, Vector3.One );
|
2024-10-30 19:01:58 +03:00
|
|
|
|
gameObject.Name = gamePrefab.Name;
|
2024-10-30 19:48:58 +03:00
|
|
|
|
gameObject.NetworkSpawn( _player.Network.Owner );
|
2024-10-30 19:01:58 +03:00
|
|
|
|
|
|
|
|
|
Add( gameObject, makeActive );
|
|
|
|
|
return gameObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Has( GameObject gameObject )
|
|
|
|
|
{
|
|
|
|
|
return Items.Contains( gameObject );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetActive( GameObject gameObject )
|
|
|
|
|
{
|
|
|
|
|
if ( !Has( gameObject ) || Active == gameObject ) return;
|
|
|
|
|
|
|
|
|
|
if ( Active is not null && Active.Components.TryGet<IInventoryItem>( out var oldActive ) )
|
|
|
|
|
{
|
|
|
|
|
if ( !oldActive.CanCarryStop() ) return;
|
|
|
|
|
oldActive.OnCarryStop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( gameObject.Components.TryGet<IInventoryItem>( out var newActive, FindMode.EverythingInSelf ) )
|
|
|
|
|
{
|
|
|
|
|
newActive.OnCarryStart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Active = gameObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetActive( string name )
|
|
|
|
|
{
|
|
|
|
|
foreach ( var item in Items )
|
|
|
|
|
{
|
|
|
|
|
if ( item.Name == name )
|
|
|
|
|
{
|
|
|
|
|
SetActive( item );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
foreach ( var item in Items )
|
|
|
|
|
{
|
|
|
|
|
item.Destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Items.Clear();
|
|
|
|
|
Active = null;
|
|
|
|
|
}
|
|
|
|
|
}
|