Files
Simulation/Assets/Scripts/SimulationModels/SimulationModelStore.cs

226 lines
6.9 KiB
C#
Raw Normal View History

2025-05-26 18:06:25 +09:00
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using RTG;
2025-07-14 17:50:13 +09:00
using Octopus.Simulator.Networks;
using Newtonsoft.Json;
using System.Collections;
2025-07-22 14:03:40 +09:00
using System;
using Octopus.Simulator;
2025-07-14 17:50:13 +09:00
public class StoreStatusUpdateData
{
public StoreStatusUpdateDatastatistics statistics;
}
public class StoreStatusUpdateDatastatistics
{
2025-07-22 14:03:40 +09:00
public Int64 current_length;
public Int64 capacity;
2025-07-14 17:50:13 +09:00
}
public class QueueStatusUpdateData
{
public QueueStatusUpdateDatastatistics statistics;
}
public class QueueStatusUpdateDatastatistics
{
2025-07-22 14:03:40 +09:00
public Int64 current_length;
public Int64 capacity;
2025-07-14 17:50:13 +09:00
}
2025-05-26 18:06:25 +09:00
public class SimulationModelStore : SimulationModel
{
public List<Transform> storePositions = new List<Transform>();
public int maxCapacity;
public List<GameObject> storedProducts = new List<GameObject>();
public string storeType = "fifo";
2025-05-27 14:53:01 +09:00
public List<Transform> transporterPositions = new List<Transform>();
2025-07-14 17:50:13 +09:00
2025-07-15 11:00:21 +09:00
protected override void init()
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
base.init();
2025-07-14 17:50:13 +09:00
maxCapacity = storePositions.Count;
}
2025-07-15 11:00:21 +09:00
2025-05-26 18:06:25 +09:00
public void StoreProduct(GameObject product)
{
2025-05-27 14:53:01 +09:00
product.transform.parent = transform;
for (int i = 0; i < storePositions.Count; i++)
{
Transform storePos = storePositions[i];
if (storePos.childCount == 0)
{
product.transform.parent = storePos;
break;
}
}
2025-05-26 18:06:25 +09:00
product.transform.localPosition = Vector3.zero;
product.transform.localRotation = Quaternion.identity;
storedProducts.Add(product);
2025-07-14 09:35:27 +09:00
if (storedProducts.Count > maxCapacity) { }
2025-07-15 11:00:21 +09:00
//Debug.LogWarning("Max Capacity " + maxCapacity + " Reached on : " + nodeID);
2025-05-26 18:06:25 +09:00
}
public GameObject GetProduct()
{
if (storedProducts.Count == 0)
return null;
if (storeType.Contains("fifo"))
{
GameObject product = storedProducts[0];
storedProducts.Remove(product);
return product;
}
else if (storeType.Contains("lifo"))
{
GameObject product = storedProducts[storedProducts.Count - 1];
storedProducts.Remove(product);
return product;
2025-07-15 11:00:21 +09:00
}
2025-05-26 18:06:25 +09:00
return null;
}
2025-05-27 14:53:01 +09:00
public Transform GetTransporterPosition()
{
if (transporterPositions.Count == 0)
return null;
return transporterPositions[UnityEngine.Random.Range(0, transporterPositions.Count)];
}
2025-06-24 17:02:07 +09:00
2025-07-14 17:50:13 +09:00
void AdjustStore(StoreStatusUpdateData Store)
{
2025-07-15 11:00:21 +09:00
int current = storedProducts.Count;
2025-07-22 14:03:40 +09:00
Int64 target = Store.statistics.current_length;
2025-07-15 11:00:21 +09:00
if (current < target)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
if (current > storePositions.Count)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
return;
2025-07-14 17:50:13 +09:00
}
2025-07-22 14:03:40 +09:00
int spawnCount = (int)Mathf.Min(target - current, storePositions.Count - current);
2025-07-15 11:00:21 +09:00
for (int k = 0; k < spawnCount; k++)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
GameObject prod = ProductManager.Instance.SpawnProduct();
Transform emptySlot = storePositions
.FirstOrDefault(slot => slot.childCount == 0);
prod.transform.SetParent(emptySlot, false);
storedProducts.Add(prod);
}
}
else if (current > target)
{
if (current > storePositions.Count)
{
return;
}
2025-07-22 14:03:40 +09:00
int removeCount = (int)(current - target);
2025-07-15 11:00:21 +09:00
for (int k = 0; k < removeCount; k++)
{
GameObject prod = (storeType == "fifo")
? storedProducts[0]
: storedProducts[storedProducts.Count - 1];
storedProducts.Remove(prod);
Destroy(prod);
2025-07-14 17:50:13 +09:00
}
}
}
2025-07-15 11:00:21 +09:00
void AdjustQueue(QueueStatusUpdateData Queue)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
int current = storedProducts.Count;
2025-07-22 14:03:40 +09:00
Int64 target = Queue.statistics.current_length;
2025-07-15 11:00:21 +09:00
if (current < target)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
if (current > storePositions.Count)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
return;
2025-07-14 17:50:13 +09:00
}
2025-07-22 14:03:40 +09:00
int spawnCount = (int)Mathf.Min(target - current, storePositions.Count - current);
2025-07-15 11:00:21 +09:00
for (int k = 0; k < spawnCount; k++)
2025-07-14 17:50:13 +09:00
{
2025-07-15 11:00:21 +09:00
GameObject prod = ProductManager.Instance.SpawnProduct();
Transform emptySlot = storePositions
.FirstOrDefault(slot => slot.childCount == 0);
prod.transform.SetParent(emptySlot, false);
storedProducts.Add(prod);
}
}
else if (current > target)
{
if (current > storePositions.Count)
{
return;
}
2025-07-22 14:03:40 +09:00
int removeCount = (int)(current - target);
2025-07-15 11:00:21 +09:00
for (int k = 0; k < removeCount; k++)
{
GameObject prod = (storeType == "fifo")
? storedProducts[0]
: storedProducts[storedProducts.Count - 1];
storedProducts.Remove(prod);
Destroy(prod);
2025-07-14 17:50:13 +09:00
}
}
}
2025-06-24 17:02:07 +09:00
public override void GetData(string data)
{
2025-07-14 17:50:13 +09:00
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
switch (wrapclass._event)
{
case "store_status_update":
2025-07-15 17:35:53 +09:00
var storeStatus = JsonConvert.DeserializeObject<StoreStatusUpdateData>(wrapclass.data.ToString());
AdjustStore(storeStatus);
SetBubble(storeStatus.statistics.current_length);
2025-07-22 14:03:40 +09:00
SetBubbleDetail_Store(storeStatus.statistics);
2025-07-14 17:50:13 +09:00
break;
case "queue_status_update":
2025-07-15 17:35:53 +09:00
var queueStatus = JsonConvert.DeserializeObject<QueueStatusUpdateData>(wrapclass.data.ToString());
AdjustQueue(queueStatus);
SetBubble(queueStatus.statistics.current_length);
2025-07-22 14:03:40 +09:00
SetBubbleDetail_Queue(queueStatus.statistics);
2025-07-14 17:50:13 +09:00
break;
default:
break;
}
2025-06-24 17:02:07 +09:00
}
2025-07-15 17:35:53 +09:00
public override void SetBubble(object data)
{
string msg = data.ToString();
if (currentBubble == null)
{
// <20><><EFBFBD><EFBFBD>
currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType<Canvas_Bubble>().transform);
currentBubble.target = DataBubbleSocket;
currentBubble.worldOffset = new Vector3(0, 0, 0); // <20>ʿ信 <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
2025-07-22 15:57:29 +09:00
currentBubble.GetComponent<RectTransform>().SetAsFirstSibling();
2025-07-15 17:35:53 +09:00
}
// <20>ؽ<EFBFBD>Ʈ <20><><EFBFBD><EFBFBD>
currentBubble.SetMessage(msg);
}
2025-07-22 14:03:40 +09:00
public void SetBubbleDetail_Store(object data)
{
if (currentBubble != null)
{
2025-07-22 15:57:29 +09:00
currentBubble.SetDetail(data, logicType.store, LogicUIManager.instance.GetItemLabelByID(nodeID));
2025-07-22 14:03:40 +09:00
}
}
public void SetBubbleDetail_Queue(object data)
{
if (currentBubble != null)
{
2025-07-22 15:57:29 +09:00
currentBubble.SetDetail(data, logicType.queue, LogicUIManager.instance.GetItemLabelByID(nodeID));
2025-07-22 14:03:40 +09:00
}
}
2025-05-26 18:06:25 +09:00
}