UnitHelper.cs 2.4 KB

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