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

88 lines
2.1 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;
2023-11-15 23:54:54 +08:00
[SerializeField] private Optional<float> invincibilityTimes;
2023-10-20 19:31:12 +08:00
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();
2023-11-15 23:54:54 +08:00
private readonly IntervalUpdate invincibilityInterval = new();
2023-10-20 19:31:12 +08:00
[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
{
2023-11-15 23:54:54 +08:00
if (invincibilityTimes.Allow && invincibilityInterval.AllowUpdateWithoutReset is false)
{
return 0;
}
2023-10-20 19:31:12 +08:00
if (IsKnockdown || _health.HealthPoint - currentDamage >=0 ) return currentDamage;
IsKnockdown = true;
2023-11-15 23:54:54 +08:00
if (invincibilityTimes.Allow)
{
invincibilityInterval.Interval = invincibilityTimes.Value;
invincibilityInterval.Reset();
}
2023-10-20 19:31:12 +08:00
OnKnockdown?.Invoke();
_health.HealthPoint = 0;
return 0;
}
}
}