Files
XRLib/Assets/Scripts/Simulator/Components/AGV/AGVNodeManager.cs
2026-03-17 10:52:33 +09:00

92 lines
2.9 KiB
C#

using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using UnityEngine;
using UVC.Core;
using UVC.Pool;
namespace Simulator.Data.Transport
{
public class AGVNodeManager : SingletonScene<AGVNodeManager>
{
private readonly Dictionary<string, string> prefabPaths = new Dictionary<string, string>()
{
{"node","prefabs/Node"},
{"path","prefabs/Path"},
};
Dictionary<string, AGVNode> nodeDict = new Dictionary<string, AGVNode>();
private GameObjectPool<AGVNode>? nodePool;
private GameObjectPool<AGVPath>? pathPool;
public GameObjectPool<AGVNode> NodePool
{
get
{
if (nodePool == null)
{
//Debug.LogError("Pool is not initialized. Please call InitializePoolAsync first.");
}
return nodePool!;
}
}
public GameObjectPool<AGVPath> PathPool
{
get
{
if (pathPool == null)
{
//Debug.LogError("Pool is not initialized. Please call InitializePoolAsync first.");
}
return pathPool!;
}
}
protected override void Init()
{
InitializePoolAsync<AGVNode>(nodePool, "node").ContinueWith(pool => { nodePool = pool; });
InitializePoolAsync<AGVPath>(pathPool, "path").ContinueWith(pool => { pathPool = pool; });
}
public Vector3 GetNodePosition(string name)
{
if (!nodeDict.ContainsKey(name))
{
return Vector3.zero;
}
return nodeDict[name].transform.position;
}
public void SpawnNode(List<NodeDataClass> datas)
{
foreach (var data in datas)
{
var node = nodePool.GetItem($"{data.id}");
node.transform.position = data.GetPosition();
node.data = data;
nodeDict[data.id] = node;
}
}
public void LinkNode(List<PathDataClass> datas)
{
foreach (var data in datas)
{
var path = pathPool.GetItem($"{data.from}{data.to}");
path.SetPathData(data, GetNodePosition(data.from), GetNodePosition(data.to));
}
}
private async UniTask<GameObjectPool<T>> InitializePoolAsync<T>(GameObjectPool<T> existingPool, string key) where T : MonoBehaviour
{
if (existingPool != null) return existingPool;
var prefab = await Resources.LoadAsync<GameObject>(prefabPaths[key]) as GameObject;
if (prefab == null)
{
Debug.LogError($"Prefab not found at path: {prefabPaths[key]}");
return null;
}
return new GameObjectPool<T>(prefab, transform);
}
}
}