Files
XRLib/Assets/Scripts/Simulator/Components/SourceComponent.cs

166 lines
5.0 KiB
C#
Raw Permalink Normal View History

2025-12-08 10:59:29 +09:00
using Simulator.UI;
2025-10-16 10:24:29 +09:00
using Simulator.Model;
2025-11-04 11:02:02 +09:00
using System.Collections.Generic;
2025-10-16 10:24:29 +09:00
using UnityEngine;
using UVC.Data.Core;
2025-12-08 10:59:29 +09:00
namespace Simulator.Data
2025-10-16 10:24:29 +09:00
{
2025-12-08 10:59:29 +09:00
public class SourceComponent : ComponentBase
2025-10-16 10:24:29 +09:00
{
2025-12-17 11:42:57 +09:00
public SourceDataClass sourceData = new SourceDataClass();
2025-12-08 10:59:29 +09:00
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)
2025-11-04 11:02:02 +09:00
{
2025-12-08 10:59:29 +09:00
this.sourceData = sourceData;
data = sourceData;
2026-02-26 14:04:33 +09:00
EntityManager.Instance.OnEntityTransferred += HandleEntityTransferred;
2025-12-16 17:57:20 +09:00
FitCollider();
2026-02-03 11:40:26 +09:00
SetPosition();
2025-11-04 11:02:02 +09:00
}
2025-12-08 10:59:29 +09:00
void IncreaseCount()
2025-11-04 11:02:02 +09:00
{
2025-12-08 10:59:29 +09:00
count++;
if (count >= entitySocket.Count)
{
//index++;
count = 0;
}
2025-11-04 11:02:02 +09:00
}
2026-02-23 17:06:38 +09:00
public override void SetEntity(string key, string name = "")
{
2026-02-26 14:04:33 +09:00
PlaceNext(EntityManager.Instance.GetEntity(key, name));
2026-02-23 17:06:38 +09:00
}
2025-12-08 10:59:29 +09:00
void PlaceNext(Entity entity)
2025-11-04 11:02:02 +09:00
{
2025-12-08 10:59:29 +09:00
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);
2025-11-04 11:02:02 +09:00
IncreaseCount();
2026-02-10 17:01:25 +09:00
ProgressDatabase.Instance.CollectData(sourceData.name, (possessEntities.Count.ToString(), sourceModelData.total_entity.ToString()));
2025-11-04 11:02:02 +09:00
}
2025-12-08 10:59:29 +09:00
public override void GetModelData(DataObject modelData)
2025-11-04 11:02:02 +09:00
{
2025-12-08 10:59:29 +09:00
sourceModelData.total_entity = modelData.GetDataObject("data").GetInt("total_entity") ?? 0;
var datas = modelData.GetDataObject("data");
2025-12-11 18:26:09 +09:00
var entity_Ids = datas.GetDataArray("entity_ids");
2025-12-08 10:59:29 +09:00
2025-12-11 18:26:09 +09:00
foreach (var entity in entity_Ids)
2025-12-08 10:59:29 +09:00
{
2025-12-11 18:26:09 +09:00
var id = entity.GetString("entity_id");
var name = entity.GetString("prefab_name");
2026-02-23 17:06:38 +09:00
SetEntity(id, name);
2025-12-08 10:59:29 +09:00
}
2026-02-03 11:40:26 +09:00
PlayerPropertyDataBase.Instance.CollectGenerateData(entity_Ids.Count);
2025-12-11 18:26:09 +09:00
/*
var entityIds = datas["entity_ids"].ConvertTo<List<string>>();
2025-12-08 10:59:29 +09:00
2025-12-11 18:26:09 +09:00
if (entityIds == null || entityIds.Count == 0)
{
Debug.LogWarning("[SourceComponent] entity_ids가 비어있습니다.");
return;
}
// 여러 개를 한 번에 스폰
2026-02-26 14:04:33 +09:00
var entities = EntityManager.Instance.SpawnEntites(entityIds);
2025-12-11 18:26:09 +09:00
if (entities == null || entities.Count == 0)
{
return;
}
// 배치
foreach (var e in entities)
{
if (e == null) continue;
PlaceNext(e);
}
*/
2025-11-04 11:02:02 +09:00
}
2026-02-26 14:04:33 +09:00
private void HandleEntityTransferred(Entity entity)
{
if (possessEntities.Contains(entity))
{
DecreaseEntity(entity);
}
}
2025-12-08 10:59:29 +09:00
public override void DecreaseEntity(Entity entity)
2025-11-04 11:02:02 +09:00
{
2025-12-08 10:59:29 +09:00
possessEntities.Remove(entity);
ReflowEntities();
2025-11-04 11:02:02 +09:00
}
2026-02-26 14:04:33 +09:00
protected override void OnDestroy()
{
base.OnDestroy();
2026-02-26 17:09:39 +09:00
if (EntityManager.HasInstance)
2026-02-26 14:04:33 +09:00
{
EntityManager.Instance.OnEntityTransferred -= HandleEntityTransferred;
}
}
2025-12-08 10:59:29 +09:00
public void ReflowEntities()
2025-11-04 11:02:02 +09:00
{
2025-12-08 10:59:29 +09:00
// 위치 인덱스 초기화
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)
{
2026-02-23 17:06:38 +09:00
PlaceNext(e);
2025-12-08 10:59:29 +09:00
}
2025-11-04 11:02:02 +09:00
}
2026-02-25 16:30:12 +09:00
public override void GetPath()
2025-12-17 11:42:57 +09:00
{
2025-12-24 17:36:01 +09:00
onComponentClicked?.Invoke(componentType, sourceData);
2025-12-17 11:42:57 +09:00
}
2025-10-16 10:24:29 +09:00
}
2025-12-08 10:59:29 +09:00
}