BITFALL/Assets/Artists/Scripts/Entities/Inventory/PlayerInventory.cs

167 lines
4.9 KiB
C#
Raw Normal View History

2023-10-20 19:31:12 +08:00
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;
2024-01-03 00:27:12 +08:00
using System.Security.Cryptography;
2023-10-20 19:31:12 +08:00
using BITFALL.Entities;
using BITFALL.Entities.Improvised;
2023-12-15 00:08:02 +08:00
using BITFALL.Items;
2023-10-20 19:31:12 +08:00
using BITFALL.Player.Inventory;
using BITKit.Selection;
2023-12-03 17:35:43 +08:00
using BITKit.UX;
2023-12-15 00:08:02 +08:00
using Unity.Mathematics;
2023-10-20 19:31:12 +08:00
namespace BITFALL
{
[CustomType(typeof(IPlayerInventory))]
public class PlayerInventory : EntityInventory,IPlayerInventory
{
2024-01-03 00:27:12 +08:00
[SerializeField] private int currentMoney;
2023-12-15 00:08:02 +08:00
[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;
2023-10-20 19:31:12 +08:00
[Inject]
private ISelector _selector;
[Inject(true)]
private ImprovisedServiceInterface _improvisedService;
[Inject(true)]
private IKnockdown _knockdown;
2024-01-03 00:27:12 +08:00
[Inject(true)]
private IUXPopup _popup;
2023-10-20 19:31:12 +08:00
public override void OnStart()
{
base.OnStart();
_selector.OnActive += OnActive;
if (_improvisedService is not null)
{
_improvisedService.OnUnEquipImprovisedItem += OnUnEquipImprovisedItem;
}
2023-12-03 17:35:43 +08:00
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}]");
2023-10-20 19:31:12 +08:00
}
2023-11-30 00:23:23 +08:00
protected override void OnSetAlive(bool alive)
{
var keepInventory = Data.Get<bool>(BITConstant.Environment.sp_keepInventory);
if (keepInventory)
{
}
else
{
base.OnSetAlive(alive);
}
}
2023-10-20 19:31:12 +08:00
private void OnUnEquipImprovisedItem(IBasicItem obj)
{
Drop(obj);
}
2023-12-15 00:08:02 +08:00
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);
}
}
2023-10-20 19:31:12 +08:00
private void OnActive(ISelectable obj)
{
2023-11-15 23:54:54 +08:00
if (!obj.Transform.TryGetComponent<WorldItem>(out var item)) return;
2023-10-24 23:37:59 +08:00
var _item = item.Pick();
2023-10-29 15:27:13 +08:00
if(item.GetAssetable().IsImprovised)
2023-10-20 19:31:12 +08:00
{
2023-10-24 23:37:59 +08:00
if (_knockdown is not null && _knockdown.IsKnockdown)
2023-10-20 19:31:12 +08:00
{
2023-10-24 23:37:59 +08:00
return;
2023-10-20 19:31:12 +08:00
}
2023-10-24 23:37:59 +08:00
if (_improvisedService.TryEquipImprovisedItem(_item))
2023-10-20 19:31:12 +08:00
{
item.Picked();
}
}
2023-10-24 23:37:59 +08:00
else if (Add(_item))
{
item.Picked();
}
2023-10-20 19:31:12 +08:00
}
2024-01-03 00:27:12 +08:00
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}");
}
}
2023-10-20 19:31:12 +08:00
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(PlayerInventory))]
2024-01-03 00:27:12 +08:00
public class EntityPlayerInventoryInspector : BITInspector<PlayerInventory>
2023-10-20 19:31:12 +08:00
{
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
}