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; private readonly IntervalUpdate healFreeze = new(4); 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) { foreach (var (type, _wound) in _playerMedical.GetAllWounds()) { var wound = (PlayerMedicalWound)_wound; wound.RemainingHealingTime -= obj; if (wound.RemainingHealingTime <= 0) { _playerMedical.RemoveWound(type); } } if (_health.IsAlive is false || _knockDown is { IsKnockdown: true }) return; if (healFreeze.AllowUpdateWithoutReset) if (_playerMedical.GetAllWounds().Count is 0 && _health.HealthPoint < _health.MaxHealthPoint) { _health.HealthPoint += 1; } } private void OnUsedItem(IBasicItem obj) { var scriptable = obj.GetAssetable(); switch (scriptable) { case not null when scriptable.TryGetProperty(out _): _playerMedical.RemoveWound(); break; case not null when scriptable.TryGetProperty(out _): _playerMedical.RemoveWound(); break; case not null when scriptable.TryGetProperty(out _): _playerMedical.RemoveWound(); break; case not null when scriptable.TryGetProperty(out _): _playerMedical.RemoveWound(); break; case not null when scriptable.TryGetProperty(out _): _playerMedical.RemoveWound(); break; } } private void OnDamageRelease(DamageMessage obj) { if (obj.Damage is 0) return; healFreeze.Reset(); switch (obj.DamageType) { case MeleeDamageMessage meleeMessage: if(meleeMessage.Bleeding) _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =meleeMessage.GetType().Name, RemainingHealingTime = 60 }); else _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =meleeMessage.GetType().Name, RemainingHealingTime = 60 }); break; case IPlayerExplosionDamage: case BulletDamageMessage: if (obj.DamageType is IPlayerExplosionDamage) { _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =obj.DamageType.GetType().Name, RemainingHealingTime = 60 }); } _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =obj.DamageType.GetType().Name, RemainingHealingTime = 60 }); _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =obj.DamageType.GetType().Name, RemainingHealingTime = 60 }); _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =obj.DamageType.GetType().Name, RemainingHealingTime = 60 }); _playerMedical.AddOrUpdateWound(new PlayerMedicalWound() { Level = 1, Source =obj.DamageType.GetType().Name, RemainingHealingTime = 60 }); break; } } } }