//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Monday, September 11, 2017
|
//--------------------------------------------------------
|
using UnityEngine;
|
using System.Collections;
|
using UnityEngine.UI;
|
|
using System.Text;
|
|
namespace vnxbqy.UI
|
{
|
|
public class BossLifeBar : MonoBehaviour
|
{
|
[SerializeField] Sprite[] m_LifeBarSprites;
|
|
[SerializeField] Image m_BossIcon;
|
[SerializeField] Image m_BackGround;
|
[SerializeField] Image m_MiddleGround;
|
[SerializeField] Image m_PrefaceGround;
|
|
[SerializeField] Slider m_SliderMiddleground;
|
[SerializeField] Slider m_SliderForeground;
|
|
[SerializeField] Text m_Surplus;
|
[SerializeField] Image m_Realm;
|
[SerializeField] Text m_BossName;
|
[SerializeField] Text m_BossLevel;
|
|
[SerializeField] float m_SmoothTime = 0.3f;
|
public float smoothTime {
|
get { return m_SmoothTime; }
|
}
|
|
Pattern pattern = Pattern.Reduce;
|
float targetValue = 0f;
|
int totalSegments = 1;
|
int surplusSegments = -1;
|
|
float refValue = 0f;
|
float timer = 0f;
|
float behaviourStartValue = 0f;
|
|
float m_CurrentBehaviourValue = 0f;
|
float currentBehaviourValue {
|
get { return m_CurrentBehaviourValue; }
|
set {
|
m_CurrentBehaviourValue = value;
|
UpdateSurplusSegments(currentBehaviourValue);
|
var behaviourDecimalValue = m_CurrentBehaviourValue - (int)m_CurrentBehaviourValue;
|
var trueDecimalValue = targetValue - (int)targetValue;
|
switch (pattern)
|
{
|
case Pattern.Add:
|
m_SliderForeground.value = behaviourDecimalValue;
|
m_SliderMiddleground.value = behaviourDecimalValue > trueDecimalValue ? 1f : trueDecimalValue;
|
break;
|
case Pattern.Reduce:
|
m_SliderMiddleground.value = behaviourDecimalValue;
|
m_SliderForeground.value = behaviourDecimalValue < trueDecimalValue ? 0 : trueDecimalValue;
|
break;
|
case Pattern.None:
|
m_SliderMiddleground.value = behaviourDecimalValue;
|
m_SliderForeground.value = behaviourDecimalValue;
|
break;
|
}
|
}
|
}
|
|
public void SetBaseInfo(int _npcId, ulong _hp, ulong _maxHp, int _level)
|
{
|
var npcConfig = NPCConfig.Get(_npcId);
|
m_BossName.text = npcConfig.charName;
|
m_BossLevel.text = _level.ToString();
|
m_BossIcon.SetSprite(npcConfig.HeadPortrait);
|
|
if (m_Realm != null)
|
{
|
if (npcConfig.Realm > 0 && RealmConfig.Has(npcConfig.Realm))
|
{
|
m_Realm.SetActive(true);
|
var realmConfig = RealmConfig.Get(npcConfig.Realm);
|
m_Realm.SetSprite(realmConfig.Img);
|
}
|
else
|
{
|
m_Realm.SetActive(false);
|
}
|
}
|
|
surplusSegments = -1;
|
totalSegments = npcConfig.LifeBarCount;
|
targetValue = currentBehaviourValue = ((float)_hp / _maxHp) * totalSegments - 0.0001f;
|
var behaviourDecimalValue = currentBehaviourValue - (int)currentBehaviourValue;
|
m_SliderForeground.value = m_SliderMiddleground.value = behaviourDecimalValue;
|
|
refValue = 0f;
|
}
|
|
public void Show(ulong _hp, ulong _maxHp)
|
{
|
var percentage = Mathf.Clamp(_hp, 0, _maxHp) / (float)_maxHp;
|
var tempValue = totalSegments * percentage - 0.00001f;
|
pattern = tempValue > targetValue ? Pattern.Add : tempValue < targetValue ? Pattern.Reduce : Pattern.None;
|
|
behaviourStartValue = currentBehaviourValue;
|
targetValue = tempValue;
|
|
timer = 0f;
|
refValue = 0f;
|
}
|
|
private void LateUpdate()
|
{
|
if (Mathf.Abs(currentBehaviourValue - targetValue) > 0.00001f)
|
{
|
var newValue = Mathf.SmoothDamp(currentBehaviourValue, targetValue, ref refValue, smoothTime);
|
currentBehaviourValue = newValue;
|
}
|
}
|
|
static StringBuilder stringBuild = new StringBuilder();
|
private void UpdateSurplusSegments(float _targetValue)
|
{
|
var currentSurplus = Mathf.CeilToInt(_targetValue);
|
if (currentSurplus != surplusSegments)
|
{
|
surplusSegments = currentSurplus;
|
var colorSetLength = m_LifeBarSprites.Length;
|
var index = surplusSegments % colorSetLength;
|
var nextIndex = index == 0 ? colorSetLength - 1 : index - 1;
|
|
m_PrefaceGround.overrideSprite = m_LifeBarSprites[index];
|
m_MiddleGround.overrideSprite = m_LifeBarSprites[index];
|
m_BackGround.overrideSprite = m_LifeBarSprites[nextIndex];
|
|
m_BackGround.SetActive(surplusSegments > 1);
|
|
var chars = surplusSegments.ToString();
|
stringBuild.Remove(0, stringBuild.Length);
|
for (var i = 0; i < chars.Length; i++)
|
{
|
var numChar = GetNumKey(chars[i]);
|
if (numChar > 0)
|
{
|
stringBuild.Append((char)numChar);
|
}
|
}
|
|
m_Surplus.text = stringBuild.ToString();
|
}
|
}
|
|
int GetNumKey(int _num)
|
{
|
var config = DamageNumConfig.Get("BossLifeBarSurplusNum");
|
return config.nums[_num - 48];
|
}
|
|
|
enum Pattern
|
{
|
None,
|
Add,
|
Reduce,
|
}
|
|
}
|
|
}
|