Files
Simulation/Assets/WorkSpace/LH/Simulation/SimulationModel.cs
2025-07-18 14:42:37 +09:00

103 lines
2.8 KiB
C#

using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
using System.Collections.Generic;
using Octopus.Simulator;
using System;
public enum SimulationModelType
{
NoSpecific,
Source,
Move,
Queue,
Process,
Store,
Resource,
Sink
}
public abstract class SimulationModel : MonoBehaviour, IClickable
{
public string modelName;
public string objectName;
public SimulationModelType modelType;
public string prefabName;
public string modelID;
public string nodeID;
public Transform DataBubbleSocket;
protected BubbleUI bubbleUIPrefab;
protected BubbleUI currentBubble;
public string eventKey;
private bool isQuitting = false;
public Action<SimulationModel> onModelClicked;
protected Panel_SimulationUI simulationUI;
void Awake()
{
init();
prefabName = gameObject.name.Replace("(Clone)", "");
}
private void Start()
{
onModelClicked += FindAnyObjectByType<Panel_ConnectedObject>(FindObjectsInactive.Include).SetConnectedDataItem;
onModelClicked += FindAnyObjectByType<LogicUIManager>(FindObjectsInactive.Include).ModelSelected;
onModelClicked += (value)=>FindAnyObjectByType<Panel_PlacedObject>(FindObjectsInactive.Include).UnsetPlacedObjectDataItem();
}
protected virtual void init()
{
FitCollider();
bubbleUIPrefab = Resources.Load<BubbleUI>("BubbleUI");
DataBubbleSocket = transform.Find("BubbleSocket");
simulationUI = FindAnyObjectByType<Panel_SimulationUI>();
if (!DataBubbleSocket)
{
DataBubbleSocket = this.transform;
}
}
private void OnEnable()
{
StartCoroutine(RunSimulationCoroutine());
}
private void OnDestroy()
{
if (isQuitting) return;
DataManager.I.RemoveModel(this);
StopAllCoroutines();
}
void OnApplicationQuit()
{
isQuitting = true;
}
public void OnClick()
{
onModelClicked?.Invoke(this);
}
void FitCollider()
{
var renderers = GetComponentsInChildren<Renderer>();
if (renderers.Length == 0) return;
Bounds bounds = renderers[0].bounds;
foreach (var r in renderers)
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));
}
protected virtual IEnumerator RunSimulationCoroutine()
{
yield return null;
}
public abstract void GetData(string data);
public abstract void SetBubble(object data);
}