This commit is contained in:
CortexCore
2024-11-03 16:38:17 +08:00
parent 056e2cada5
commit 4ba741408d
4693 changed files with 2445 additions and 5443 deletions

View File

@@ -7,7 +7,8 @@
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:1193c2664d97cc049a6e4c486c6bce71"
"GUID:1193c2664d97cc049a6e4c486c6bce71",
"GUID:d750d221812bb1d48baff92e6ef73e28"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: dad0d8732f120a848a84b5b41d23c141
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,23 +0,0 @@
{
"name": "BITKit.WorldNode.Editor",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:508392158bd966c4d9c21e19661a441d",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:01cf845b40a364e4cbaee058936fa4a3"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"UNITY_EDITOR"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 9f93c330bf016d047a49b0c29307ef42
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,96 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.WorldNodes
{
public class WorldNodeServiceEditor : EditorWindow
{
[MenuItem("Tools/Scenes/WorldNode Window")]
public static void Open()
{
GetWindow<WorldNodeServiceEditor>().Show();
}
private List<ValueTuple<IWorldNode,IWorldNode>> _connections = new();
private List<ValueTuple<IWorldNode, IWorldNode>> _blockConnections = new();
private Label reportLabel;
private Toggle drawBlocked;
private void OnEnable()
{
var checkButton = rootVisualElement.Create<Button>();
checkButton.text = "Check";
checkButton.clicked += Rebuild;
drawBlocked = rootVisualElement.Create<Toggle>();
reportLabel = rootVisualElement.Create<Label>();
drawBlocked.label = "Draw Blocked Connections";
WorldNodeService.OnDrawGizmo += DrawGizmo;
}
private void OnDisable()
{
WorldNodeService.OnDrawGizmo -= DrawGizmo;
}
private void Rebuild()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
_connections.Clear();
_blockConnections.Clear();
//这里会在WorldNodeService中获取节点的数组,然后获取所有两个节点之间的组合
var nodes = WorldNodeService.WorldNodes;
foreach (var t in nodes)
{
foreach (var t1 in nodes)
{
if(t1==t)continue;
if(Physics.Linecast(t.Position, t1.Position, out var hit))
{
_blockConnections.Add((t, t1));
}
else
{
_connections.Add((t, t1));
}
}
}
watch.Stop();
reportLabel.text = $"Connections: {_connections.Count}";
reportLabel.text += $"\nBlockConnections: {_blockConnections.Count}";
reportLabel.text += $"\nTime: {watch.ElapsedMilliseconds}ms\tcompleted at {DateTime.Now:HH:mm:ss}";
}
private void DrawGizmo()
{
foreach (var x in WorldNodeService.WorldNodes)
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(x.Position, 0.1f);
}
foreach (var (x, y) in _connections)
{
Gizmos.color = Color.green;
Gizmos.DrawLine(x.Position, y.Position);
}
if (drawBlocked.value)
{
foreach (var (x, y) in _blockConnections)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(x.Position, y.Position);
}
}
}
}
}

