第一批重构,主要是物理迁移脚本和部分命名空间的调整
- 结构上迁移文件,将脚本分成 Base/Runtime/Presentation/Procedure/Editor 五大文件夹 - 调整 DataTable 和 Definition 下脚本的命名空间 - 依据 UI-5层架构设计规范 拆分原先的 UI 到 Runtime 和 Presentation 下
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using System.Collections;
|
||||
using Entity;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace StarForce
|
||||
{
|
||||
public class HPBarItem : MonoBehaviour
|
||||
{
|
||||
private const float AnimationSeconds = 0.3f;
|
||||
private const float KeepSeconds = 0.4f;
|
||||
private const float FadeOutSeconds = 0.3f;
|
||||
|
||||
[SerializeField]
|
||||
private Slider m_HPBar = null;
|
||||
|
||||
private Canvas m_ParentCanvas = null;
|
||||
private RectTransform m_CachedTransform = null;
|
||||
private CanvasGroup m_CachedCanvasGroup = null;
|
||||
private EntityBase m_Owner = null;
|
||||
private int m_OwnerId = 0;
|
||||
|
||||
public EntityBase Owner
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Owner;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(EntityBase owner, Canvas parentCanvas, float fromHPRatio, float toHPRatio)
|
||||
{
|
||||
if (owner == null)
|
||||
{
|
||||
Log.Error("Owner is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_ParentCanvas = parentCanvas;
|
||||
|
||||
gameObject.SetActive(true);
|
||||
StopAllCoroutines();
|
||||
|
||||
m_CachedCanvasGroup.alpha = 1f;
|
||||
if (m_Owner != owner || m_OwnerId != owner.Id)
|
||||
{
|
||||
m_HPBar.value = fromHPRatio;
|
||||
m_Owner = owner;
|
||||
m_OwnerId = owner.Id;
|
||||
}
|
||||
|
||||
Refresh();
|
||||
|
||||
StartCoroutine(HPBarCo(toHPRatio, AnimationSeconds, KeepSeconds, FadeOutSeconds));
|
||||
}
|
||||
|
||||
public bool Refresh()
|
||||
{
|
||||
if (m_CachedCanvasGroup.alpha <= 0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_Owner != null && Owner.Available && Owner.Id == m_OwnerId)
|
||||
{
|
||||
Vector3 worldPosition = m_Owner.CachedTransform.position + Vector3.forward;
|
||||
Vector3 screenPosition = GameEntry.Scene.MainCamera.WorldToScreenPoint(worldPosition);
|
||||
|
||||
Vector2 position;
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)m_ParentCanvas.transform, screenPosition,
|
||||
m_ParentCanvas.worldCamera, out position))
|
||||
{
|
||||
m_CachedTransform.localPosition = position;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
m_CachedCanvasGroup.alpha = 1f;
|
||||
m_HPBar.value = 1f;
|
||||
m_Owner = null;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
m_CachedTransform = GetComponent<RectTransform>();
|
||||
if (m_CachedTransform == null)
|
||||
{
|
||||
Log.Error("RectTransform is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_CachedCanvasGroup = GetComponent<CanvasGroup>();
|
||||
if (m_CachedCanvasGroup == null)
|
||||
{
|
||||
Log.Error("CanvasGroup is invalid.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator HPBarCo(float value, float animationDuration, float keepDuration, float fadeOutDuration)
|
||||
{
|
||||
yield return m_HPBar.SmoothValue(value, animationDuration);
|
||||
yield return new WaitForSeconds(keepDuration);
|
||||
yield return m_CachedCanvasGroup.FadeToAlpha(0f, fadeOutDuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user