提交示例代码

This commit is contained in:
Bob.Song
2026-03-05 11:39:06 +08:00
commit 25958f58c3
2534 changed files with 209593 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8601 // Possible null reference assignment.
namespace Fantasy;
public sealed class ChatChannelCenterComponentDestroySystem : DestroySystem<ChatChannelCenterComponent>
{
protected override void Destroy(ChatChannelCenterComponent self)
{
foreach (var chatChannelComponent in self.Channels.Values.ToArray())
{
chatChannelComponent.Dispose();
}
self.Channels.Clear();
}
}
public static class ChatChannelCenterComponentSystem
{
public static ChatChannelComponent Apply(this ChatChannelCenterComponent self, long channelId)
{
if (self.Channels.TryGetValue(channelId, out var channel))
{
return channel;
}
channel = Entity.Create<ChatChannelComponent>(self.Scene, channelId, true, true);
self.Channels.Add(channelId, channel);
return channel;
}
public static bool TryGet(this ChatChannelCenterComponent self, long channelId, out ChatChannelComponent channel)
{
return self.Channels.TryGetValue(channelId, out channel);
}
public static void Disband(this ChatChannelCenterComponent self, long channelId)
{
if (self.Channels.Remove(channelId, out var channel))
{
return;
}
channel.Dispose();
}
}

View File

@@ -0,0 +1,86 @@
using Fantasy.Entitas;
namespace Fantasy;
public class MemoryEntity : Entity
{
}
public static class ChatChannelComponentSystem
{
public static void Send(this ChatChannelComponent self, ChatInfoTree tree)
{
var chatUnitManageComponent = self.Scene.GetComponent<ChatUnitManageComponent>();
var networkMessagingComponent = self.Scene.NetworkMessagingComponent;
var chatMessage = new Chat2C_Message()
{
ChatInfoTree = tree
};
foreach (var unitId in self.Units)
{
if (!chatUnitManageComponent.Units.TryGetValue(unitId, out var chatUnit))
{
continue;
}
networkMessagingComponent.SendInnerRoute(chatUnit.GateRouteId, chatMessage);
}
}
public static bool JoinChannel(this ChatChannelComponent self, long chatUnitId)
{
var chatUnitManageComponent = self.Scene.GetComponent<ChatUnitManageComponent>();
if (!chatUnitManageComponent.TryGet(chatUnitId, out var chatUnit))
{
return false;
}
// 将当前频道中加入该用户。
self.Units.Add(chatUnitId);
// 给用户添加频道。
if (!chatUnit.Channels.ContainsKey(self.Id))
{
chatUnit.Channels.Add(self.Id, self);
}
// 可以在这里给客户端发送一个加入频道成功的消息。
return true;
}
public static bool IsJoinedChannel(this ChatChannelComponent self, long chatUnitId)
{
return self.Units.Contains(chatUnitId);
}
public static void ExitChannel(this ChatChannelComponent self, long chatUnitId, bool isRemoveUnitChannel = true)
{
if (!self.Units.Contains(chatUnitId))
{
return;
}
var chatUnitManageComponent = self.Scene.GetComponent<ChatUnitManageComponent>();
if (!chatUnitManageComponent.TryGet(chatUnitId, out var chatUnit))
{
return;
}
if (isRemoveUnitChannel)
{
// 给用户移除频道。
chatUnit.Channels.Remove(self.Id);
}
// 在当前频道中移除该用户。
self.Units.Remove(chatUnitId);
// 如果当前频道中没有用户了,则销毁该频道。
if (self.Units.Count == 0)
{
self.Dispose();
}
}
}

View File

@@ -0,0 +1,67 @@
using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
#pragma warning disable CS8601 // Possible null reference assignment.
namespace Fantasy;
public sealed class ChatUnitManageComponentDestroySystem : DestroySystem<ChatUnitManageComponent>
{
protected override void Destroy(ChatUnitManageComponent self)
{
foreach (var chatUnit in self.Units.Values.ToArray())
{
chatUnit.Dispose();
}
self.Units.Clear();
}
}
public static class ChatUnitManageComponentSystem
{
public static ChatUnit Add(this ChatUnitManageComponent self, long unitId, string userName, long gateRouteId)
{
if (!self.Units.TryGetValue(unitId, out var chatUnit))
{
chatUnit = Entity.Create<ChatUnit>(self.Scene, unitId, true, true);
self.Units.Add(unitId, chatUnit);
Log.Debug($"Add ChatUnit Count: {self.Units.Count} UnitId: {unitId} UserName: {userName} GateRouteId: {gateRouteId}");
}
else
{
Log.Debug($"ChatUnit: {chatUnit.UserName}({chatUnit.GateRouteId})");
}
chatUnit.UserName = userName;
chatUnit.GateRouteId = gateRouteId;
return chatUnit;
}
public static ChatUnit? Get(this ChatUnitManageComponent self, long unitId)
{
return self.Units.GetValueOrDefault(unitId);
}
public static bool TryGet(this ChatUnitManageComponent self, long unitId, out ChatUnit chatUnit)
{
return self.Units.TryGetValue(unitId, out chatUnit);
}
public static void Remove(this ChatUnitManageComponent self, long unitId, bool isDispose = true)
{
// 由于退出频道的时候也会检查该玩家是否在ChatUnitManageComponent中所以这里不做移除操作。
if (!self.Units.TryGetValue(unitId, out var chatUnit))
{
return;
}
if (isDispose)
{
chatUnit.Dispose();
}
// 因为玩家已经执行了退出频道的操作了,所以要清除一下这个数据。
self.Units.Remove(unitId);
Log.Debug($"Remove ChatUnit: {chatUnit.UserName}({chatUnit.GateRouteId}) Count: {self.Units.Count}");
}
}

View File

@@ -0,0 +1,21 @@
using Fantasy.Entitas.Interface;
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
namespace Fantasy;
public sealed class ChatUnitDestroySystem : DestroySystem<ChatUnit>
{
protected override void Destroy(ChatUnit self)
{
self.UserName = null;
self.GateRouteId = 0;
// 退出当前ChatUnit拥有的所有频道
foreach (var (_,chatChannelComponent) in self.Channels)
{
chatChannelComponent.ExitChannel(self.Id, false);
}
// 理论情况下这个self.Channels不会存在因为数据的因为上面已经给清空掉了。
// 但是self.Channels.Clear();还是加上吧,防止以后忘记了。
self.Channels.Clear();
}
}