This commit is contained in:
CortexCore
2023-10-20 22:46:14 +08:00
parent a160813262
commit 325f63d6bc
42 changed files with 1602 additions and 79 deletions

View File

@@ -1,12 +1,18 @@
{
"name": "BITFALL.Entities.Equipment",
"name": "BITFALL.Entities.Armor.Runtime",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:48ef04d98836e2640bf90b524bdff904",
"GUID:677cd05ca06c46b4395470200b1acdad",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:677cd05ca06c46b4395470200b1acdad",
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0"
"GUID:c0b9c98c59e49554c8f4ca6dc4998d79",
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
"GUID:7efac18f239530141802fb139776f333",
"GUID:30cdc242b1ac6a944a460f4ab0b77b88",
"GUID:84d565da37ad40546a118cfb3c3509f3",
"GUID:42a9827d94e00374aa52e51f0a1b035c"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -0,0 +1,106 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITFALL.Entities.Equipment;
using BITFALL.Entities.Inventory;
using BITFALL.Items.Armor;
using BITKit;
using BITKit.Entities;
using UnityEngine;
namespace BITFALL.Entities.Armor
{
[CustomType(typeof(IArmor))]
public class EntityArmor : EntityComponent,IArmor
{
private int _armor;
public int Armor
{
get => _armor;
set=>OnArmorChanged?.Invoke(_armor = value);
}
public bool TryGetCurrentArmor(out IBasicItem item)
{
item = _currentArmor;
return _currentArmor is not null;
}
public event Action<int> OnArmorChanged;
public event Action<IBasicItem> OnEquipArmor;
public event Action<IBasicItem> OnUnEquipArmor;
[Inject] private IHealth _health;
[Inject] private IPlayerEquipSelector _playerEquipSelector;
[Inject] private IEntityInventory _inventory;
[Inject] private IEntityEquipmentContainer _equipmentContainer;
private IBasicItem _currentArmor;
public override void OnAwake()
{
base.OnAwake();
_health.OnDamage += OnDamage;
_inventory.OnUsed += OnUsed;
_equipmentContainer.OnEquip += OnEquip;
_equipmentContainer.OnDeEquip += OnDeEquip;
_playerEquipSelector.OnTryEquip += OnTryEquip;
}
private bool OnTryEquip(IBasicItem arg)
{
if (arg is null) return true;
if (arg.GetAssetable().TryGetProperty<AddArmor>(out _))
{
if (_currentArmor is null)
{
return false;
}
if (Armor == _currentArmor.GetAssetable().As<AssetableArmor>().MaxArmor)
{
return false;
}
}
return true;
}
private void OnDeEquip(IEquipmentSlot arg1, IBasicItem arg2)
{
if (arg1 is not EquipmentAsArmor) return;
_currentArmor = null;
OnUnEquipArmor?.Invoke(arg2);
}
private void OnEquip(IEquipmentSlot arg1, IBasicItem arg2)
{
if (arg1 is not EquipmentAsArmor) return;
_currentArmor = arg2;
OnEquipArmor?.Invoke(arg2);
}
private void OnUsed(IBasicItem obj)
{
if (_currentArmor?.GetAssetable() is not AssetableArmor assetableArmor) return;
if (obj.GetAssetable().TryGetProperty<AddArmor>(out var addArmor))
{
Armor = Mathf.Clamp(Armor + addArmor.Armor, 0, assetableArmor.MaxArmor);
}
}
private int OnDamage(DamageMessage arg1, int damage)
{
if (_currentArmor is null) return damage;
if (Armor is 0) return damage;
if (damage > Armor)
{
Armor = 0;
return damage-Armor;
}
Armor -= damage;
return 0;
}
}
}

View File

@@ -15,7 +15,9 @@ namespace BITFALL
/// </summary>
public interface IPlayerEquipSelector
{
event Func<IBasicItem,bool> OnTryEquip;
event Action<IDictionary<int, IBasicItem>> OnUpdateEquip;
bool TryDeEquip(IBasicItem item);
bool Cancel();
}
}

View File

@@ -0,0 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using UnityEngine;
namespace BITFALL.Entities.Inventory
{
}

View File

