Files
Studio/Assets/Scripts/Examples/AutoFactory/AGVMap.cs

120 lines
3.4 KiB
C#
Raw Normal View History

2025-03-24 20:03:17 +09:00
using Newtonsoft.Json;
2025-03-20 12:23:33 +09:00
using System;
2025-03-20 10:33:24 +09:00
using System.Collections.Generic;
2025-03-20 12:23:33 +09:00
using UnityEditor;
2025-03-20 10:33:24 +09:00
using UnityEngine;
using XRLib;
2025-05-20 16:25:58 +09:00
namespace Studio.VirtualFactory
2025-03-20 10:33:24 +09:00
{
public class AGVMap : MonoBehaviour, ISingle
{
public List<AGVNode> nodes = new();
2025-03-20 12:23:33 +09:00
#if UNITY_EDITOR
public void OnDrawGizmos()
{
HashSet<(Vector3, Vector3)> drawnLines = new HashSet<(Vector3, Vector3)>();
foreach (var n in nodes)
{
if (n == null)
continue;
2025-03-24 20:03:17 +09:00
Handles.Label(n.transform.position + Vector3.up * 0.5f, n.name);
2025-03-20 12:23:33 +09:00
foreach (var l in n.linkedNodes)
{
Vector3 start = n.transform.position;
if (l == null)
continue;
Vector3 end = l.transform.position;
// Ensure the line is always stored in a consistent order
var line = start.x < end.x || (start.x == end.x && start.y < end.y) || (start.x == end.x && start.y == end.y && start.z < end.z)
? (start, end)
: (end, start);
if (!drawnLines.Contains(line))
{
Gizmos.DrawLine(start, end);
drawnLines.Add(line);
}
}
}
}
2025-03-24 20:03:17 +09:00
#endif
public List<AGVPortNode> GetPortNodes()
2025-03-20 12:23:33 +09:00
{
2025-03-24 20:03:17 +09:00
var result = new List<AGVPortNode>();
2025-03-20 12:23:33 +09:00
foreach(var n in nodes)
{
2025-03-24 20:03:17 +09:00
if (n is AGVPortNode pn)
{
result.Add(pn);
}
}
return result;
}
public bool TryGetEmptyInputPortNode(out AGVPortNode portNode)
{
foreach (var n in nodes)
{
if (n is not AGVPortNode pn)
continue;
if (pn.portType != AGVPortNode.PortType.Input)
continue;
if (pn.loader is not InputPort)
continue;
Debug.Log($"TryGetEmptyInputPortNode: {pn.name}");
if (pn.loader.isEmpty)
{
portNode = pn;
return true;
}
}
portNode = null;
return false;
}
internal List<AGVNode> FindPath(AGVNode startNode, AGVNode targetNode)
{
var visited = new HashSet<AGVNode>();
var path = new List<AGVNode>();
if (DFS(startNode, targetNode, visited, path))
{
return path;
}
return null; // <20><><EFBFBD>θ<EFBFBD> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD>
}
private bool DFS(AGVNode current, AGVNode target, HashSet<AGVNode> visited, List<AGVNode> path)
{
visited.Add(current);
path.Add(current);
if (current == target)
{
return true;
}
foreach (var neighbor in current.linkedNodes)
{
if (neighbor == null || visited.Contains(neighbor))
continue;
if (DFS(neighbor, target, visited, path))
2025-03-20 12:23:33 +09:00
{
return true;
}
}
2025-03-24 20:03:17 +09:00
path.RemoveAt(path.Count - 1);
2025-03-20 12:23:33 +09:00
return false;
}
2025-03-20 10:33:24 +09:00
}
}