Files
XRLib/Assets/Scripts/Simulator/Components/ComponentBase.cs
2026-02-25 16:30:12 +09:00

122 lines
3.5 KiB
C#

using Simulator.Data;
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UVC.Data.Core;
namespace Simulator.Data
{
public enum ComponentType
{
Source,
Sink,
Rack,
Queue,
ASRS,
RobotArm,
Processor,
Worker
}
public class ComponentBase : MonoBehaviour, IPointerClickHandler
{
protected ComponentDataBase data;
protected ModelDataBase modelData;
public ComponentType componentType;
public Action<ComponentType, ComponentDataBase> onComponentClicked;
public GameObject pivot;
public void SetPosition()
{
if (pivot != null)
{
pivot.transform.position = new Vector3(data.physical.position.x, 0, -data.physical.position.y);
}
else
{
transform.position = new Vector3(data.physical.position.x, 0, -data.physical.position.y);
}
}
public void SetRotation()
{
if (pivot != null)
{
pivot.transform.rotation = Quaternion.Euler(0, data.physical.orientation, 0);
}
else
{
transform.rotation = Quaternion.Euler(0, data.physical.orientation, 0);
}
}
public virtual void GetModelData(DataObject modelData)
{
}
public virtual void DecreaseEntity(Entity entity)
{
}
public virtual void GetPath()
{
}
public virtual void SetEntity(string key, string name = "")
{
}
protected void FitCollider()
{
var renderers = GetComponentsInChildren<Renderer>();
if (renderers.Length == 0) return;
Bounds bounds = renderers[0].bounds;
foreach (var r in renderers)
{
if (r.gameObject.name.Contains("StackerCrane"))
{
continue;
}
bounds.Encapsulate(r.bounds);
}
BoxCollider collider = GetComponent<BoxCollider>();
if (!collider) collider = gameObject.AddComponent<BoxCollider>();
collider.center = transform.InverseTransformPoint(bounds.center);
collider.size = transform.InverseTransformVector(bounds.size);
collider.size = new Vector3(Mathf.Abs(collider.size.x), Mathf.Abs(collider.size.y), Mathf.Abs(collider.size.z));
collider.isTrigger = true;
if (data != null)
{
data.physical.position.onChanged += SetPosition;
data.physical.onOrientationChanged += SetRotation;
}
if (pivot != null)
{
gameObject.transform.parent = null;
Destroy(pivot.gameObject);
}
pivot = new GameObject($"{this.gameObject.name}pivot");
pivot.transform.position = new Vector3(bounds.center.x, 0, bounds.center.z);
gameObject.transform.parent = pivot.transform;
}
protected virtual void OnDestroy()
{
if (data != null)
{
data.physical.position.onChanged -= SetPosition;
data.physical.onOrientationChanged -= SetRotation;
}
}
public void OnPointerClick(PointerEventData eventData)
{
GameObject clickedObject = eventData.pointerCurrentRaycast.gameObject;
GetPath();
}
}
}