@@ -26,6 +26,8 @@ namespace BITFALL.Entities.Equipment
{
[Header(Constant.Header.InternalVariables)]
private readonly Dictionary<int, IBasicItem> equips=new();
public event Func<IBasicItem, bool> OnTryEquip;
public event Action<IDictionary<int, IBasicItem>> OnUpdateEquip;
[Inject(true)]
@@ -179,8 +181,13 @@ namespace BITFALL.Entities.Equipment
switch (asset)
{
case var _ when asset.TryGetProperty<EquipmentUseItem>(out _):
if (OnTryEquip?.CastAsFunc().Any(x => x.Invoke(value)) is false) return false;
_equipment.EntryEquip(value);
_improvisedService?.TryUnEquipImprovised(out _);
if (currentEquip is not null)
{
_cachedItem.Release(currentEquip);
}
currentEquip = value;
return true;
}
@@ -263,6 +270,18 @@ namespace BITFALL.Entities.Equipment
return true;
}
public bool Cancel()
{
if (currentEquip is null) return false;
if (_cachedItem.TryGetRelease(out var item))
{
Equip(item);
return true;
}
Equip(null);
return false;
}
private void Equip(IBasicItem item)
{
_equipment.EntryEquip(item);

View File

@@ -13,7 +13,9 @@
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:30cdc242b1ac6a944a460f4ab0b77b88"
"GUID:30cdc242b1ac6a944a460f4ab0b77b88",
"GUID:7efac18f239530141802fb139776f333",
"GUID:ef0bb553b58b90b488bdbe8672e3be0b"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -1,17 +0,0 @@
using BITKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace BITFALL
{
public interface IEquipmentAsArms : IProperty { }
[System.Serializable]
public record EquipmentAsWeapon: IEquipmentAsArms { }
[System.Serializable]
public record EquipmentUseItem: IEquipmentAsArms { }
}

View File

@@ -1,38 +0,0 @@
using BITKit;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITFALL
{
public interface IEquipmentSlot
{
}
public abstract record EquipmentSlot: IEquipmentSlot
{
public override int GetHashCode() => GetType().GetHashCode();
}
[System.Serializable]
public sealed record EquipmentAsHead: EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsBody : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsArmor : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsHeal : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsBackpack : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsArmorPlates : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsTactics : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsThrow : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsEquip : EquipmentSlot { }
[System.Serializable]
public sealed record EquipmentAsSlot : IProperty
{
[SerializeReference, SubclassSelector] public IEquipmentSlot slot;
}
}

View File

@@ -1,27 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.Core.Entites;
using UnityEngine;
namespace BITFALL.Entities.Equipment
{
public interface IEntityEquipment
{
event Action<IBasicItem> OnEquip;
event Action<IBasicItem> OnDeEquip;
bool IsSupportItem(IBasicItem item);
void EntryEquip(int index);
void EntryEquip(Func<string,bool> item);
void EntryEquip(IBasicItem item);
}
public interface IEquipBase : IEntryElement, IAwake, IStart, IUpdate
{
string AddressablePath { get; }
IEntity Entity { get; set; }
IBasicItem Item { get; set; }
bool IsSupportItem(IBasicItem item);
void PlayAudio(string name);
}
}

View File

@@ -36,14 +36,15 @@ namespace BITKit.Entities
allowGlobalActivation = true
};
protected readonly ValidHandle AllowRendering = new();
public virtual string AddressablePath => throw new System.NotImplementedException();
public virtual string AddressablePath => item.AddressablePath;
protected virtual Vector3 meleeForce => Transform.forward;
public bool IsEntered { get; set; }
public virtual void Entry()
{
AllowRendering.AddElement(this);
inputActionGroup.allowInput.AddElement(this);
animator.animator.enabled = true;
animator.animator.Update(0);
}
public virtual UniTask EntryAsync()
{
@@ -52,7 +53,7 @@ namespace BITKit.Entities
public virtual void Exit()
{
inputActionGroup.allowInput.AddElement(this);
inputActionGroup.allowInput.RemoveElement(this);
}
public virtual UniTask ExitAsync()
{
@@ -61,8 +62,13 @@ namespace BITKit.Entities
}
public virtual void OnAwake()
{
AllowRendering.AddListener(x=>renderers.ForEach(y=>y.enabled = x));
AllowRendering.AddListener(x=>renderers.ForEach(y=>
{
y.enabled = x;
animator.animator.enabled = x;
}));
AllowRendering.Invoke();
Initialize();
}

View File

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

View File

@@ -64,7 +64,7 @@ namespace BITFALL
public override void OnAwake()
{
_health.OnSetAlive += OnSetAlive;
}
}
private void OnSetAlive(bool alive)
{
if (alive) return;

View File

@@ -1,4 +1,5 @@
using System;
using BITFALL.Entities.Equipment;
using UnityEngine;
using BITKit;
using BITKit.Entities;
@@ -27,7 +28,7 @@ namespace BITFALL
container.OnUsed += OnRemove;
container.OnRemove += OnRemove;
var playerEquipContainer = entity.Get<IPlayerEquipContainer>();
var playerEquipContainer = entity.Get<IEntityEquipmentContainer>();
playerEquipContainer.OnEquip += OnEquip;
playerEquipContainer.OnDeEquip += DeEquip;
}

View File

@@ -9,11 +9,7 @@ using BITKit.Entities;
namespace BITFALL
{
public interface IPlayerEquipContainer {
Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
Action<IEquipmentSlot, IBasicItem> OnDeEquip { get; set; }
bool TryDeEquip<T>(T slot=default) where T : IEquipmentSlot;
}
/// <summary>
/// 玩家装备容器
/// 支持,护甲,头盔和背包等

View File

@@ -14,7 +14,7 @@ namespace BITFALL
[SerializeField] private SerializedDictionary<string, GameObject> equipDictionary = new();
[Inject] private IEntityEquipment _entityEquipment;
[Inject] private IPlayerEquipContainer _playerEquipContainer;
[Inject] private IEntityEquipmentContainer _playerEquipContainer;
[Inject] private IPlayerEquipSelector _playerEquipSelector;
public override void OnStart()