1
This commit is contained in:
77
Assets/Artists/Scripts/Entities/Knockdown/EntityKnockdown.cs
Normal file
77
Assets/Artists/Scripts/Entities/Knockdown/EntityKnockdown.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BITFALL.Items;
|
||||
using BITFALL.Player.Inventory;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITFALL.Entities
|
||||
{
|
||||
[CustomType(typeof(IKnockdown))]
|
||||
public sealed class EntityKnockdown :EntityComponent,IKnockdown
|
||||
{
|
||||
[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]
|
||||
private IPlayerInventory _inventory;
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
_health.OnDamage += OnDamage;
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
_health.OnSetHealthPoint += OnSetHealthPoint;
|
||||
|
||||
_inventory.OnUseItem += OnUseItem;
|
||||
}
|
||||
|
||||
private void OnSetHealthPoint(int obj)
|
||||
{
|
||||
if (obj > 0 && IsKnockdown && _health.IsAlive)
|
||||
{
|
||||
IsKnockdown = false;
|
||||
OnRevive?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private bool OnUseItem(IBasicItem arg)
|
||||
{
|
||||
if (IsKnockdown is false || arg.GetAssetable().TryGetProperty<PlayerReviveItem>(out var reviveItem) is false) return false;
|
||||
OnRevive?.Invoke();
|
||||
IsKnockdown = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool obj)
|
||||
{
|
||||
IsKnockdown = false;
|
||||
}
|
||||
|
||||
private int OnDamage(DamageMessage arg,int currentDamage)
|
||||
{
|
||||
if (IsKnockdown || _health.HealthPoint - currentDamage >=0 ) return currentDamage;
|
||||
IsKnockdown = true;
|
||||
OnKnockdown?.Invoke();
|
||||
_health.HealthPoint = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user