83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Windows.Forms;
|
||
|
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
|
||
|
{
|
||
|
public static event Action OnDrawGizmo;
|
||
|
public static readonly CacheList<IWorldNode> WorldNodeList = new();
|
||
|
public static IWorldNode[] WorldNodes => WorldNodeList.ValueArray;
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|