Files
XRLib/Assets/Scripts/Simulator/Components/RackComponent.cs
2026-01-16 11:36:54 +09:00

138 lines
6.0 KiB
C#

using System.Collections.Generic;
using Unity.VisualScripting;
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;
}
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 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 entityIds = datas["entities"].ConvertTo<List<string>>();
//var entities=EntityManager.Instance.SpawnEntites(entityIds, this);
List<Entity> entities = new List<Entity>();
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");
entities.Add(EntityManager.Instance.SpawnEntity(id, this, name));
}
int index = 0;
for (int x = fx; x < tx; x++)
{
for(int y = fy; y < fy; y++)
{
for(int z = fz; z < fz; 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);
entitys.Add(entity.gameObject);
}
public void SpawnCell(rack_layout layout,bool asrs)
{
//Vector3 center = new Vector3(layout.x * layout.x_length / 2f, 0f, layout.z * layout.z_length / 2f);
//transform.position = center;
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.position = new Vector3(layout.x_length * x, layout.y_length * y+0.8f, layout.z_length*z);
cell.transform.parent = this.transform;
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.position = new Vector3(layout.x_length*0f, layout.y_length * 0 + 0.8f, layout.z_length * -4f);
pickcell.transform.parent = this.transform;
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.position = new Vector3(layout.x_length * (layout.x-1f), layout.y_length * 0+0.8f, layout.z_length * -4f);
dropcell.transform.parent = this.transform;
cellComponents.Add(((int)(layout.x), 0, 0), dropcell.GetComponent<CellComponent>());
}
else
{
FitCollider();
}
}
public override void getpath()
{
onComponentClicked?.Invoke(ComponentType.Rack, rackData);
}
}
}