Files
Hasebe/Assets/Code/OfficeGame/BattleMode/Health/UI/EntityHealthPanel.cs
T

114 lines
4.0 KiB
C#

using System;
using System.Threading;
using Code.Infrastructure.Camera;
using Code.Infrastructure.UI;
using Code.OfficeGame.BattleMode.Timeline;
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 Image silentHealthBar;
[SerializeField] private Color criticalColor;
[SerializeField] private Color fullColor;
[SerializeField] private Color deadColor;
[SerializeField, Range(0.1f, 0.9f)]
private float criticalPercent;
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);
if (screenPos.z > 0)
transform.position = screenPos;
}
public void SetTarget(Transform target) => _target = target;
public IDisposable BindToTimeline(BattleTimeline timeline, EntityId entityId)
{
var bag = new DisposableBag();
void Refresh()
{
int slot = timeline.GetSlotIndex(entityId);
if (slot < 0 || timeline.Track.FrameCount == 0) return;
var viewSnap = timeline.Track.GetFrame(timeline.DisplayFrame.CurrentValue);
var endSnap = timeline.Track.GetFrame(timeline.Track.LastFrame);
var viewHp = viewSnap.Entities[slot].PhysicsAndVisuals.CurrentHealth;
var endHp = endSnap.Entities[slot].PhysicsAndVisuals.CurrentHealth;
ApplyHealthStatus(viewHp, endHp);
}
timeline.DisplayFrame.Subscribe(_ => Refresh()).AddTo(ref bag);
timeline.OnTrackRebaked.Subscribe(_ => Refresh()).AddTo(ref bag);
Refresh();
return bag;
}
private void ApplyHealthStatus(HealthData viewHp, HealthData predictedEndHp)
{
float max = Mathf.Max(1, viewHp.MaxHealth);
float viewPct = Mathf.Clamp01(viewHp.CurrentHealth / max);
float endPct = Mathf.Clamp01(predictedEndHp.CurrentHealth / max);
if (viewHp.CurrentHealth <= 0)
{
healthText.color = deadColor;
healthText.SetText("Dead");
healthBar.fillAmount = 0f;
silentHealthBar.fillAmount = 0f;
return;
}
// Основной бар — HP на текущем кадре ползунка;
// silentHealthBar — подложка прогнозируемого остатка/урона в конце превью
healthBar.fillAmount = viewPct;
silentHealthBar.fillAmount = predictedEndHp.CurrentHealth < viewHp.CurrentHealth ? viewPct : 0f;
// Если хотите показывать именно остаток после урона на healthBar, а потерянный кусок на silentHealthBar:
// healthBar.fillAmount = endPct;
// silentHealthBar.fillAmount = viewPct;
healthText.color = Color.white;
healthText.SetText($"{viewHp.CurrentHealth}/{viewHp.MaxHealth}");
healthBar.color = viewPct <= criticalPercent ? criticalColor : 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;
}
}
}