BITFALL/Assets/Artists/Scripts/Entities/Knockdown/EntityKnockdown.cs

76 lines
1.7 KiB
C#
Raw Normal View History

2023-10-20 19:31:12 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2023-10-24 23:37:59 +08:00
using BITFALL.Entities.Inventory;
2023-10-20 19:31:12 +08:00
using BITFALL.Items;
using BITKit;
using BITKit.Entities;
using UnityEditor;
using UnityEngine;
namespace BITFALL.Entities
{
[CustomType(typeof(IKnockdown))]
2023-10-30 01:25:53 +08:00
public sealed class EntityKnockdown :EntityBehavior,IKnockdown
2023-10-20 19:31:12 +08:00
{
[SerializeField] private int knockedHealth;
[SerializeField] private int initialKnockedHealth;
public int KnockedHealth=>knockedHealth;
public int InitialKnockedHealth
{
get => initialKnockedHealth;
set => initialKnockedHealth = value;
}
public event Action OnKnockdown;
public event Action OnRevive;
public bool IsKnockdown { get;private set; }
private readonly Optional<int> knockedHeal=new();
[Inject]
private IHealth _health;
[Inject]
2023-10-24 23:37:59 +08:00
private IEntityInventory _inventory;
2023-10-20 19:31:12 +08:00
public override void OnStart()
{
2023-10-24 23:37:59 +08:00
_health.OnDamageFactory += OnDamageFactory;
2023-10-20 19:31:12 +08:00
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHealthPoint;
2023-10-24 23:37:59 +08:00
_inventory.OnUsedItem += OnUseItem;
2023-10-20 19:31:12 +08:00
}
private void OnSetHealthPoint(int obj)
{
2023-10-24 23:37:59 +08:00
if (obj <= 0 || !IsKnockdown || !_health.IsAlive) return;
IsKnockdown = false;
OnRevive?.Invoke();
2023-10-20 19:31:12 +08:00
}
2023-10-24 23:37:59 +08:00
private void OnUseItem(IBasicItem arg)
2023-10-20 19:31:12 +08:00
{
2023-10-24 23:37:59 +08:00
if (IsKnockdown is false ||
arg.GetAssetable().TryGetProperty<PlayerReviveItem>(out var reviveItem) is false) return;
2023-10-20 19:31:12 +08:00
IsKnockdown = false;
2023-10-24 23:37:59 +08:00
OnRevive?.Invoke();
2023-10-20 19:31:12 +08:00
}
private void OnSetAlive(bool obj)
{
IsKnockdown = false;
}
2023-10-24 23:37:59 +08:00
private int OnDamageFactory(DamageMessage arg,int currentDamage)
2023-10-20 19:31:12 +08:00
{
if (IsKnockdown || _health.HealthPoint - currentDamage >=0 ) return currentDamage;
IsKnockdown = true;
OnKnockdown?.Invoke();
_health.HealthPoint = 0;
return 0;
}
}
}