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