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

213 lines
6.1 KiB
C#
Raw Normal View History

2025-05-26 18:06:25 +09:00
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
2025-07-14 17:50:13 +09:00
using Newtonsoft.Json;
2025-07-22 14:03:40 +09:00
using System;
using Octopus.Simulator;
2025-07-14 17:50:13 +09:00
2025-08-04 17:26:24 +09:00
#region class
public interface IStatusUpdateStatistics
{
long current_length { get; }
long capacity { get; }
}
2025-07-14 17:50:13 +09:00
public class StoreStatusUpdateData
{
public StoreStatusUpdateDatastatistics statistics;
}
2025-08-04 17:26:24 +09:00
public class StoreStatusUpdateDatastatistics : IStatusUpdateStatistics
2025-07-14 17:50:13 +09:00
{
2025-08-04 17:26:24 +09:00
public long current_length;
public long capacity;
long IStatusUpdateStatistics.current_length => current_length;
long IStatusUpdateStatistics.capacity => capacity;
2025-07-14 17:50:13 +09:00
}
public class QueueStatusUpdateData
{
public QueueStatusUpdateDatastatistics statistics;
}
2025-08-04 17:26:24 +09:00
public class QueueStatusUpdateDatastatistics : IStatusUpdateStatistics
2025-07-14 17:50:13 +09:00
{
2025-08-04 17:26:24 +09:00
public long current_length;
public long capacity;
long IStatusUpdateStatistics.current_length => current_length;
long IStatusUpdateStatistics.capacity => capacity;
2025-07-14 17:50:13 +09:00
}
2025-08-04 17:26:24 +09:00
public enum StoreBehavior
{
FIFO,
LIFO
}
#endregion
2025-05-26 18:06:25 +09:00
public class SimulationModelStore : SimulationModel
{
2025-08-04 17:26:24 +09:00
[Header("Configuration")]
2025-05-26 18:06:25 +09:00
public List<Transform> storePositions = new List<Transform>();
2025-05-27 14:53:01 +09:00
public List<Transform> transporterPositions = new List<Transform>();
2025-07-14 17:50:13 +09:00
2025-08-04 17:26:24 +09:00
public List<GameObject> storedProducts = new List<GameObject>();
public int maxCapacity;
public StoreBehavior storeType = StoreBehavior.FIFO;
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-08-04 17:26:24 +09:00
if (product == null) return;
2025-05-27 14:53:01 +09:00
product.transform.parent = transform;
2025-08-04 17:26:24 +09:00
Transform emptySlot = storePositions.FirstOrDefault(slot => slot.childCount == 0);
if (emptySlot != null)
2025-05-27 14:53:01 +09:00
{
2025-08-04 17:26:24 +09:00
product.transform.SetParent(emptySlot, false);
2025-05-27 14:53:01 +09:00
}
2025-08-04 17:26:24 +09:00
2025-05-26 18:06:25 +09:00
product.transform.localPosition = Vector3.zero;
product.transform.localRotation = Quaternion.identity;
storedProducts.Add(product);
}
2025-08-04 17:26:24 +09:00
2025-05-26 18:06:25 +09:00
public GameObject GetProduct()
{
if (storedProducts.Count == 0)
return null;
2025-08-04 17:26:24 +09:00
GameObject product = storeType == StoreBehavior.FIFO
? storedProducts[0]
: storedProducts[storedProducts.Count - 1];
if (storeType == StoreBehavior.FIFO)
storedProducts.RemoveAt(0);
else
storedProducts.RemoveAt(storedProducts.Count - 1);
return product;
2025-05-26 18:06:25 +09:00
}
2025-08-04 17:26:24 +09:00
2025-05-27 14:53:01 +09:00
public Transform GetTransporterPosition()
{
2025-08-04 17:26:24 +09:00
if (transporterPositions == null || transporterPositions.Count == 0)
2025-05-27 14:53:01 +09:00
return null;
2025-08-04 17:26:24 +09:00
int idx = UnityEngine.Random.Range(0, transporterPositions.Count);
return transporterPositions[idx];
2025-05-27 14:53:01 +09:00
}
2025-06-24 17:02:07 +09:00
2025-08-04 17:26:24 +09:00
private void AdjustToTarget(IStatusUpdateStatistics status)
2025-07-14 17:50:13 +09:00
{
2025-08-04 17:26:24 +09:00
if (status == null) return;
2025-07-15 11:00:21 +09:00
2025-08-04 17:26:24 +09:00
int current = storedProducts.Count;
long target = status.current_length;
int capacity = storePositions.Count;
2025-07-15 11:00:21 +09:00
2025-08-04 17:26:24 +09:00
if (capacity <= 0)
return;
2025-07-14 17:50:13 +09:00
2025-07-15 11:00:21 +09:00
if (current < target)
2025-07-14 17:50:13 +09:00
{
2025-08-04 17:26:24 +09:00
int availableSlots = capacity - current;
int spawnCount = (int)Mathf.Min(target - current, availableSlots);
for (int i = 0; i < spawnCount; i++)
2025-07-14 17:50:13 +09:00
{
2025-08-04 17:26:24 +09:00
Transform emptySlot = storePositions.FirstOrDefault(slot => slot.childCount == 0);
if (emptySlot == null)
break;
2025-07-15 11:00:21 +09:00
2025-08-04 17:26:24 +09:00
GameObject prod = ProductManager.Instance.SpawnProduct();
2025-07-15 11:00:21 +09:00
prod.transform.SetParent(emptySlot, false);
storedProducts.Add(prod);
}
}
else if (current > target)
{
2025-07-22 14:03:40 +09:00
int removeCount = (int)(current - target);
2025-08-04 17:26:24 +09:00
for (int i = 0; i < removeCount; i++)
2025-07-15 11:00:21 +09:00
{
2025-08-04 17:26:24 +09:00
GameObject prod = storeType == StoreBehavior.FIFO
? storedProducts.FirstOrDefault()
: storedProducts.LastOrDefault();
if (prod == null)
continue;
if (storeType == StoreBehavior.FIFO)
storedProducts.RemoveAt(0);
else
storedProducts.RemoveAt(storedProducts.Count - 1);
2025-07-15 11:00:21 +09:00
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());
2025-08-04 17:26:24 +09:00
AdjustToTarget(storeStatus?.statistics);
2025-07-15 17:35:53 +09:00
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());
2025-08-04 17:26:24 +09:00
AdjustToTarget(queueStatus?.statistics);
2025-07-15 17:35:53 +09:00
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
}