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