OnNewZoneObject.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using CommonAI.ZoneClient;
  2. using ET.EventType;
  3. using FairyGUI;
  4. using Sirenix.Utilities;
  5. using UnityEngine;
  6. using XmdsCommon.Plugin;
  7. namespace ET.Client
  8. {
  9. [Event]
  10. [FriendOfAttribute(typeof(ET.Client.UnitRenderComponet))]
  11. public class OnNewZoneObjectHandler : BEvent<EventType.OnNewZoneObject>
  12. {
  13. protected override async ETTask OnEvent(EventType.OnNewZoneObject args)
  14. {
  15. var obj = UnitMgr.Instance.GetUnit(args.ObjectId);
  16. if (obj == null)
  17. {
  18. //还没显示就已挂掉的单位,走好
  19. Log.Debug($"ignore dead unit: {args.ObjectId}");
  20. return;
  21. }
  22. if (obj is BattleUnit)
  23. {
  24. CreatUnitModel(obj as BattleUnit, args.ModelName).Coroutine();
  25. }
  26. else if (obj is BattleSpell)
  27. {
  28. //TODO: 性能有问题时,可以减少法术展示
  29. CreateSpellModel(obj as BattleSpell).Coroutine();
  30. }
  31. else
  32. {
  33. Log.Error("unknow new object");
  34. }
  35. await ETTask.CompletedTask;
  36. }
  37. private static CommonLang.Geometry.Vector3 vecTemp = new();
  38. private async ETTask CreatUnitModel(BattleUnit unit, string modelName)
  39. {
  40. var zu = unit.ZUnit;
  41. if(modelName.IsNullOrWhitespace() )
  42. {
  43. modelName = $"Unit_{zu.Info.FileName}";
  44. }
  45. var go = await GameObjectPool.Instance.Acquire(modelName);
  46. go.SetActive(true);
  47. go.transform.parent = GlobalViewMgr.Instance.Unit;
  48. vecTemp.Set(zu.X, zu.Y, zu.Z);
  49. go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
  50. go.transform.rotation = RenderUtils.UnityRotationFromBattle(zu.Direction);
  51. var render = ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(unit.Id, go, true);
  52. if (zu.Info.Properties is XmdsUnitProperties prop && prop.ShowHPBanner)
  53. {
  54. await CreateHeadbar(render, zu);
  55. }
  56. if (unit is BattleActor)
  57. {
  58. CameraMgr.FollowMe(go.transform.position);
  59. }
  60. //Log.Debug($"unitRender({zu.ObjectID}),pos({zu.X},{zu.Y},{zu.Z}) ok.");
  61. switch(zu.CurrentState)
  62. {
  63. case CommonAI.Zone.Helper.UnitActionStatus.Move:
  64. EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(zu.ObjectID, PlayAnimatorEvent.AniType.Run));
  65. break;
  66. case CommonAI.Zone.Helper.UnitActionStatus.Dead:
  67. EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(zu.ObjectID, PlayAnimatorEvent.AniType.Dead));
  68. break;
  69. case CommonAI.Zone.Helper.UnitActionStatus.Stun:
  70. case CommonAI.Zone.Helper.UnitActionStatus.Damage:
  71. default:
  72. EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(zu.ObjectID, PlayAnimatorEvent.AniType.Idle));
  73. break;
  74. }
  75. if(zu.GetBuffStatusCount() > 0)
  76. {
  77. EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(zu.ObjectID, null, EventType.BuffChangeEvent.BuffOP.Reload));
  78. }
  79. }
  80. private async ETTask CreateSpellModel(BattleSpell spell)
  81. {
  82. var zs = spell.ZoneObject as ZoneSpell;
  83. var res = zs.Info.FileName;
  84. if (res.IsNullOrWhitespace())
  85. {
  86. Log.Debug($"spell({zs.Info.ID}) not config fileName");
  87. return;
  88. }
  89. if (!UnitMgr.Instance.HasUnit(zs.ObjectID) || (zs.Sender != null && !UnitMgr.Instance.HasUnit(zs.Sender.ObjectID)))
  90. {
  91. //还没显示就已挂掉的单位,走好
  92. Log.Debug($"ignore dead unit's spell: {zs.ObjectID}@{zs.Sender.ObjectID}");
  93. return;
  94. }
  95. GameObject go = await GameObjectPool.Instance.Acquire($"Effect_{res}");
  96. go.transform.parent = GlobalViewMgr.Instance.Unit;
  97. go.SetActive(true);
  98. go.transform.localScale = Vector3.one * zs.Info.FileBodyScale;
  99. vecTemp.Set(zs.X, zs.Y, zs.Z + zs.LaunchHeight);
  100. go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
  101. go.transform.rotation = RenderUtils.UnityRotationFromBattle(zs.Direction);
  102. ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(zs.ObjectID, go, true);
  103. var et = go.GetComponent<EffectTime>();
  104. if (et != null)
  105. {
  106. et.speed = zs.Info.EffectAddSpeed;
  107. //et.playAnimName = zs.Info.AnimtionName;
  108. //TODO:特效不支持播放动画
  109. }
  110. }
  111. private async ETTask CreateHeadbar(UnitRenderComponet render, ZoneUnit zu)
  112. {
  113. var view = await GameObjectPool.Instance.AcquireHeadBar();
  114. render.HeadBar = view;
  115. var name = view.GetChild("text_name");
  116. var progresshp = view.GetChild("bar_hp") as GProgressBar;
  117. progresshp.max = 100;
  118. progresshp.min = 0;
  119. render.NameBar = name;
  120. render.HPBar = progresshp;
  121. name.text = "";
  122. name.visible = false;
  123. progresshp.visible = false;
  124. progresshp.value = zu.HP * 100 / zu.MaxHP;
  125. render.SyncHeadBarPos();
  126. }
  127. }
  128. }