BITFALL/Assets/Artists/Scripts/Player/Medical/PlayerMedicalBehaviour.cs

143 lines
4.0 KiB
C#
Raw Normal View History

2024-03-31 23:34:22 +08:00
using System.Collections;
using System.Collections.Generic;
using BITFALL.Entities;
using BITFALL.Entities.Inventory;
using BITFALL.Player.Inventory;
using BITKit;
using BITKit.Entities;
using UnityEngine;
namespace BITFALL.Player.Medical
{
public class PlayerMedicalBehaviour : EntityBehavior
{
[SerializeReference,SubclassSelector] private ITicker ticker;
[Inject] private IPlayerMedical _playerMedical;
[Inject] private IHealth _health;
[Inject] private IEntityInventory _inventory;
[Inject(true)] private IKnockdown _knockDown;
2024-04-15 14:57:50 +08:00
private readonly IntervalUpdate healFreeze = new(4);
2024-03-31 23:34:22 +08:00
public override void OnAwake()
{
base.OnAwake();
_health.OnDamageRelease += OnDamageRelease;
_inventory.OnUsedItem += OnUsedItem;
ticker.Add(OnTick);
}
public override void OnDestroyComponent()
{
base.OnDestroyComponent();
ticker.Remove(OnTick);
}
private void OnTick(float obj)
{
2024-04-15 14:57:50 +08:00
foreach (var (type, _wound) in _playerMedical.GetAllWounds())
2024-03-31 23:34:22 +08:00
{
var wound = (PlayerMedicalWound)_wound;
wound.RemainingHealingTime -= obj;
if (wound.RemainingHealingTime <= 0)
{
_playerMedical.RemoveWound(type);
}
}
2024-04-15 14:57:50 +08:00
2024-03-31 23:34:22 +08:00
if (_health.IsAlive is false || _knockDown is { IsKnockdown: true }) return;
2024-04-15 14:57:50 +08:00
if (healFreeze.AllowUpdateWithoutReset)
if (_playerMedical.GetAllWounds().Count is 0 && _health.HealthPoint < _health.MaxHealthPoint)
{
_health.HealthPoint += 1;
}
2024-03-31 23:34:22 +08:00
}
private void OnUsedItem(IBasicItem obj)
{
var scriptable = obj.GetAssetable();
switch (scriptable)
{
case not null when scriptable.TryGetProperty<Disinfect_InternalWound>(out _):
_playerMedical.RemoveWound<PlayerDirtyInternalWound>();
break;
case not null when scriptable.TryGetProperty<Disinfect_ExternalWound>(out _):
_playerMedical.RemoveWound<PlayerDirtyExternalWound>();
break;
case not null when scriptable.TryGetProperty<Bandage_ExternalWound>(out _):
_playerMedical.RemoveWound<PlayerOpenWound>();
break;
case not null when scriptable.TryGetProperty<Bandage_Suture>(out _):
_playerMedical.RemoveWound<PlayerClosedWound>();
break;
case not null when scriptable.TryGetProperty<Clear_WoundShards>(out _):
_playerMedical.RemoveWound<PlayerDebrisWound>();
break;
}
}
private void OnDamageRelease(DamageMessage obj)
{
if (obj.Damage is 0) return;
2024-04-15 14:57:50 +08:00
healFreeze.Reset();
2024-03-31 23:34:22 +08:00
switch (obj.DamageType)
{
case MeleeDamageMessage meleeMessage:
if(meleeMessage.Bleeding)
_playerMedical.AddOrUpdateWound<PlayerOpenWound>(new PlayerMedicalWound()
{
Level = 1,
Source =meleeMessage.GetType().Name,
RemainingHealingTime = 60
});
else
_playerMedical.AddOrUpdateWound<PlayerClosedWound>(new PlayerMedicalWound()
{
Level = 1,
Source =meleeMessage.GetType().Name,
RemainingHealingTime = 60
});
break;
case IPlayerExplosionDamage:
case BulletDamageMessage:
if (obj.DamageType is IPlayerExplosionDamage)
{
_playerMedical.AddOrUpdateWound<PlayerClosedWound>(new PlayerMedicalWound()
{
Level = 1,
Source =obj.DamageType.GetType().Name,
RemainingHealingTime = 60
});
}
_playerMedical.AddOrUpdateWound<PlayerDebrisWound>(new PlayerMedicalWound()
{
Level = 1,
Source =obj.DamageType.GetType().Name,
RemainingHealingTime = 60
});
_playerMedical.AddOrUpdateWound<PlayerOpenWound>(new PlayerMedicalWound()
{
Level = 1,
Source =obj.DamageType.GetType().Name,
RemainingHealingTime = 60
});
_playerMedical.AddOrUpdateWound<PlayerDirtyExternalWound>(new PlayerMedicalWound()
{
Level = 1,
Source =obj.DamageType.GetType().Name,
RemainingHealingTime = 60
});
_playerMedical.AddOrUpdateWound<PlayerDirtyInternalWound>(new PlayerMedicalWound()
{
Level = 1,
Source =obj.DamageType.GetType().Name,
RemainingHealingTime = 60
});
break;
}
}
}
}