167 lines
4.9 KiB
C#
167 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using BITKit;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine.UIElements;
|
|
using System.Text;
|
|
using BITKit.Entities;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using BITFALL.Entities;
|
|
using BITFALL.Entities.Improvised;
|
|
using BITFALL.Items;
|
|
using BITFALL.Player.Inventory;
|
|
using BITKit.Selection;
|
|
using BITKit.UX;
|
|
using Unity.Mathematics;
|
|
|
|
namespace BITFALL
|
|
{
|
|
[CustomType(typeof(IPlayerInventory))]
|
|
public class PlayerInventory : EntityInventory,IPlayerInventory
|
|
{
|
|
[SerializeField] private int currentMoney;
|
|
|
|
[Header(nameof(PlayerInventory))]
|
|
[SerializeReference,SubclassSelector] private INetClient netClient;
|
|
[SerializeReference,SubclassSelector] private INetServer netServer;
|
|
|
|
private INetProvider _clientNetProvider=>netClient as INetProvider;
|
|
private INetProvider _serverNetProvider=>netServer as INetProvider;
|
|
[Inject]
|
|
private ISelector _selector;
|
|
[Inject(true)]
|
|
private ImprovisedServiceInterface _improvisedService;
|
|
[Inject(true)]
|
|
private IKnockdown _knockdown;
|
|
[Inject(true)]
|
|
private IUXPopup _popup;
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
_selector.OnActive += OnActive;
|
|
if (_improvisedService is not null)
|
|
{
|
|
_improvisedService.OnUnEquipImprovisedItem += OnUnEquipImprovisedItem;
|
|
}
|
|
OnAdd += OnAddItem;
|
|
OnRemove += OnRemoveItem;
|
|
OnUsedItem += OnUseItem;
|
|
}
|
|
|
|
private void OnUseItem(IBasicItem obj)
|
|
{
|
|
_popup?.Popup($"使用了[{obj.Name}]");
|
|
}
|
|
private void OnRemoveItem(IBasicItem obj)
|
|
{
|
|
_popup?.Popup($"移除了[{obj.Name}]");
|
|
}
|
|
private void OnAddItem(IBasicItem obj)
|
|
{
|
|
_popup?.Popup($"捡起了[{obj.Name}]");
|
|
}
|
|
|
|
protected override void OnSetAlive(bool alive)
|
|
{
|
|
var keepInventory = Data.Get<bool>(BITConstant.Environment.sp_keepInventory);
|
|
if (keepInventory)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
base.OnSetAlive(alive);
|
|
}
|
|
}
|
|
|
|
private void OnUnEquipImprovisedItem(IBasicItem obj)
|
|
{
|
|
Drop(obj);
|
|
}
|
|
|
|
public override bool Add(IBasicItem item)
|
|
{
|
|
var result = base.Add(item);
|
|
if (result && netClient.IsConnected)
|
|
{
|
|
_clientNetProvider.ServerCommand(new WorldItemDespawnCommand()
|
|
{
|
|
Id = item.Id,
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
protected override void Drop(IBasicItem item)
|
|
{
|
|
if(netClient.IsConnected)
|
|
{
|
|
InvokeOnDrop(item);
|
|
_clientNetProvider.ServerCommand(new WorldItemSpawnCommand()
|
|
{
|
|
Id = item.Id,
|
|
AddressableId = item.GetAssetable().AddressableId,
|
|
Position = Transform.position,
|
|
Rotation = Transform.rotation,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
base.Drop(item);
|
|
}
|
|
}
|
|
|
|
private void OnActive(ISelectable obj)
|
|
{
|
|
if (!obj.Transform.TryGetComponent<WorldItem>(out var item)) return;
|
|
var _item = item.Pick();
|
|
if(item.GetAssetable().IsImprovised)
|
|
{
|
|
if (_knockdown is not null && _knockdown.IsKnockdown)
|
|
{
|
|
return;
|
|
}
|
|
if (_improvisedService.TryEquipImprovisedItem(_item))
|
|
{
|
|
item.Picked();
|
|
}
|
|
}
|
|
else if (Add(_item))
|
|
{
|
|
item.Picked();
|
|
}
|
|
}
|
|
|
|
public int Money => currentMoney;
|
|
public void Transfer(IMoneyStrace strace)
|
|
{
|
|
currentMoney += strace.Money;
|
|
if (string.IsNullOrEmpty(strace.Detail) is false)
|
|
{
|
|
_popup?.Popup($"{strace.Detail}:{strace.Money}");
|
|
}
|
|
}
|
|
}
|
|
#if UNITY_EDITOR
|
|
[UnityEditor.CustomEditor(typeof(PlayerInventory))]
|
|
public class EntityPlayerInventoryInspector : BITInspector<PlayerInventory>
|
|
{
|
|
public override VisualElement CreateInspectorGUI()
|
|
{
|
|
FillDefaultInspector();
|
|
CreateSubTitle(Constant.Header.Debug);
|
|
var serializeLabel = root.Create<Label>();
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
foreach (var x in agent.GetItems())
|
|
{
|
|
stringBuilder.AppendLine($"{x.Id}@{x.Name}");
|
|
}
|
|
serializeLabel.text=stringBuilder.ToString();
|
|
return root;
|
|
}
|
|
}
|
|
#endif
|
|
} |