OnNewZoneObject.cs 4.4 KB

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