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

@@ -0,0 +1,26 @@
{
"name": "BITFALL.Entities.Armor.Runtime",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:48ef04d98836e2640bf90b524bdff904",
"GUID:677cd05ca06c46b4395470200b1acdad",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:c0b9c98c59e49554c8f4ca6dc4998d79",
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
"GUID:7efac18f239530141802fb139776f333",
"GUID:30cdc242b1ac6a944a460f4ab0b77b88",
"GUID:84d565da37ad40546a118cfb3c3509f3",
"GUID:42a9827d94e00374aa52e51f0a1b035c"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

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;
}
}
}