123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using CommonAI.Zone;
- using CommonAI.Zone.Helper;
- using CommonAI.ZoneClient;
- using ET;
- using ET.EventType;
- using Sirenix.Utilities;
- using System;
- using XmdsCommon.Plugin;
- public class BattleUnit : BattleObject
- {
- public BattleUnit() { }
- public ZoneUnit ZUnit { get { return ZoneObject as ZoneUnit; } }
- public bool IsDead { get { return ZUnit.CurrentState == UnitActionStatus.Dead; } }
- private EventDispatcher<UnitActionStatus, object> actionChangeHandler;
- public override void OnAwake(ZoneObject zo)
- {
- base.OnAwake(zo);
- actionChangeHandler = new();
- registerActionHandler();
- var zu = zo as ZoneUnit;
- zu.OnActionChanged += OnActionChanged;
- zu.OnSkillActionChanged += (ZoneUnit unit, ZoneUnit.SkillState skill, byte index) => { };
- zu.OnHPChanged += (ZoneUnit unit, int oldHP, int newHP)=> { OnHPChanged(); };
- zu.OnMPChanged += (ZoneUnit unit, int oldMaxMP, int newMaxMP) => { };
- zu.OnMaxHPChanged += (ZoneUnit unit, int oldHP, int newHP) => { OnHPChanged(); };
- zu.OnMaxMPChanged += (ZoneUnit unit, int oldMaxMP, int newMaxMP)=> { };
- zu.OnLaunchSkill += OnLaunchSkill;
- zu.OnBuffAdded += (ZoneUnit unit, ZoneUnit.BuffState buff) => {
- EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(Id, buff, BuffChangeType.Add));
- };
- zu.OnBuffRemoved += (ZoneUnit unit, ZoneUnit.BuffState buff) => {
- EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(Id, buff, BuffChangeType.Remove));
- };
- zu.OnBuffChanged += (ZoneUnit unit, ZoneUnit.BuffState buff) => {
- EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(Id, buff, BuffChangeType.Change));
- };
- zu.OnRemoveBuffByOverlayLevel += (ZoneUnit unit, ZoneUnit.BuffState buff) => { };
- zu.OnActionSubStatusChanged += OnActionSubStatusChanged;
- }
- private static uint groupId = 1;
- protected static uint GroupId()
- {
- return ++ groupId;
- }
- protected virtual void OnLaunchSkill(ZoneUnit _, ZoneUnit.SkillState __, UnitLaunchSkillEvent ___)
- {
- CommonAI.ZoneClient.ZoneUnit.ISkillAction action = ZUnit.CurrentSkillAction;
- if(action == null)
- {
- Log.Warning("object's currentSkillAction = null, cannot launch skill");
- return;
- }
- SkillTemplate skillTemplate = action.SkillData;
- var curActionIndex = action.CurrentActionIndex;
- if (skillTemplate.ActionQueue.Count <= curActionIndex)
- {
- Log.Error("Enter Skill state error> ({0})>CurrentActionIndex>{1}", skillTemplate.TemplateID, curActionIndex);
- return;
- }
- //Log.Debug($"OnLaunchSkill({Id}), skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
-
- if (skillTemplate.IsSingleAction)
- {
- UnitActionData actdata = skillTemplate.ActionQueue[curActionIndex];
- if (actdata.ActionName.IsNullOrWhitespace())
- {
- Log.Debug($"ActionName is null @skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
- return;
- }
- var duration = action.ActionTimeArray != null ? action.ActionTimeArray[0] : actdata.TotalTimeMS;
- EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(
- Id,
- AnimatorEventType.Skill,
- actdata.ActionName,
- duration,
- action.ActionSpeed,
- 0,
- actdata.IsCycAction, actdata.ActionAudioName)
- );
- }
- else
- {
- float speed = 1.0f;
- bool isFrist = true;
- for (int i = curActionIndex; i < skillTemplate.ActionQueue.Count; i++)
- {
- UnitActionData item = skillTemplate.ActionQueue[i];
- if (item.ActionName.IsNullOrWhitespace())
- {
- Log.Debug($"ActionName is null @skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
- return;
- }
- if (action.LaunchEvent != null)
- {
- speed = action.LaunchEvent.action_speed;
- if (action.LaunchEvent.action_speed_add != null && i < action.LaunchEvent.action_speed_add.Length)
- {
- speed += action.LaunchEvent.action_speed_add[i];
- }
- }
- EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(
- Id,
- AnimatorEventType.Skill,
- item.ActionName,
- action.ActionTimeArray[i],
- speed, GroupId(),
- item.IsCycAction,
- isFrist ? item.ActionAudioName : ""));
- if(!item.ActionAudioName.IsNullOrWhitespace())
- {
- if (isFrist)
- {
- isFrist = false;
- }
- else
- {
- Log.Error("not implements multiAction audio");
- }
- }
- }
- }
- }
- private void OnHPChanged()
- {
- if (ZUnit.Info.Properties is XmdsUnitProperties prop)
- {
- if (prop.GameStatusType == XmdsUnitProperties.StatusType.SpecialElite)
- {
- var hp = ZUnit.HP;
- var pg = (float)ZUnit.HP * 100 / ZUnit.MaxHP;
- EventSystem.Instance.Publish<HPRefresh>(HPRefresh.Static.Clone(HPRefresh.Index.Tower, pg, hp));
- //Log.Debug($"tower({ZUnit.ObjectID}@{ZUnit.Info.Name}) hp: {ZUnit.HP}/{ZUnit.MaxHP}");
- }
- else if (prop.GameStatusType == XmdsUnitProperties.StatusType.SpecialBoss)
- {
- var hp = ZUnit.HP;
- var pg = (float)ZUnit.HP * 100 / ZUnit.MaxHP;
- EventSystem.Instance.Publish<HPRefresh>(HPRefresh.Static.Clone(HPRefresh.Index.Boss, pg, hp));
- //Log.Debug($"Boss({ZUnit.ObjectID}) hp: {ZUnit.HP}/{ZUnit.MaxHP}");
- }
- }
- //Log.Debug($"hp({ZUnit.ObjectID}) change: {ZUnit.HP}");
- }
- protected virtual void OnActionChanged(ZoneUnit unit, UnitActionStatus status, object evt)
- {
- //Log.Debug($"ActionChange({unit.Info.TemplateID}): {status}<= zobject status:{ZUnit.CurrentState}");
- if(!actionChangeHandler.Notify(status, evt))
- {
- Log.Error($"unhandle action changed: {status}");
- }
- }
- protected virtual void OnActionSubStatusChanged(ZoneUnit unit, byte status, object evt)
- {
- //Log.Debug($"OnActionSubStatusChanged({unit.Info.TemplateID}): {status}<= zobject status:{ZUnit.CurrentSubState}");
- }
- private void registerActionHandler()
- {
- actionChangeHandler.AddListener(UnitActionStatus.Idle, (_) => {
- EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(Id, AnimatorEventType.Idle));
- });
- var move = actionChangeHandler.AddListener(UnitActionStatus.Move, (o) => {
- EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(Id, AnimatorEventType.Run));
- });
- actionChangeHandler.AddListener(UnitActionStatus.Chaos, move);
- actionChangeHandler.AddListener(UnitActionStatus.ServerSyncMove, move);
- actionChangeHandler.AddListener(UnitActionStatus.Escape, move);
- var stun = actionChangeHandler.AddListener(UnitActionStatus.Stun, (o) => {
- //TODO: 判断没有FROZEN的BUFF状态,才进行stun的动作
- });
- actionChangeHandler.AddListener(UnitActionStatus.HitMove, stun);
- actionChangeHandler.AddListener(UnitActionStatus.Dead, (o) => {
- OnHPChanged();
- });
- actionChangeHandler.AddListener(UnitActionStatus.Damage, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.Pick, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.Spawn, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkMove, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkIdle, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.DaZuo, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.DaZuoRecoveryAttr, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.ChuanGongA, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.ChuanGongB, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.BuffLockStateAction, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.HomeBack, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.ClearYaoQi, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.PetLock, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.BreakShield, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.Transport, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.Skill, (o) => { });
- actionChangeHandler.AddListener(UnitActionStatus.Rebirth, (o) => { });
- }
- }
|