You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

145 lines
4.9 KiB

using System;
using CoreGame;
using Proto;
using UnityEngine;
namespace CoreGame.Render
{
[Combat]
public class StateBarProxy : ProxyBase
{
internal StateBarRender render;
private bool m_ShowStateBar;
internal float stateBarShowTime;
// HP Bar
internal HpBarItem hpBar;
private CallbackWrapper m_ShowHpBarCallback;
// Ammo Bar
internal AmmoBarItem ammoBar;
private CallbackWrapper m_ShowAmmoBarCallback;
protected override void Initialize(GameObject go, CombatEntity ent)
{
var bpTrans = ent.bindPointProxy.GetBindPoint(BindPointType.StateBar);
render = bpTrans.GetComponentInChildren<StateBarRender>();
m_ShowStateBar = true;
if (render.hideDefault)
ShowStateBar(false);
hpBar = SptPool<HpBarItem>.Malloc();
hpBar.Init(render);
hpBar.SetHpBar(1f, 0);
m_ShowHpBarCallback = CallbackWrapper.New(OnShowHurt);
ent.AddEventCallBack(ClientEvent.ShowHurt, m_ShowHpBarCallback);
if (render.hasAmmo)
{
ammoBar = SptPool<AmmoBarItem>.Malloc();
ammoBar.Init(render);
ammoBar.SetAmmoBar(1f);
m_ShowAmmoBarCallback = CallbackWrapper.New(OnShowAmmo);
ent.AddEventCallBack(ClientEvent.ShowAmmo, m_ShowAmmoBarCallback);
}
render.SetLayoutByFixedAffix(ent.blackboard.affixIds);
render.SetPickUpProgress(0f);
}
public void UpdatePickUpProgress()
{
if (render.pickUpProgress && ent.hasWaitPickUpDropItem)
{
var waitPickUpDropItem = ent.waitPickUpDropItem;
var dropItem = Contexts.Combat.GetEntity(waitPickUpDropItem.dropItemEid);
if (dropItem.IsValid() && dropItem.hasWaitToBeAttracted)
{
var waitToBeAttracted = dropItem.waitToBeAttracted;
var progress = waitPickUpDropItem.timeAcc / waitToBeAttracted.pickUpTime;
render.SetPickUpProgress(progress);
return;
}
}
render.SetPickUpProgress(0f);
}
public void ShowStateBar(bool active)
{
if (m_ShowStateBar == active)
return;
m_ShowStateBar = active;
render.transform.localPosition = active ? Vector3.zero : new Vector3(0f, 10000f, 0f);
}
protected override void OnReset()
{
ent.RemoveCallback(ClientEvent.ShowHurt, ref m_ShowHpBarCallback);
SptPool<HpBarItem>.Free(ref hpBar);
if (render.hasAmmo)
{
ent.RemoveCallback(ClientEvent.ShowAmmo, ref m_ShowAmmoBarCallback);
SptPool<AmmoBarItem>.Free(ref ammoBar);
}
stateBarShowTime = 0f;
m_ShowStateBar = false;
render = null;
}
public void RefreshHpBar(PropertyContainer property)
{
var currHp = property.GetProperty_Long(PropertyDef.CurHp);
var maxHp = property.TotalHp;
var hpShield = property.GetProperty_Long(PropertyDef.Calc_HpShield);
double hpShieldRate;
double hpRate;
if (hpShield + currHp > maxHp)
{
hpRate = (currHp * 1.0) / (hpShield + currHp);
hpShieldRate = 1;
}
else
{
hpRate = (currHp * 1.0) / maxHp;
hpShieldRate = (hpShield + currHp) * 1.0 / maxHp;
}
hpBar.SetHpBar((float)hpRate, (float)hpShieldRate);
}
// todo: 优化重新计算的当前生命值
private void OnShowHurt(object self, int eventId, object hurtData)
{
var evt = (HurtData)hurtData;
if (Mathf.Abs(evt.calculateDeltaHp + evt.hpShieldDelta) > 0.001f && render.hideDefault)
stateBarShowTime = BattleConst.StateBarShowTime.AsFloat;
var property = ent.property;
RefreshHpBar(property.container);
}
public void RefreshAmmoBar(PropertyContainer property)
{
var currAmmo = (int)property.GetProperty(PropertyDef.Ammo);
var maxAmmo = (int)property.GetProperty(PropertyDef.AmmoMax);
ammoBar.SetAmmoBar((float)currAmmo / maxAmmo);
}
private void OnShowAmmo(object self, int eventId, object data)
{
var temp = (AmmoBarData)data;
if (temp.isRate)
{
ammoBar.SetAmmoBar(temp.ammoRate);
ammoBar.ReloadAmmoEnd();
}
else
{
ammoBar.SetAmmoBar(0f);
ammoBar.ReloadAmmoStart(temp.reloadCdTotal, temp.reloadCdAcc);
}
}
}
}