Health System
This commit is contained in:
1 parent
329cc98ade
commit
cb05da5e89
87 files changed
+568550
-155362
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user