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

171 lines
4.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using BITFALL.Entities.Inventory;
using BITFALL.Items;
using BITKit;
using BITKit.Entities;
using Cysharp.Threading.Tasks;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
namespace BITFALL.Entities
{
[CustomType(typeof(IKnockdown))]
public sealed class EntityKnockdown :EntityBehavior,IKnockdown
{
[SerializeField,ReadOnly] private int knockedHealth;
[SerializeField] private int initialKnockedHealth;
[SerializeField] private Optional<float> invincibilityTimes;
[SerializeField] private InputActionReference pressureAction;
[SerializeReference,SubclassSelector] private ITicker ticker;
[Inject(true)] private InputActionGroup _inputActionGroup;
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; }
public bool IsPressured => _isPressured;
private readonly IntervalUpdate invincibilityInterval = new();
[Inject]
private IHealth _health;
[Inject]
private IEntityInventory _inventory;
private readonly ValidHandle _isPressured=new();
public override void OnStart()
{
_health.OnDamageFactory += OnDamageFactory;
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHealthPoint;
_inventory.TryUseItemFactory+=TryUseItem;
_inventory.OnUsedItem += OnUseItem;
if (_inputActionGroup is not null)
{
_inputActionGroup.RegisterCallback(pressureAction, OnPressureAction);
}
ticker?.Add(OnTick);
destroyCancellationToken.Register(() =>
{
ticker?.Remove(OnTick);
});
}
private void OnTick(float obj)
{
if (IsKnockdown is false || _health.HealthPoint<0) return;
knockedHealth-=_isPressured?1:2;
if (knockedHealth < 0)
{
_health.HealthPoint =-1;
}
}
private void OnPressureAction(InputAction.CallbackContext obj)
{
if (IsKnockdown is false) return;
switch (obj)
{
case {interaction:PressInteraction,performed:true}:
_isPressured.AddElement("123");
break;
case {interaction:PressInteraction,canceled:true}:
_isPressured.RemoveElement("123");
break;
}
}
private bool TryUseItem(IBasicItem arg)
{
if (IsKnockdown is false) return false;
if (arg is not null && arg.GetAssetable().TryGetProperty<PlayerReviveItem>(out _))
{
_inventory.UseItem(arg);
return true;
}
return false;
}
private void OnSetHealthPoint(int obj)
{
if (obj <= 0 || !IsKnockdown || !_health.IsAlive) return;
IsKnockdown = false;
OnRevive?.Invoke();
}
private void OnUseItem(IBasicItem arg)
{
if (IsKnockdown is false ||
arg.GetAssetable().TryGetProperty<PlayerReviveItem>(out var reviveItem) is false) return;
IsKnockdown = false;
OnRevive?.Invoke();
}
private void OnSetAlive(bool obj)
{
IsKnockdown = false;
}
private int OnDamageFactory(DamageMessage arg, int currentDamage)
{
if (Data.Get<bool>(BITConstant.Environment.sp_knockdown_disabled)) return currentDamage;
if (IsKnockdown && invincibilityTimes.Allow && invincibilityInterval.AllowUpdateWithoutReset is false)
{
return 0;
}
if (currentDamage > _health.MaxHealthPoint + initialKnockedHealth) return currentDamage;
if (IsKnockdown is false && _health.HealthPoint - currentDamage >= 0) return currentDamage;
if (IsKnockdown && knockedHealth is 0)
{
return currentDamage;
}
if (IsKnockdown && KnockedHealth >= 0)
{
knockedHealth -= currentDamage;
return 0;
}
IsKnockdown = true;
knockedHealth = initialKnockedHealth;
if (invincibilityTimes.Allow)
{
invincibilityInterval.Interval = invincibilityTimes.Value;
invincibilityInterval.Reset();
}
OnKnockdown?.Invoke();
_health.HealthPoint = 0;
return 0;
}
}
}