Health System

This commit is contained in:
ThomasCharester committed 2026-11-17 18:44:54 +03:00
1 parent 329cc98ade
commit cb05da5e89
87 files changed
+568550 -155362

No files matched your search

@@ -8,21 +8,20 @@ namespace Code.OfficeGame.BattleMode.Health
{
public class EntityHealth : MonoBehaviour
{
[SerializeField] private int maxHealth;
[SerializeField] private int currentHealth;
public ReactiveProperty<int> _maxHealth;
public ReactiveProperty<int> _currentHealth;
[SerializeField] private HealthData healthData;
public ReadOnlyReactiveProperty<int> MaxHealth => _maxHealth;
public ReadOnlyReactiveProperty<int> CurrentHealth => _currentHealth;
private ReactiveProperty<HealthData> _healthData;
public ReadOnlyReactiveProperty<HealthData> HealthData => _healthData;
public bool IsDead => _healthData.CurrentValue.CurrentHealth <= 0;
[SerializeField]
public Dictionary<HitBoxPartType, int> hitBoxPartModifier = new();
private void Awake()
{
_maxHealth = new ReactiveProperty<int>(maxHealth);
_currentHealth = new ReactiveProperty<int>(currentHealth);
_healthData = new ReactiveProperty<HealthData>(healthData);
}
public virtual void GetDamage(int damage, HitBoxPartType hitBoxPart)
@@ -32,15 +31,42 @@ namespace Code.OfficeGame.BattleMode.Health
GetHeal(-damage);
return;
}
_currentHealth.Value -= damage * hitBoxPartModifier[hitBoxPart];
var newCurrentHealth = _healthData.CurrentValue.CurrentHealth - damage * hitBoxPartModifier[hitBoxPart];
_healthData.Value = new HealthData()
{
MaxHealth = _healthData.CurrentValue.MaxHealth,
CurrentHealth = newCurrentHealth
};
Debug.Log($"{GetEntityId()} got {damage} damage in {hitBoxPart} and have {newCurrentHealth} health");
}
public virtual void GetHeal(int heal)
{
if (heal < 0) return;
_currentHealth.Value += heal;
var newCurrentHealth = _healthData.CurrentValue.CurrentHealth + heal;
_healthData.Value = new HealthData()
{
MaxHealth = _healthData.CurrentValue.MaxHealth,
CurrentHealth = newCurrentHealth
};
}
public virtual void SetHealthData(HealthData newHealthData)
{
_healthData.Value = newHealthData;
}
}
[Serializable]
public struct HealthData
{
public int MaxHealth;
public int CurrentHealth;
}
/// Социальные метрики
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using Code.Infrastructure.Camera;
using Code.Infrastructure.UI;
using Code.OfficeGame.BattleMode.Actions;
using Code.OfficeGame.BattleMode.Health.UI;
using Cysharp.Threading.Tasks;
using Reflex.Attributes;
namespace Code.OfficeGame.BattleMode.Health
{
public class HealthPanelsHolder : IDisposable
{
public List<EntityHealthPanel> PanelList;
[Inject]
private void Construct(CameraStateMachine cameraStateMachine)
{
foreach (var panel in PanelList) panel.Construct(cameraStateMachine);
}
/// <summary>
/// Список опрокинуть в контейнер
/// </summary>
/// <param name="factory"></param>
/// <param name="entities"></param>
/// <returns></returns>
public async UniTask SpawnHealthBars(
IUIFactory factory,
List<EntityProperties> entities)
{
PanelList = new List<EntityHealthPanel>();
foreach (var entity in entities)
{
var healthBarTarget = entity.GetComponentInChildren<HealthBarTarget>();
if(!healthBarTarget) continue;
var panel = (EntityHealthPanel) await factory.LoadPanel(UIPanelType.EntityHealthBar, false);
panel.SetTarget(healthBarTarget.transform);
panel.SubscribeHealthPanel(entity.EntityHealth);
PanelList.Add(panel);
}
}
public void Dispose()
{
foreach (var panel in PanelList)
panel.Dispose();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5e6246babc37464a867f371c1df5f395
timeCreated: 1794926045
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 285e4ad050214d57bd139507b18394e3
timeCreated: 1794920071
@@ -0,0 +1,105 @@
using System;
using System.Threading;
using Code.Infrastructure.Camera;
using Code.Infrastructure.UI;
using Cysharp.Threading.Tasks;
using R3;
using Reflex.Attributes;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Code.OfficeGame.BattleMode.Health.UI
{
public class EntityHealthPanel : UIPanel
{
[SerializeField] private TMP_Text healthText;
[SerializeField] private Image healthBar;
[SerializeField] private Color criticalColor;
[SerializeField] private Color fullColor;
[SerializeField] private Color deadColor;
[SerializeField]
[Range(0.1f,0.9f)]
private float criticalPercent;
private DisposableBag _healthSubscriptions;
private Camera _camera;
private Transform _target;
[Inject]
public void Construct(CameraStateMachine cameraStateMachine)
{
_camera = cameraStateMachine.Camera;
}
private void LateUpdate()
{
if (!_target || !_camera) return;
Vector3 screenPos = _camera.WorldToScreenPoint(_target.position);
bool isVisible = screenPos.z > 0;
if (gameObject.activeSelf != isVisible)
gameObject.SetActive(isVisible);
if (isVisible)
transform.position = screenPos;
}
public void SetTarget(Transform target)
{
_target = target;
}
public void SubscribeHealthPanel(EntityHealth entityHealth)
{
entityHealth.HealthData
.Subscribe(SetHealthStatus)
.AddTo(ref _healthSubscriptions);
}
private void SetHealthStatus(HealthData healthData)
{
if (healthData.CurrentHealth <= 0)
{
healthText.color = deadColor;
healthText.SetText("Dead");
healthBar.fillAmount = 1;
return;
}
float healthPercentage = (float)healthData.CurrentHealth / healthData.MaxHealth;
healthBar.fillAmount = healthPercentage;
// SB
healthText.SetText($"{healthData.CurrentHealth}/{healthData.MaxHealth}");
if (healthPercentage <= criticalPercent)
healthBar.color = criticalColor;
else
healthBar.color = fullColor;
}
public override UniTask Open(CancellationToken cts = default)
{
gameObject.SetActive(true);
return UniTask.CompletedTask;
}
public override UniTask Close(CancellationToken cts = default)
{
gameObject.SetActive(false);
return UniTask.CompletedTask;
}
private void OnDestroy()
{
_healthSubscriptions.Dispose();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4bfc885db5af44af89786ce14877955e
timeCreated: 1794920269
@@ -0,0 +1,7 @@
using UnityEngine;
namespace Code.OfficeGame.BattleMode.Health.UI
{
public class HealthBarTarget : MonoBehaviour
{ }
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d9cf2c0132634b5591ef2553223721ca
timeCreated: 1794923322