166 lines
5.0 KiB
C#
166 lines
5.0 KiB
C#
using Simulator.UI;
|
|
using Simulator.Model;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UVC.Data.Core;
|
|
|
|
|
|
namespace Simulator.Data
|
|
{
|
|
public class SourceComponent : ComponentBase
|
|
{
|
|
public SourceDataClass sourceData = new SourceDataClass();
|
|
SourceModelData sourceModelData = new SourceModelData();
|
|
public List<GameObject> entitySocket;
|
|
List<Entity> possessEntities = new List<Entity>();
|
|
public int count = 0;
|
|
public int index = 0;
|
|
|
|
public void SetComponent(SourceDataClass sourceData)
|
|
{
|
|
this.sourceData = sourceData;
|
|
data = sourceData;
|
|
EntityManager.Instance.OnEntityTransferred += HandleEntityTransferred;
|
|
FitCollider();
|
|
SetPosition();
|
|
}
|
|
|
|
void IncreaseCount()
|
|
{
|
|
count++;
|
|
if (count >= entitySocket.Count)
|
|
{
|
|
//index++;
|
|
count = 0;
|
|
}
|
|
}
|
|
|
|
public override void SetEntity(string key, string name = "")
|
|
{
|
|
PlaceNext(EntityManager.Instance.GetEntity(key, name));
|
|
}
|
|
|
|
void PlaceNext(Entity entity)
|
|
{
|
|
if (entitySocket == null || entitySocket.Count == 0)
|
|
{
|
|
Debug.LogWarning("[SourceComponent] entitySocket이 비어있습니다. 배치를 건너뜁니다.");
|
|
return;
|
|
}
|
|
|
|
// 현재 소켓이 비어있으면 다음 유효 소켓을 찾는다(안전장치)
|
|
int guard = 0;
|
|
while ((entitySocket[count] == null) && guard < entitySocket.Count)
|
|
{
|
|
IncreaseCount();
|
|
guard++;
|
|
}
|
|
|
|
if (entitySocket[count] == null)
|
|
{
|
|
Debug.LogWarning("[SourceComponent] 사용 가능한 소켓이 없습니다. 배치를 건너뜁니다.");
|
|
return;
|
|
}
|
|
|
|
// 부모/로컬 트랜스폼 설정
|
|
entity.transform.SetParent(entitySocket[count].transform, worldPositionStays: false);
|
|
|
|
// Z방향으로 적층(원래 로직 유지)
|
|
float stepZ = EntityManager.Instance.ObjectSize.z;
|
|
entity.transform.localPosition = new Vector3(0f, 0f, stepZ * index);
|
|
entity.transform.localRotation = Quaternion.identity;
|
|
|
|
if (!possessEntities.Contains(entity))
|
|
possessEntities.Add(entity);
|
|
IncreaseCount();
|
|
ProgressDatabase.Instance.CollectData(sourceData.name, (possessEntities.Count.ToString(), sourceModelData.total_entity.ToString()));
|
|
}
|
|
|
|
public override void GetModelData(DataObject modelData)
|
|
{
|
|
sourceModelData.total_entity = modelData.GetDataObject("data").GetInt("total_entity") ?? 0;
|
|
|
|
var datas = modelData.GetDataObject("data");
|
|
var entity_Ids = datas.GetDataArray("entity_ids");
|
|
|
|
foreach (var entity in entity_Ids)
|
|
{
|
|
var id = entity.GetString("entity_id");
|
|
var name = entity.GetString("prefab_name");
|
|
SetEntity(id, name);
|
|
}
|
|
PlayerPropertyDataBase.Instance.CollectGenerateData(entity_Ids.Count);
|
|
/*
|
|
var entityIds = datas["entity_ids"].ConvertTo<List<string>>();
|
|
|
|
|
|
if (entityIds == null || entityIds.Count == 0)
|
|
{
|
|
Debug.LogWarning("[SourceComponent] entity_ids가 비어있습니다.");
|
|
return;
|
|
}
|
|
|
|
// 여러 개를 한 번에 스폰
|
|
var entities = EntityManager.Instance.SpawnEntites(entityIds);
|
|
if (entities == null || entities.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 배치
|
|
foreach (var e in entities)
|
|
{
|
|
if (e == null) continue;
|
|
PlaceNext(e);
|
|
}
|
|
*/
|
|
}
|
|
|
|
private void HandleEntityTransferred(Entity entity)
|
|
{
|
|
if (possessEntities.Contains(entity))
|
|
{
|
|
DecreaseEntity(entity);
|
|
}
|
|
}
|
|
|
|
public override void DecreaseEntity(Entity entity)
|
|
{
|
|
possessEntities.Remove(entity);
|
|
ReflowEntities();
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
base.OnDestroy();
|
|
if (EntityManager.HasInstance)
|
|
{
|
|
EntityManager.Instance.OnEntityTransferred -= HandleEntityTransferred;
|
|
}
|
|
}
|
|
|
|
public void ReflowEntities()
|
|
{
|
|
// 위치 인덱스 초기화
|
|
count = 0;
|
|
index = 0;
|
|
|
|
var snapshot = new List<Entity>(possessEntities.Count);
|
|
foreach (var e in possessEntities)
|
|
{
|
|
if (e != null) snapshot.Add(e);
|
|
}
|
|
|
|
possessEntities.Clear();
|
|
|
|
foreach (var e in snapshot)
|
|
{
|
|
PlaceNext(e);
|
|
}
|
|
}
|
|
public override void GetPath()
|
|
{
|
|
onComponentClicked?.Invoke(componentType, sourceData);
|
|
}
|
|
}
|
|
} |