View File

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

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit.WorldNode
{
public sealed class UnityNode : MonoBehaviour,IWorldNode
{
[SerializeReference, SubclassSelector] private IWorldNodeService worldNodeService = new WorldNodeService();
[SerializeReference, SubclassSelector] private IWorldNode worldNode;
public int Id { get; set; }
public object WorldObject
{
get => gameObject;
set=>throw new InvalidOperationException("Cannot set WorldObject");
}
private void Start()
{
Id = gameObject.GetInstanceID();
worldNode.Id = Id;
worldNode.WorldObject = gameObject;
worldNodeService.RegisterNode(worldNode);
worldNode.Initialize();
Destroy(this);
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1edc46d4a7d336f41bc9f8c4a1bdecc5
guid: 054d7fc7589c04d4a8412a6f82b32d6c
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,62 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace BITKit.WorldNodes
{
public interface IWorldNode
{
Vector3 Position { get; }
}
[ExecuteAlways]
public class WorldNode : MonoBehaviour,IWorldNode
{
[SerializeField, ReadOnly] private string report;
[SerializeField,ReadOnly] private bool isRegistered;
private void OnEnable()
{
var reportBuilder = new StringBuilder();
reportBuilder.AppendLine(nameof(OnEnable));
var instanceId = GetInstanceID();
reportBuilder.AppendLine($"InstanceId{instanceId}");
switch (instanceId)
{
case <0:
isRegistered = true;
WorldNodeService.WorldNodeList.Add(this);
reportBuilder.AppendLine(report);
break;
case >0:
reportBuilder.AppendLine($"WorldNode {name} will not registered");
break;
}
report = reportBuilder.ToString();
}
private void OnDisable()
{
var reportBuilder = new StringBuilder();
reportBuilder.AppendLine(nameof(OnDisable));
if (isRegistered)
{
WorldNodeService.WorldNodeList.Remove(this);
reportBuilder.AppendLine( $"WorldNode {name} unregistered");
isRegistered = false;
}
else
{
reportBuilder.AppendLine($"WorldNode {name} is not registered");
}
report = reportBuilder.ToString();
}
public Vector3 Position => transform.position;
}
}

View File

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

View File

@@ -1,100 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Quadtree;
using Quadtree.Items;
using UnityEngine;
namespace BITKit.WorldNodes
{
public interface IWorldNodeService
{
IWorldNode[] WorldNodes { get; }
IWorldNode[] GetNodes(Vector3 position, float radius);
IWorldNode[] GetConnectedNodes(IWorldNode node);
}
public abstract class WorldNodeServiceImplementation : IWorldNodeService
{
protected virtual IWorldNodeService Service { get; }
public IWorldNode[] WorldNodes => Service.WorldNodes;
public IWorldNode[] GetNodes(Vector3 position, float radius)
{
return Service.GetNodes(position, radius);
}
public IWorldNode[] GetConnectedNodes(IWorldNode node)
{
return Service.GetConnectedNodes(node);
}
}
[ExecuteInEditMode]
public class WorldNodeService : MonoBehaviour,IWorldNodeService
{
private class WorldNodeProxy : IItem<WorldNodeProxy, Node<WorldNodeProxy>>
{
public Bounds Bounds;
public Bounds GetBounds()=>Bounds;
public Node<WorldNodeProxy> ParentNode { get; set; }
public void QuadTree_Root_Initialized(IQuadtreeRoot<WorldNodeProxy, Node<WorldNodeProxy>> root)
{
}
}
public static event Action OnDrawGizmo;
public static readonly CacheList<IWorldNode> WorldNodeList = new();
public static IWorldNode[] WorldNodes => WorldNodeList.ValueArray;
private static QuadtreeRoot<WorldNodeProxy, Node<WorldNodeProxy>> _QuadtreeRoot;
public static IWorldNode[] GetNodes(Vector3 position, float radius)
{
return WorldNodes.Where(node => InRange(position, node, radius))
.Where(node => InSight(position, node))
.ToArray();
}
public static IWorldNode[] GetConnectedNodes(IWorldNode node)
{
throw new System.NotImplementedException();
}
private static bool InRange(Vector3 position, IWorldNode node, float radius)
{
return Vector3.Distance(position, node.Position) <= radius;
}
private static bool InSight(Vector3 position, IWorldNode node)
{
if (Physics.Linecast(position, node.Position, out var hit))
{
return hit.transform == node.As<MonoBehaviour>().transform;
}
return true;
}
IWorldNode[] IWorldNodeService.WorldNodes => WorldNodes;
IWorldNode[] IWorldNodeService.GetNodes(Vector3 position, float radius)
{
return GetNodes(position, radius);
}
IWorldNode[] IWorldNodeService.GetConnectedNodes(IWorldNode node)
{
return GetConnectedNodes(node);
}
private void OnDrawGizmos()
{
OnDrawGizmo?.Invoke();
}
private void Reset()
{
WorldNodeList.Clear();
}
[SerializeField] private Vector3 size;
private void Start()
{
_QuadtreeRoot = new QuadtreeRoot<WorldNodeProxy, Node<WorldNodeProxy>>(transform.position,size);
}
}
}