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

103 lines
3.2 KiB
C#
Raw Normal View History

2023-08-27 02:58:19 +08:00
using System;
2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
2023-08-27 02:58:19 +08:00
using System.Linq;
2023-10-02 23:24:56 +08:00
using BITFALL.Player.Inventory;
2023-06-08 14:09:50 +08:00
using UnityEngine;
using BITKit;
using BITKit.Entities;
namespace BITFALL
{
public interface IPlayerEquipContainer {
2023-09-01 14:33:54 +08:00
Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
Action<IEquipmentSlot, IBasicItem> OnDeEquip { get; set; }
2023-08-27 02:58:19 +08:00
bool TryDeEquip<T>(T slot=default) where T : IEquipmentSlot;
2023-06-08 14:09:50 +08:00
}
/// <summary>
/// 玩家装备容器
/// 支持,护甲,头盔和背包等
/// </summary>
2023-09-01 14:33:54 +08:00
[CustomType(typeof(IPlayerEquipContainer))]
2023-10-02 23:24:56 +08:00
public class PlayerEquipContainer : EntityComponent, IPlayerEquipContainer
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
private readonly Dictionary<IEquipmentSlot, IBasicItem> dictionary = new();
2023-10-20 19:31:12 +08:00
[Inject]
2023-08-27 02:58:19 +08:00
private IBasicItemContainer inventory;
public override void OnAwake()
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
var health = entity.Get<IHealth>();
health.OnSetAlive += OnSetAlive;
2023-06-08 14:09:50 +08:00
}
2023-08-27 02:58:19 +08:00
private void OnSetAlive(bool obj)
{
2023-09-01 14:33:54 +08:00
if (obj) return;
foreach (var x in dictionary.ToArray())
2023-08-27 02:58:19 +08:00
{
2023-09-01 14:33:54 +08:00
OnDeEquip?.Invoke(x.Key, x.Value);
inventory.Add(x.Value);
2023-08-27 02:58:19 +08:00
}
2023-09-01 14:33:54 +08:00
dictionary.Clear();
2023-08-27 02:58:19 +08:00
}
2023-06-08 14:09:50 +08:00
public override void OnStart()
{
base.OnStart();
inventory = entity.Get<IBasicItemContainer>();
2023-10-02 23:24:56 +08:00
var playerInventory = entity.Get<IPlayerInventory>();
playerInventory.OnUseItem += TryExecute;
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
public Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
public Action<IEquipmentSlot, IBasicItem> OnDeEquip { get; set; }
2023-08-27 02:58:19 +08:00
public bool TryDeEquip<T>(T slot) where T : IEquipmentSlot
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
if (!dictionary.TryGetValue(slot, out var equipable)) return false;
if (inventory.Add(equipable))
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
DeEquip(slot, equipable);
2023-06-08 14:09:50 +08:00
}
return false;
}
2023-08-27 02:58:19 +08:00
private bool Equip(IEquipmentSlot slot, IBasicItem item)
2023-06-08 14:09:50 +08:00
{
dictionary.Add(slot, item);
2023-09-01 14:33:54 +08:00
OnEquip?.Invoke(slot, item);
2023-06-08 14:09:50 +08:00
return true;
}
2023-08-27 02:58:19 +08:00
private bool DeEquip(IEquipmentSlot slot, IBasicItem item)
2023-06-08 14:09:50 +08:00
{
dictionary.Remove(slot);
2023-09-01 14:33:54 +08:00
OnDeEquip?.Invoke(slot, item);
2023-06-08 14:09:50 +08:00
return true;
}
2023-09-01 14:33:54 +08:00
public int Priority => 0;
2023-06-08 14:09:50 +08:00
2023-08-27 02:58:19 +08:00
public bool TryExecute(IBasicItem value)
2023-06-08 14:09:50 +08:00
{
var asset = value.GetAssetable();
2023-08-27 02:58:19 +08:00
//尝试获取可装备信息
if (!asset.TryGetProperty<EquipmentAsSlot>(out var equipable)) return false;
//已装备物品
if (dictionary.TryGetValue(equipable.slot, out var equipedItem))
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
//尝试将装配放回背包
if (inventory.Add(equipedItem))
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
//移除已装备物品
DeEquip(equipable.slot, value);
2023-06-08 14:09:50 +08:00
}
}
2023-08-27 02:58:19 +08:00
//从库存中移除物品
if (inventory.Remove(value))
{
//装配物品
Equip(equipable.slot, value);
}
2023-06-08 14:09:50 +08:00
return false;
}
}
}