221 lines
6.8 KiB
C#
221 lines
6.8 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.IO;
|
|
using BITKit.Selection;
|
|
using BITKit.UX;
|
|
using Unity.Mathematics;
|
|
using YooAsset;
|
|
|
|
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;
|
|
[Inject(true)]
|
|
private IApplicationFile applicationFile;
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
_selector.OnActive += OnActive;
|
|
if (_improvisedService is not null)
|
|
{
|
|
_improvisedService.OnUnEquipImprovisedItem += OnUnEquipImprovisedItem;
|
|
}
|
|
OnAdd += OnAddItem;
|
|
OnRemove += OnRemoveItem;
|
|
OnUsedItem += OnUseItem;
|
|
|
|
applicationFile.AddListener(nameof(PlayerInventory),OnRelease);
|
|
applicationFile.DataHandle += OnData;
|
|
}
|
|
|
|
public override void OnDestroyComponent()
|
|
{
|
|
base.OnDestroyComponent();
|
|
applicationFile.RemoveListener(nameof(PlayerInventory),OnRelease);
|
|
applicationFile.DataHandle -= OnData;
|
|
}
|
|
private (string, byte[]) OnData()
|
|
{
|
|
using var ms = new System.IO.MemoryStream();
|
|
using var writer = new System.IO.BinaryWriter(ms);
|
|
|
|
var reportBuilder = new StringBuilder();
|
|
|
|
var items = GetItems();
|
|
writer.Write(items.Length);
|
|
reportBuilder.AppendLine(items.Length.ToString());
|
|
foreach (var item in items)
|
|
{
|
|
writer.Write(item.Id);
|
|
writer.Write(item.AddressablePath);
|
|
reportBuilder.AppendLine(item.AddressablePath);
|
|
}
|
|
BIT4Log.Log<PlayerInventory>(reportBuilder.ToString());
|
|
return new(nameof(PlayerInventory),ms.ToArray());
|
|
}
|
|
|
|
private void OnRelease(byte[] obj)
|
|
{
|
|
using var ms = new System.IO.MemoryStream(obj);
|
|
using var reader = new System.IO.BinaryReader(ms);
|
|
dictionary.Clear();
|
|
|
|
var length = reader.ReadInt32();
|
|
for (var i = 0; i < length; i++)
|
|
{
|
|
var id = reader.ReadInt32();
|
|
var path = reader.ReadString();
|
|
|
|
var so = YooAssets.LoadAssetSync(path).AssetObject.As<AssetableItem>();
|
|
var item = new ManagedItem();
|
|
item.CopyItemsFrom(so);
|
|
item.Id = id;
|
|
dictionary.Add(id,item);
|
|
}
|
|
RebuildInternal();
|
|
}
|
|
|
|
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
|
|
} |