This commit is contained in:
CortexCore
2025-03-03 18:44:00 +08:00
parent 561974a1ea
commit c29bf0d12f
84 changed files with 3986 additions and 1027 deletions

View File

@@ -8,12 +8,6 @@ namespace BITKit.WorldNode
/// </summary>
public interface IWorldNode
{
public int Id { get; set; }
public object WorldObject { get; set; }
public void Initialize()
{
}
}
}

View File

@@ -1,41 +0,0 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace BITKit.WorldNode
{
/// <summary>
/// 世界节点服务,所有动态世界节点通过此接口注册
/// </summary>
public interface IWorldNodeService
{
public IReadOnlyDictionary<int, HashSet<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, HashSet<IWorldNode>> IWorldNodeService.WorldNodes => WorldNodes;
private static readonly ConcurrentDictionary<int, HashSet<IWorldNode>> WorldNodes = new();
public void RegisterNode(IWorldNode node)
{
OnNodeRegistered?.Invoke(node);
WorldNodes.GetOrCreate(node.Id).Add(node);
}
event Action<IWorldNode> IWorldNodeService.OnNodeRegistered
{
add=>OnNodeRegistered+=value;
remove=>OnNodeRegistered-=value;
}
public void Dispose()
{
WorldNodes.Clear();
}
}
}

View File

@@ -2,37 +2,30 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.Entities;
using Microsoft.Extensions.DependencyInjection;
using UnityEngine;
namespace BITKit.WorldNode
{
public sealed class UnityNode : MonoBehaviour,IWorldNode
[RequireComponent(typeof(UnityEntity))]
public sealed class UnityNode : MonoBehaviour
{
[SerializeReference, SubclassSelector] private IWorldNodeService worldNodeService = new WorldNodeService();
[SerializeReference, SubclassSelector] private IWorldNode worldNode;
public int Id { get; set; }
public IWorldNode WorldNode => worldNode;
public object WorldObject
{
get => gameObject;
set=>throw new InvalidOperationException("Cannot set WorldObject");
}
private void Start()
{
if (worldNode is null)
if(worldNode is null)return;
var entity = GetComponent<IEntity>();
var type = worldNode.GetType();
GetComponent<IEntity>().ServiceCollection.AddSingleton(type,worldNode);
foreach (var interfaceType in type.GetInterfaces())
{
Debug.LogWarning("WorldNode is null");
return;
entity.ServiceCollection.AddSingleton(interfaceType, worldNode);
}
Id = gameObject.GetInstanceID();
worldNode.Id = Id;
worldNode.WorldObject = gameObject;
worldNode.Initialize();
worldNodeService.RegisterNode(worldNode);
Destroy(this);
}
}
}

View File

@@ -0,0 +1,33 @@
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 class WorldInfoNode : IWorldNode
{
#if UNITY_5_3_OR_NEWER
[SerializeReference, SubclassSelector]
private IReference name;
[SerializeReference, SubclassSelector]
private IReference description;
public string Name
{
get => name?.Value;
set => name = new Reference(value);
}
public string Description
{
get => description?.Value;
set=>description = new Reference(value);
}
#endif
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: ca82e09109a03de47b638f35b49b59e5
guid: 5e1cbe087263f1342abbc3c6fb5c4068
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,60 +0,0 @@
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;
#if UNITY_5_3_OR_NEWER
public void Initialize()
{
Name = name?.Value;
Description = description?.Value;
}
#endif
public int Id { get; set; }
public object WorldObject { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public sealed class WorldInfoNodeService : IDisposable
{
public IReadOnlyDictionary<int, WorldInfoNode> WorldInfoNodes => InfoNodes;
private readonly IWorldNodeService _worldNodeService;
private static 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();
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 132a27b99db1e664692937ec23f40676
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -5,13 +5,8 @@ using System.Collections.Generic;
namespace BITKit.WorldNode
{
[Serializable]
public struct WorldInfoNpcStartNode:IWorldNode
public class WorldInfoNpcStartNode:IWorldNode
{
public string Address;
public int Id { get; set; }
public object WorldObject { get; set; }
public void Initialize()
{
}
}
}

View File

@@ -5,14 +5,8 @@ using System.Collections.Generic;
namespace BITKit.WorldNode
{
[Serializable]
public struct WorldInfoPlayerStart:IWorldNode
public class WorldInfoPlayerStart:IWorldNode
{
public static WorldInfoPlayerStart Current { get; set; }
public int Id { get; set; }
public object WorldObject { get; set; }
public void Initialize()
{
Current = this;
}
}
}

View File

@@ -19,8 +19,6 @@ namespace BITKit.WorldNode
public IReference MapName;
public float3 Position;
public float3 EulerAngle;
public int Id { get; set; }
public object WorldObject { get; set; }
}
}