TH1/Unity/Assets/Scripts/Hotfix/Server/Demo/Map/MapMessageHelper.cs
2025-07-17 18:26:28 +08:00

49 lines
1.8 KiB
C#

using System.Collections.Generic;
using System.IO;
namespace ET.Server
{
public static partial class MapMessageHelper
{
public static void NoticeUnitAdd(Unit unit, Unit sendUnit)
{
M2C_CreateUnits createUnits = M2C_CreateUnits.Create();
createUnits.Units.Add(UnitHelper.CreateUnitInfo(sendUnit));
MapMessageHelper.SendToClient(unit, createUnits);
}
public static void NoticeUnitRemove(Unit unit, Unit sendUnit)
{
M2C_RemoveUnits removeUnits = M2C_RemoveUnits.Create();
removeUnits.Units.Add(sendUnit.Id);
MapMessageHelper.SendToClient(unit, removeUnits);
}
public static void Broadcast(Unit unit, IMessage message)
{
(message as MessageObject).IsFromPool = false;
Dictionary<long, EntityRef<AOIEntity>> dict = unit.GetBeSeePlayers();
// 网络底层做了优化,同一个消息不会多次序列化
MessageLocationSenderOneType oneTypeMessageLocationType = unit.Root().GetComponent<MessageLocationSenderComponent>().Get(LocationType.GateSession);
foreach (AOIEntity u in dict.Values)
{
oneTypeMessageLocationType.Send(u.Unit.Id, message);
}
}
public static void SendToClient(Unit unit, IMessage message)
{
unit.Root().GetComponent<MessageLocationSenderComponent>().Get(LocationType.GateSession).Send(unit.Id, message);
}
/// <summary>
/// 发送协议给Actor
/// </summary>
public static void Send(Scene root, ActorId actorId, IMessage message)
{
root.GetComponent<MessageSender>().Send(actorId, message);
}
}
}