using CommonAI.Zone.Instance;
using CommonAI.Zone.Formula;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmdsCommon.Plugin;
using XmdsCommonServer.Plugin;
using XmdsCommonServer.Plugin.XmdsSkillTemplate.Skills;
using CommonAI.Zone;
using XmdsCommonSkill.Plugin.Interface;
using static XmdsCommonSkill.Plugin.Interface.ComSpellTemplate;
using CommonAI.Data;
using CommonAI.Zone.Helper;
using CommonLang;
using static CommonAI.Zone.Instance.InstanceUnit;
namespace XmdsCommonSkill.Plugin.Buffs
{
///
/// FileName: XmdsBuff_Shield_AbsorbDamage.cs
/// Author: Alex.Yu
/// Corporation:...
/// Description: 护盾型吸收伤害.
/// DateTime: 2015/6/8 21:05:08
///
public class XmdsBuff_Shield_AbsorbDamage : XmdsBuffBase
{
///
/// 吸收伤害总量.
///
public int AbsorbDamageSum = 0;
public XmdsSpellID breakSpellID = 0;
/** 触发频率 ms*/
public int HitInterval = 0;
///
/// 是否吸收过量伤害.
///
public bool AbsorbOverFlowDamage = false;
private long mNextHitTime;
private int mHandleUUID = 0;
public override int GetAbilityID()
{
return (int)XmdsCommon.Plugin.XmdsBuffProperties.XmdsBuffAbility.SHIELD_AbsorbDamage;
}
public override void CopyTo(UnitBuff other)
{
var ret = other as XmdsBuff_Shield_AbsorbDamage;
ret.AbsorbDamageSum = this.AbsorbDamageSum;
ret.AbsorbOverFlowDamage = this.AbsorbOverFlowDamage;
ret.breakSpellID = this.breakSpellID;
ret.HitInterval = this.HitInterval;
base.CopyTo(other);
}
protected override void OnBuffBegin(XmdsCommonServer.Plugin.XmdsVirtual hitter, XmdsCommonServer.Plugin.XmdsVirtual attacker, InstanceUnit.BuffState state)
{
//注册监听.
if(hitter.mUnit.IsPet)
{
mHandleUUID = hitter.RegistShareMasterDmgEvent(OnShareMasterDmgEvent, null);
}
else
{
mHandleUUID = hitter.RegistOnHitDamage(OnHandleHitDmage, null);
}
XmdsVirtual.FormatLog("BuffBegin : 单位【{0}】增加吸收伤害护盾.", hitter.mProp.ServerData.BaseInfo.name);
}
protected override void OnBuffEnd(XmdsCommonServer.Plugin.XmdsVirtual hitter, InstanceUnit.BuffState state, bool replace)
{
//取消监听.
if(hitter.mUnit.IsPet)
{
hitter.UnRegistShareMasterDmgEvent(mHandleUUID);
}
else
{
hitter.UnRegistOnHitDamage(mHandleUUID);
}
XmdsVirtual.FormatLog("BuffBegin : 单位【{0}】失去吸收伤害护盾.", hitter.mProp.ServerData.BaseInfo.name);
//护盾破碎,创建法术
if(this.breakSpellID != 0)
{
// //播放声音特效.
hitter.mUnit.Parent.unitLaunchSpell(XmdsSkillType.none, hitter.mUnit,
ComSpellTemplate.Instance().GetSpellByID(this.breakSpellID),
hitter.mUnit.X, hitter.mUnit.Y, hitter.mUnit.ID);
}
}
//单位被攻击时.伤害吸收计算.
private float OnHandleHitDmage(float damage, XmdsVirtual hitted, XmdsVirtual attacker, AttackSource source,
ref XmdsVirtual.AtkResult result, DamageType damageType, GameSkill skill, ref bool isEndDispatch)
{
return DoAbsortDmg(damage, hitted, attacker, source);
}
private int OnShareMasterDmgEvent(int hp, InstanceUnit sender, InstanceUnit unit, GameSkill skill, ref bool isEndDispatch)
{
return (int)(DoAbsortDmg(hp, unit.Virtual as XmdsVirtual, sender == null ? null : (sender.Virtual as XmdsVirtual)));
}
private float DoAbsortDmg(float damage, XmdsVirtual hitted, XmdsVirtual attacker, AttackSource source = null)
{
float ret = damage;
if (this.mNextHitTime > CommonLang.CUtils.localTimeMS)
{
return damage;
}
//damage > 0是伤害,damage < 0是加血机制.
if (damage > 0 && AbsorbDamageSum > 0)
{
ret = damage - AbsorbDamageSum;
if (ret < 0)
{
ret = 0;
if(source != null)
{
source.OutClientState = (byte)XmdsCommonServer.Plugin.XmdsVirtual.UnitHitEventState.Absorb;
}
}
int abDamage = CUtils.CastInt(damage - ret);
AbsorbDamageSum -= abDamage;
if (AbsorbDamageSum <= 0)
{
//破盾.
hitted.mUnit.removeBuff(BindBuffID, BuffState.ENDResult_HuDunBreak);
//过量伤害吸收.
if (AbsorbOverFlowDamage)
{
ret = 0;
}
}
//触发频率
if (this.HitInterval > 0)
{
this.mNextHitTime = CommonLang.CUtils.localTimeMS + this.HitInterval;
}
this.OnAbsortDamage(hitted, attacker, source);
}
return ret;
}
protected virtual void OnAbsortDamage(XmdsVirtual hitted, XmdsVirtual attacker, AttackSource source)
{
if(hitted.mUnit.IsPet)
{
//手动推送吸收伤害消息
UnitHitEvent evt = new UnitHitEvent(hitted.mUnit.ID);
evt.senderId = attacker == null ? 0 : attacker.mUnit.ID;
evt.senderMasterId = hitted.GetMasterID();
evt.hitMasterId = attacker == null ? 0 : attacker.GetMasterID();
evt.client_state = (byte)XmdsCommonServer.Plugin.XmdsVirtual.UnitHitEventState.Absorb;
hitted.mUnit.queueEvent(evt);
}
}
}
}