using Newtonsoft.Json.Linq; using UnityEngine; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine.Events; public class SimulationModelMove : SimulationModel { public string eventLoading = "transporter_loading"; public string eventMove = "transporter_moving"; public string eventUnloading = "transporter_unloading"; public string eventSpeed = "transporter_speed_factor_applied"; public string eventReturning = "transporter_returning"; public List listProducts = new List(); public Transform productPos; public Vector3 productDistance = new Vector3(0, 0.321f, 0); public UnityEvent onMove; public UnityEvent onWait; ConcurrentQueue dataQueue; int productCount = 0; Transform origin = null; Transform destination = null; float arrivalTime = 0; float elapsedTime = 0; Vector3 originalPos; Vector3 prevPos; bool isRewind = false; void Update() { int interval = 0; if (productCount != listProducts.Count) { productCount = listProducts.Count; for (int i = 0; i < productCount; i++) { GameObject gb = listProducts[i]; gb.transform.localPosition = productDistance * interval; interval++; } } if (destination != null) { elapsedTime += Time.deltaTime; if (elapsedTime / arrivalTime > 1.0f) { transform.position = destination.position; transform.forward = destination.forward; destination = null; } else { transform.position = Vector3.Lerp(originalPos, destination.position, elapsedTime / arrivalTime); Vector3 dir = transform.position - prevPos; dir.y = 0; dir.Normalize(); transform.forward = dir; prevPos = transform.position; } } } protected override IEnumerator RunSimulationCoroutine() { yield return new WaitUntil(() => !string.IsNullOrEmpty(nodeID)); while (dataQueue == null) { dataQueue = MQTTDataBase.Instance.GetDataQueue(nodeID); if (dataQueue == null) yield return null; else break; } while (true) { if (dataQueue.IsEmpty) { yield return null; continue; } if (dataQueue.TryDequeue(out JObject currentData)) { string eventKey = "_event"; string[] arrivalTimeKey = { "data", "time" }; string[] returnTimeKey = { "data", "return_time" }; string[] originalTimeKey = { "data", "original" }; string[] adjustedTimeKey = { "data", "adjusted" }; string[] inputQueueKey = { "data", "input", "queues" }; string[] outputQueueKey = { "data", "output", "queue" }; string[] outputStoreKey = { "data", "output", "store" }; string[] sourceQueueKey = { "data", "source_queues" }; string[] sourceStoreKey = { "data", "source_stores" }; string[] loadCountKey = { "data", "loaded_count" }; string[] unloadQueueKey = { "data", "output_queue" }; if (currentData.ContainsKey(eventKey)) { string value = currentData[eventKey].ToString(); //if (value.Contains(eventMove)) //{ // Debug.Log("MoveeventIncomming"); // elapsedTime = 0; // arrivalTime = GetJsonFloatValue(currentData, arrivalTimeKey); // originalPos = transform.position; // prevPos = transform.position; // JArray originQueues = GetJsonArray(currentData, inputQueueKey); // string arrivalQueue = GetJsonValue(currentData, outputQueueKey)?.ToString(); // if ( string.IsNullOrEmpty(arrivalQueue)) // { // arrivalQueue = GetJsonValue(currentData, outputStoreKey)?.ToString(); // } // if (originQueues != null && originQueues.HasValues) // { // string queueID = originQueues[0].ToString(); // SimulationModel model = DataManager.I.GetModel(queueID); // SimulationModelStore storeModel = (SimulationModelStore)model; // origin = storeModel.GetTransporterPosition(); // } // if (arrivalQueue != null) // { // SimulationModel model = DataManager.I.GetModel(arrivalQueue); // SimulationModelStore storeModel = (SimulationModelStore)model; // destination = storeModel.GetTransporterPosition(); // } //} if (value.Contains(eventLoading)) { yield return null; JArray sourceQueues = GetJsonArray(currentData, sourceQueueKey); JArray sourceStores = GetJsonArray(currentData, sourceStoreKey); int loadCount = GetJsonIntValue(currentData, loadCountKey); if (sourceQueues != null && sourceQueues.HasValues) { string queueID = sourceQueues[0].ToString(); SimulationModel model = DataManager.I.GetModel(queueID); SimulationModelStore storeModel = (SimulationModelStore)model; for (int i = 0; i < loadCount; i++) { GameObject product = null; yield return new WaitUntil(() => { product = storeModel.GetProduct(); return product != null; }); product.transform.parent = productPos; product.transform.localPosition = Vector3.zero; product.transform.localRotation = Quaternion.identity; listProducts.Add(product); } } else if (sourceStores != null && sourceStores.HasValues) { string storeID = sourceStores[0].ToString(); SimulationModel model = DataManager.I.GetModel(storeID); SimulationModelStore storeModel = (SimulationModelStore)model; for (int i = 0; i < loadCount; i++) { GameObject product = null; yield return new WaitUntil(() => { product = storeModel.GetProduct(); return product != null; }); product.transform.parent = productPos; product.transform.localPosition = Vector3.zero; product.transform.localRotation = Quaternion.identity; } } } else if (value.Contains(eventMove)) { Debug.Log("MoveeventIncomming"); elapsedTime = 0; arrivalTime = GetJsonFloatValue(currentData, arrivalTimeKey); originalPos = transform.position; prevPos = transform.position; JArray originQueues = GetJsonArray(currentData, inputQueueKey); string arrivalQueue = GetJsonValue(currentData, outputQueueKey)?.ToString(); if (originQueues != null && originQueues.HasValues) { string queueID = originQueues[0].ToString(); SimulationModel model = DataManager.I.GetModel(queueID); SimulationModelStore storeModel = (SimulationModelStore)model; origin = storeModel.GetTransporterPosition(); } if (arrivalQueue != null) { SimulationModel model = DataManager.I.GetModel(arrivalQueue); SimulationModelStore storeModel = (SimulationModelStore)model; destination = storeModel.GetTransporterPosition(); } } else if (value.Contains(eventUnloading)) { yield return null; string queueID = GetJsonValue(currentData, unloadQueueKey)?.ToString(); SimulationModel model = DataManager.I.GetModel(queueID); SimulationModelStore storeModel = (SimulationModelStore)model; if (listProducts.Count > 0) { GameObject product = listProducts[0]; storeModel.StoreProduct(product); listProducts.Remove(product); } else { Debug.LogWarning("Trying to unload from empty transporter : " + nodeID); } } else if (value.Contains(eventSpeed)) { float originalTime = GetJsonFloatValue(currentData, originalTimeKey); float adjustedTime = GetJsonFloatValue(currentData, adjustedTimeKey); arrivalTime += adjustedTime - originalTime; elapsedTime = 0; originalPos = transform.position; prevPos = transform.position; } else if (value.Contains(eventReturning)) { elapsedTime = 0; arrivalTime = GetJsonFloatValue(currentData, returnTimeKey); originalPos = transform.position; prevPos = transform.position; if (origin != null) { destination = origin; } } } } yield return null; } yield return null; } }