125 lines
3.8 KiB
C#
125 lines
3.8 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Gpm.Ui.Sample;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using UnityEngine;
|
|
using UVC.Core;
|
|
using UVC.Pool;
|
|
using static UnityEngine.EventSystems.EventTrigger;
|
|
|
|
public class EntityManager : SingletonScene<EntityManager>
|
|
{
|
|
private readonly Dictionary<string, string> prefabPaths = new Dictionary<string, string>()
|
|
{
|
|
{"box","prefabs/Box"}
|
|
};
|
|
|
|
private Dictionary<string, Entity> entityDatas = new Dictionary<string, Entity>();
|
|
Dictionary<Entity,ComponentBase> possessComponents= new Dictionary<Entity,ComponentBase>();
|
|
|
|
public Vector3 ObjectSize;
|
|
public event Action<Entity> OnEntityDestroyed;
|
|
|
|
private GameObjectPool<Entity>? entityPool;
|
|
public GameObjectPool<Entity> EntityPool
|
|
{
|
|
get
|
|
{
|
|
if (entityPool == null)
|
|
{
|
|
Debug.LogError("Pool is not initialized. Please call InitializePoolAsync first.");
|
|
}
|
|
return entityPool!;
|
|
}
|
|
}
|
|
|
|
protected override void Init()
|
|
{
|
|
InitializeEntityPoolAsync();
|
|
}
|
|
|
|
private async UniTask InitializeEntityPoolAsync()
|
|
{
|
|
if (entityPool != null) return;
|
|
|
|
var prefab = await Resources.LoadAsync<GameObject>(prefabPaths["box"]) as GameObject;
|
|
if (prefab == null)
|
|
{
|
|
Debug.LogError($"Prefab not found at path: {prefabPaths["box"]}");
|
|
return;
|
|
}
|
|
entityPool = new GameObjectPool<Entity>(prefab, transform);
|
|
ObjectSize = prefab.GetComponent<MeshFilter>().sharedMesh.bounds.size;
|
|
}
|
|
public Entity SpawnEntity(string entityName,ComponentBase component)
|
|
{
|
|
//Debug.Log($"spawn{entityName}");
|
|
var entity = entityPool.GetItem($"{entityName}");
|
|
entity.name = entityName;
|
|
entityDatas.Add(entityName, entity);
|
|
possessComponents.Add(entity, component);
|
|
return entity;
|
|
}
|
|
|
|
public List<Entity> SpawnEntites(List<string> entityNames,ComponentBase component)
|
|
{
|
|
List<Entity> entities = new List<Entity>();
|
|
foreach (var entityName in entityNames)
|
|
{
|
|
//Debug.Log($"spawn{entityName}");
|
|
var entity = entityPool.GetItem($"{entityName}");
|
|
entity.name = entityName;
|
|
if (!entityDatas.ContainsKey(entityName))
|
|
{
|
|
entityDatas.Add(entityName, entity);
|
|
entities.Add(entity);
|
|
possessComponents.Add(entity,component);
|
|
}
|
|
}
|
|
return entities;
|
|
}
|
|
|
|
public Entity GetEntity(string entityName,ComponentBase component)
|
|
{
|
|
if (entityDatas.ContainsKey(entityName))
|
|
{
|
|
var entity = entityDatas[entityName];
|
|
possessComponents[entity].DecreaseEntity(entity);
|
|
possessComponents[entity] = component;
|
|
return entity;
|
|
}
|
|
else
|
|
{
|
|
return SpawnEntity(entityName,component);
|
|
}
|
|
}
|
|
|
|
public List<Entity> GetEntities(List<string> entityNames, ComponentBase component)
|
|
{
|
|
List<Entity> entities = new List<Entity>();
|
|
foreach (var entityName in entityNames)
|
|
{
|
|
var entity = GetEntity(entityName, component);
|
|
entities.Add(entity);
|
|
}
|
|
return entities;
|
|
}
|
|
|
|
public void DestroyEnity(List<string> entityNames)
|
|
{
|
|
foreach (var entityName in entityNames)
|
|
{
|
|
if (entityDatas.ContainsKey(entityName))
|
|
{
|
|
var entity = entityDatas[entityName];
|
|
possessComponents[entity].DecreaseEntity(entity);
|
|
possessComponents.Remove(entity);
|
|
OnEntityDestroyed?.Invoke(entity);
|
|
Destroy(entity.gameObject);
|
|
entityDatas.Remove(entityName);
|
|
}
|
|
}
|
|
}
|
|
}
|