Files
Simulation/Assets/Scripts/SimulationModels/SimulationModel.cs
2025-06-11 17:40:10 +09:00

142 lines
3.5 KiB
C#

using UnityEngine;
using System.Collections;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections.Generic;
using Octopus.Simulator;
using System;
public enum SimulationModelType
{
NoSpecific,
Source,
Move,
Queue,
Process,
Store,
Resource,
Sink
}
public class SimulationModel : MonoBehaviour, IClickable
{
public string modelName;
public SimulationModelType modelType;
public string modelID;
public string nodeID;
private bool isQuitting = false;
public Action<SimulationModel> onModelClicked;
void Awake()
{
FitCollider();
}
private void Start()
{
onModelClicked += FindAnyObjectByType<Panel_ConnectedObject>(FindObjectsInactive.Include).SetConnectedDataItem;
onModelClicked += FindAnyObjectByType<LogicUIManager>(FindObjectsInactive.Include).ModelSelected;
onModelClicked += (value)=>FindAnyObjectByType<Panel_PlacedObject>(FindObjectsInactive.Include).UnsetPlacedObjectDataItem();
}
private void OnEnable()
{
StartCoroutine(RunSimulationCoroutine());
}
private void OnDisable()
{
if (isQuitting) return;
DataManager.I.RemoveModel(this);
StopAllCoroutines();
}
void OnApplicationQuit()
{
isQuitting = true;
}
public void OnClick()
{
//Debug.Log("Clicked : " + gameObject.name);
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);
}
protected virtual IEnumerator RunSimulationCoroutine()
{
yield return null;
}
public object GetJsonValue(JToken token, IEnumerable<string> path)
{
foreach (var key in path)
{
if (token == null) return null;
token = token[key];
}
return token;
}
public JArray GetJsonArray(JToken token, IEnumerable<string> path)
{
foreach (var key in path)
{
if (token == null) return null;
token = token[key];
}
return token as JArray;
}
public int GetJsonIntValue(JToken token, IEnumerable<string> path)
{
int value = 0;
foreach (var key in path)
{
if (token == null) return 0;
token = token[key];
}
if (token != null && int.TryParse(token.ToString(), out value))
{
return value;
}
return 0;
}
public float GetJsonFloatValue(JToken token, IEnumerable<string> path)
{
float value = 0;
foreach (var key in path)
{
if (token == null) return 0;
token = token[key];
}
if (token != null && float.TryParse(token.ToString(), out value))
{
return value;
}
return 0;
}
public void UpdatePopupData(string json)
{
var wrapper=JsonConvert.DeserializeObject<mqttwrab>(json);
}
public void SetPopupData()
{
var ldmanager = FindAnyObjectByType<LogicDataManager>();
}
public void DeletePopupData()
{
}
}