Files
XRLib/Assets/Scripts/Simulator/Components/RackComponent.cs
2026-02-23 17:06:38 +09:00

148 lines
6.6 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UVC.Data.Core;
namespace Simulator.Data {
public class RackComponent : ComponentBase
{
public GameObject cellPrefab;
public GameObject emptyPrefab;
public RackDataClass rackData;
public Dictionary<(int x,int y, int z),CellComponent> cellComponents=new Dictionary<(int x, int y, int z), CellComponent>();
public List<GameObject> entitys=new List<GameObject>();
public void SetComponent(RackDataClass data)
{
this.data = data;
rackData= data;
SpawnCell(data.rack_layout,false);
onComponentClicked += FindAnyObjectByType<RackProperty>().SetProertyWindow;
rackData.rack_layout.onLayoutChanged += ()=>SpawnCell(rackData.rack_layout, false);
rackData.rack_layout.onLayoutChanged += FitCollider;
rackData.rack_layout.onLayoutChanged += SetPosition;
FitCollider();
SetPosition();
SetRotation();
}
public override void GetModelData(DataObject modelData)
{
var datas = modelData.GetDataObject("data");
var entityid = datas.GetString("entity_id");
List<string> entityids = new List<string>() { entityid };
var coordinates = datas.GetDataObject("coordinates");
var x = (int)coordinates.GetInt("x");
var y = (int)coordinates.GetInt("y");
var z = (int)coordinates.GetInt("z");
var entity=EntityManager.Instance.GetEntities(entityids,this);
SetEntityToCell(entity[0], (x, y, z));
}
public void SetASRSQueue(DataObject modelData)
{
var datas = modelData.GetDataObject("data");
var entityid = datas.GetString("entity_id");
var entity = EntityManager.Instance.GetEntity(entityid, this);
entity.transform.SetParent(cellComponents[(-1, 0, 0)].Socket.transform);
entity.transform.localPosition = new Vector3(0, -0.25f, -0.25f);
entity.transform.localRotation= Quaternion.identity;
entitys.Add(entity.gameObject);
}
public void SetASRSQueue(Entity entity)
{
entity.transform.SetParent(cellComponents[(-1, 0, 0)].Socket.transform);
entity.transform.localPosition = new Vector3(0, -0.25f, -0.25f);
entity.transform.localRotation = Quaternion.identity;
entitys.Add(entity.gameObject);
}
public void InitializeQueue(DataObject modelData)
{
var datas = modelData.GetDataObject("data");
var from_position = datas.GetDataObject("from_position");
var fx = from_position.GetInt("x").Value;
var fy = from_position.GetInt("y").Value;
var fz = from_position.GetInt("z").Value;
Vector3 fromVector3 = new Vector3(fx, fy, fz);
var to_position = datas.GetDataObject("to_position");
var tx = to_position.GetInt("x").Value;
var ty = to_position.GetInt("y").Value;
var tz = to_position.GetInt("z").Value;
Vector3 toVector3 = new Vector3(tx, ty, tz);
var entity_Ids = datas.GetDataArray("entities");
List<Entity> entities = new List<Entity>();
var prefab = datas.GetDataObject("prefab");
foreach (var entity in entity_Ids)
{
entities.Add(EntityManager.Instance.SpawnEntity(entity.GetString("value"), this, prefab.GetString("name")));
}
int index = 0;
for (int x = fx; x <= tx; x++)
{
for(int y = fy; y <= ty; y++)
{
for(int z = fz; z <= tz; z++)
{
SetEntityToCell(entities[index++], (x, y, z));
}
}
}
}
void SetEntityToCell(Entity entity,(int x,int y,int z) coord)
{
entity.transform.SetParent(cellComponents[(coord.x,coord.y,coord.z)].Socket.transform);
entity.transform.localPosition = new Vector3(0, -0.25f, -0.25f);
entity.transform.localRotation = Quaternion.identity;
entitys.Add(entity.gameObject);
}
public void SpawnCell(rack_layout layout,bool asrs)
{
foreach(var cell in cellComponents)
{
cell.Value.gameObject.SetActive(false);
Destroy(cell.Value.gameObject);
}
cellComponents.Clear();
for (int z = 0; z < layout.z; z++)
{
for(int y=0;y<layout.y; y++)
{
for(int x=0;x<layout.x; x++)
{
var cell = Instantiate(cellPrefab);
cell.transform.localScale=new Vector3(layout.x_length/0.63f, layout.y_length / 0.63f, layout.z_length / 0.59f);
cell.transform.parent = this.transform;
cell.transform.localPosition = new Vector3(layout.x_length * x, layout.y_length * y + 0.8f, layout.z_length * z);
cellComponents.Add((x, y, z), cell.GetComponent<CellComponent>());
cellComponents[(x, y, z)].x = x;
cellComponents[(x, y, z)].y = y;
cellComponents[(x, y, z)].z = z;
}
}
}
if (asrs)
{
var pickcell = Instantiate(emptyPrefab);
pickcell.transform.localScale = new Vector3(layout.x_length / 0.63f, layout.y_length / 0.63f, layout.z_length / 0.59f);
pickcell.transform.parent = this.transform;
pickcell.transform.localPosition = new Vector3(layout.x_length * 0f, layout.y_length * 0 + 0.8f, layout.z_length * -4f);
cellComponents.Add((-1, 0, 0), pickcell.GetComponent<CellComponent>());
var dropcell = Instantiate(emptyPrefab);
dropcell.transform.localScale = new Vector3(layout.x_length / 0.63f, layout.y_length / 0.63f, layout.z_length / 0.59f);
dropcell.transform.parent = this.transform;
dropcell.transform.localPosition = new Vector3(layout.x_length * (layout.x - 1f), layout.y_length * 0 + 0.8f, layout.z_length * -4f);
cellComponents.Add(((int)(layout.x), 0, 0), dropcell.GetComponent<CellComponent>());
}
}
public override void getpath()
{
onComponentClicked?.Invoke(ComponentType.Rack, rackData);
}
}
}