UnitHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace ET
  4. {
  5. public static class UnitHelper
  6. {
  7. public static UnitInfo CreateUnitInfo(Unit unit)
  8. {
  9. UnitInfo unitInfo = new UnitInfo();
  10. NumericComponent nc = unit.GetComponent<NumericComponent>();
  11. unitInfo.UnitId = unit.Id;
  12. unitInfo.ConfigId = unit.ConfigId;
  13. unitInfo.Type = (int)unit.Type;
  14. Vector3 position = unit.Position;
  15. unitInfo.X = position.x;
  16. unitInfo.Y = position.y;
  17. unitInfo.Z = position.z;
  18. Vector3 forward = unit.Forward;
  19. unitInfo.ForwardX = forward.x;
  20. unitInfo.ForwardY = forward.y;
  21. unitInfo.ForwardZ = forward.z;
  22. MoveComponent moveComponent = unit.GetComponent<MoveComponent>();
  23. if (moveComponent != null)
  24. {
  25. if (!moveComponent.IsArrived())
  26. {
  27. unitInfo.MoveInfo = new MoveInfo();
  28. for (int i = moveComponent.N; i < moveComponent.Targets.Count; ++i)
  29. {
  30. Vector3 pos = moveComponent.Targets[i];
  31. unitInfo.MoveInfo.X.Add(pos.x);
  32. unitInfo.MoveInfo.Y.Add(pos.y);
  33. unitInfo.MoveInfo.Z.Add(pos.z);
  34. }
  35. }
  36. }
  37. foreach ((int key, long value) in nc.NumericDic)
  38. {
  39. unitInfo.Ks.Add(key);
  40. unitInfo.Vs.Add(value);
  41. }
  42. return unitInfo;
  43. }
  44. // 获取看见unit的玩家,主要用于广播
  45. public static Dictionary<long, AOIEntity> GetBeSeePlayers(this Unit self)
  46. {
  47. return self.GetComponent<AOIEntity>().GetBeSeePlayers();
  48. }
  49. public static void NoticeUnitAdd(Unit unit, Unit sendUnit)
  50. {
  51. M2C_CreateUnits createUnits = new M2C_CreateUnits();
  52. createUnits.Units.Add(CreateUnitInfo(sendUnit));
  53. MessageHelper.SendToClient(unit, createUnits);
  54. }
  55. public static void NoticeUnitRemove(Unit unit, Unit sendUnit)
  56. {
  57. M2C_RemoveUnits removeUnits = new M2C_RemoveUnits();
  58. removeUnits.Units.Add(sendUnit.Id);
  59. MessageHelper.SendToClient(unit, removeUnits);
  60. }
  61. }
  62. }