MessageHelper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace ET.Server
  5. {
  6. public static partial class MessageHelper
  7. {
  8. public static void NoticeUnitAdd(Unit unit, Unit sendUnit)
  9. {
  10. M2C_CreateUnits createUnits = new() { Units = new List<UnitInfo>() };
  11. createUnits.Units.Add(UnitHelper.CreateUnitInfo(sendUnit));
  12. MessageHelper.SendToClient(unit, createUnits);
  13. }
  14. public static void NoticeUnitRemove(Unit unit, Unit sendUnit)
  15. {
  16. M2C_RemoveUnits removeUnits = new() {Units = new List<long>()};
  17. removeUnits.Units.Add(sendUnit.Id);
  18. MessageHelper.SendToClient(unit, removeUnits);
  19. }
  20. public static void Broadcast(Unit unit, IActorMessage message)
  21. {
  22. Dictionary<long, AOIEntity> dict = unit.GetBeSeePlayers();
  23. // 网络底层做了优化,同一个消息不会多次序列化
  24. ActorLocationSenderOneType oneTypeLocationType = unit.Fiber().GetComponent<ActorLocationSenderComponent>().Get(LocationType.GateSession);
  25. foreach (AOIEntity u in dict.Values)
  26. {
  27. oneTypeLocationType.Send(u.Unit.Id, message);
  28. }
  29. }
  30. public static void SendToClient(Unit unit, IActorMessage message)
  31. {
  32. unit.Fiber().GetComponent<ActorLocationSenderComponent>().Get(LocationType.GateSession).Send(unit.Id, message);
  33. }
  34. /// <summary>
  35. /// 发送协议给Actor
  36. /// </summary>
  37. public static void SendActor(Fiber fiber, ActorId actorId, IActorMessage message)
  38. {
  39. fiber.GetComponent<ActorSenderComponent>().Send(actorId, message);
  40. }
  41. }
  42. }