109 lines
3.2 KiB
C#
109 lines
3.2 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()
|
|
{
|
|
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<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 InitializeNodePoolAsync()
|
|
{
|
|
if (nodePool != null) return;
|
|
|
|
var prefab = await Resources.LoadAsync<GameObject>(prefabPaths["node"]) as GameObject;
|
|
if (prefab == null)
|
|
{
|
|
Debug.LogError($"Prefab not found at path: {prefabPaths["node"]}");
|
|
return;
|
|
}
|
|
nodePool = new GameObjectPool<AGVNode>(prefab, transform);
|
|
}
|
|
private async UniTask InitializePathPoolAsync()
|
|
{
|
|
if (pathPool != null) return;
|
|
|
|
var prefab = await Resources.LoadAsync<GameObject>(prefabPaths["path"]) as GameObject;
|
|
if (prefab == null)
|
|
{
|
|
Debug.LogError($"Prefab not found at path: {prefabPaths["path"]}");
|
|
return;
|
|
}
|
|
pathPool = new GameObjectPool<AGVPath>(prefab, transform);
|
|
}
|
|
}
|
|
} |