42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Concurrent;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
namespace BITKit.WorldNode
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 世界节点服务,所有动态世界节点通过此接口注册
|
||
|
/// </summary>
|
||
|
public interface IWorldNodeService
|
||
|
{
|
||
|
public IReadOnlyDictionary<int, IWorldNode> WorldNodes { get; }
|
||
|
public void RegisterNode(IWorldNode node);
|
||
|
public event Action<IWorldNode> OnNodeRegistered;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 世界节点默认实现
|
||
|
/// </summary>
|
||
|
[Serializable]
|
||
|
public class WorldNodeService : IWorldNodeService,IDisposable
|
||
|
{
|
||
|
public static event Action<IWorldNode> OnNodeRegistered;
|
||
|
IReadOnlyDictionary<int, IWorldNode> IWorldNodeService.WorldNodes => WorldNodes;
|
||
|
private static readonly ConcurrentDictionary<int, IWorldNode> WorldNodes = new();
|
||
|
public void RegisterNode(IWorldNode node)
|
||
|
{
|
||
|
OnNodeRegistered?.Invoke(node);
|
||
|
WorldNodes.TryAdd(node.Id, node);
|
||
|
}
|
||
|
event Action<IWorldNode> IWorldNodeService.OnNodeRegistered
|
||
|
{
|
||
|
add=>OnNodeRegistered+=value;
|
||
|
remove=>OnNodeRegistered-=value;
|
||
|
}
|
||
|
public void Dispose()
|
||
|
{
|
||
|
WorldNodes.Clear();
|
||
|
}
|
||
|
}
|
||
|
}
|