OnNewZoneObject.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using CommonAI.ZoneClient;
  2. using Sirenix.Utilities;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace ET.Client
  6. {
  7. [Event(SceneType.Current)]
  8. public class OnNewZoneObjectHandler : AEvent<EventType.OnNewZoneObject>
  9. {
  10. protected override async ETTask Run(Scene scene, EventType.OnNewZoneObject args)
  11. {
  12. var obj = UnitMgr.Instance.GetUnit(args.ObjectId);
  13. if(obj is BattleUnit)
  14. {
  15. await CreatUnitModel(obj as BattleUnit);
  16. }
  17. else if(obj is BattleSpell)
  18. {
  19. //TODO: 性能有问题时,可以减少法术展示
  20. await CreateSpellModel(obj as BattleSpell);
  21. }
  22. else
  23. {
  24. Log.Error("unknow new object");
  25. }
  26. }
  27. private static CommonLang.Geometry.Vector3 vecTemp = new();
  28. private async ETTask CreatUnitModel(BattleUnit unit)
  29. {
  30. var zu = unit.ZUnit;
  31. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>($"Unit_{zu.Info.FileName}");
  32. if(!UnitMgr.Instance.HasUnit(zu.ObjectID))
  33. {
  34. //还没显示就已挂掉的单位,走好
  35. Log.Debug($"ignore dead unit: {zu}@{zu.ObjectID}");
  36. return;
  37. }
  38. var prefab = handle.GetAssetObject<GameObject>();
  39. GameObject go = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.Unit, true);
  40. vecTemp.Set(zu.X, zu.Y, zu.Z);
  41. go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
  42. go.transform.rotation = RenderUtils.UnityRotationFromBattle(zu.Direction);
  43. ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(unit.Id, go, true);
  44. if (unit is BattleActor)
  45. {
  46. CameraMgr.FollowMe(go.transform.position);
  47. }
  48. Log.Debug($"unitRender({zu.ObjectID}),pos({zu.X},{zu.Y},{zu.Z}) ok.");
  49. //TODO: 同步ZoneUnit status
  50. }
  51. private async ETTask CreateSpellModel(BattleSpell spell)
  52. {
  53. var zs = spell.ZoneObject as ZoneSpell;
  54. //spell effect hack
  55. var res = zs.Info.FileName;
  56. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>($"Effect_{res}");
  57. if (!UnitMgr.Instance.HasUnit(zs.ObjectID) || (zs.Sender != null && !UnitMgr.Instance.HasUnit(zs.Sender.ObjectID)))
  58. {
  59. //还没显示就已挂掉的单位,走好
  60. Log.Debug($"ignore dead unit's spell: {zs.ObjectID}@{zs.Sender.ObjectID}");
  61. return;
  62. }
  63. var prefab = handle.GetAssetObject<GameObject>();
  64. GameObject go = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.Unit, true);
  65. vecTemp.Set(zs.X, zs.Y, zs.Z);
  66. go.name = "spell_" + zs.ObjectID;
  67. go.transform.localScale = Vector3.one * zs.Info.FileBodyScale;
  68. go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
  69. go.transform.rotation = RenderUtils.UnityRotationFromBattle(zs.Direction);
  70. ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(zs.ObjectID, go, true);
  71. Log.Debug($"spellRender({zs.ObjectID}) ok.");
  72. }
  73. }
  74. }