BITKit/Src/Core/WorldNode/WorldInfoNodeService.cs

54 lines
1.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
#if UNITY_5_3_OR_NEWER
using UnityEngine;
#endif
namespace BITKit.WorldNode
{
[Serializable]
public struct WorldInfoNode : IWorldNode
{
// ReSharper disable once InconsistentNaming
#if UNITY_5_3_OR_NEWER
[SerializeReference,SubclassSelector]
#endif
private IReference name;
// ReSharper disable once InconsistentNaming
#if UNITY_5_3_OR_NEWER
[SerializeReference,SubclassSelector]
#endif
private IReference description;
public int Id { get; set; }
public object WorldObject { get; set; }
public string Name => name?.Value;
public string Description => description?.Value;
}
public sealed class WorldInfoNodeService : IDisposable
{
public IReadOnlyDictionary<int, WorldInfoNode> WorldInfoNodes => _infoNodes;
private readonly IWorldNodeService _worldNodeService;
private readonly ConcurrentDictionary<int, WorldInfoNode> _infoNodes = new();
public WorldInfoNodeService(IWorldNodeService worldNodeService)
{
_worldNodeService = worldNodeService;
_worldNodeService.OnNodeRegistered += OnNodeRegistered;
}
private void OnNodeRegistered(IWorldNode obj)
{
if (obj is not WorldInfoNode infoNode) return;
_infoNodes.TryAdd(obj.Id, infoNode);
}
public void Dispose()
{
_infoNodes.Clear();
}
}
}