85 lines
2.1 KiB
C#
85 lines
2.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BITFALL.Entities.Inventory;
|
|
using BITFALL.Items;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.Selection;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.Player.Inventory
|
|
{
|
|
[CustomType(typeof(IEntitySwapItem))]
|
|
public class PlayerInventorySwap : EntityBehavior,IEntitySwapItem
|
|
{
|
|
public bool TryGetCurrentContainer(out IBasicItemContainer container)
|
|
{
|
|
container = _currentContainer;
|
|
return container is not null;
|
|
}
|
|
|
|
public event Func<IBasicItemContainer, bool> OpenSwapFactory;
|
|
public event Action<IBasicItemContainer> OnSwapOpened;
|
|
public event Action<IBasicItemContainer> OnSwapClosed;
|
|
|
|
[Inject] private ISelector _selector;
|
|
[Inject] private IHealth _health;
|
|
[Inject] private IEntityInventory _inventory;
|
|
|
|
private IBasicItemContainer _currentContainer;
|
|
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
_selector.OnActive += OnActive;
|
|
_health.OnSetAlive += OnSetAlive;
|
|
}
|
|
|
|
private void OnSetAlive(bool obj)
|
|
{
|
|
if (obj is false)
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
|
|
private void OnActive(ISelectable obj)
|
|
{
|
|
if (_currentContainer is not null) return;
|
|
if (obj.Transform.TryGetComponent<IBasicItemContainer>(out var container) is false) return;
|
|
Open(container);
|
|
}
|
|
|
|
public bool Add(IBasicItem item)
|
|
{
|
|
if (_currentContainer is null) return false;
|
|
return _inventory.Add(item) && _currentContainer.Remove(item);
|
|
}
|
|
public bool Remove(IBasicItem item)
|
|
{
|
|
if (_currentContainer is null) return false;
|
|
return _currentContainer.Add(item) && _inventory.Remove(item);
|
|
}
|
|
|
|
public bool Open(IBasicItemContainer container)
|
|
{
|
|
if (_currentContainer is not null) return false;
|
|
if (OpenSwapFactory.CastAsFunc().Any(x=>x.Invoke(container) is false)) return false;
|
|
_currentContainer = container;
|
|
OnSwapOpened?.Invoke(_currentContainer);
|
|
return true;
|
|
}
|
|
|
|
public bool Close()
|
|
{
|
|
if (_currentContainer is null) return false;
|
|
OnSwapClosed?.Invoke(_currentContainer);
|
|
_currentContainer = null;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|