226 lines
6.9 KiB
C#
226 lines
6.9 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using RTG;
|
|
using Octopus.Simulator.Networks;
|
|
using Newtonsoft.Json;
|
|
using System.Collections;
|
|
using System;
|
|
using Octopus.Simulator;
|
|
|
|
public class StoreStatusUpdateData
|
|
{
|
|
public StoreStatusUpdateDatastatistics statistics;
|
|
}
|
|
|
|
public class StoreStatusUpdateDatastatistics
|
|
{
|
|
public Int64 current_length;
|
|
public Int64 capacity;
|
|
}
|
|
|
|
public class QueueStatusUpdateData
|
|
{
|
|
public QueueStatusUpdateDatastatistics statistics;
|
|
}
|
|
|
|
public class QueueStatusUpdateDatastatistics
|
|
{
|
|
public Int64 current_length;
|
|
public Int64 capacity;
|
|
}
|
|
|
|
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";
|
|
public List<Transform> transporterPositions = new List<Transform>();
|
|
|
|
protected override void init()
|
|
{
|
|
base.init();
|
|
maxCapacity = storePositions.Count;
|
|
}
|
|
|
|
public void StoreProduct(GameObject product)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
product.transform.localPosition = Vector3.zero;
|
|
product.transform.localRotation = Quaternion.identity;
|
|
storedProducts.Add(product);
|
|
if (storedProducts.Count > maxCapacity) { }
|
|
//Debug.LogWarning("Max Capacity " + maxCapacity + " Reached on : " + nodeID);
|
|
}
|
|
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;
|
|
}
|
|
return null;
|
|
}
|
|
public Transform GetTransporterPosition()
|
|
{
|
|
if (transporterPositions.Count == 0)
|
|
return null;
|
|
return transporterPositions[UnityEngine.Random.Range(0, transporterPositions.Count)];
|
|
}
|
|
|
|
void AdjustStore(StoreStatusUpdateData Store)
|
|
{
|
|
int current = storedProducts.Count;
|
|
Int64 target = Store.statistics.current_length;
|
|
if (current < target)
|
|
{
|
|
if (current > storePositions.Count)
|
|
{
|
|
return;
|
|
}
|
|
int spawnCount = (int)Mathf.Min(target - current, storePositions.Count - current);
|
|
for (int k = 0; k < spawnCount; k++)
|
|
{
|
|
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;
|
|
}
|
|
int removeCount = (int)(current - target);
|
|
for (int k = 0; k < removeCount; k++)
|
|
{
|
|
GameObject prod = (storeType == "fifo")
|
|
? storedProducts[0]
|
|
: storedProducts[storedProducts.Count - 1];
|
|
|
|
storedProducts.Remove(prod);
|
|
Destroy(prod);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AdjustQueue(QueueStatusUpdateData Queue)
|
|
{
|
|
int current = storedProducts.Count;
|
|
Int64 target = Queue.statistics.current_length;
|
|
if (current < target)
|
|
{
|
|
if (current > storePositions.Count)
|
|
{
|
|
return;
|
|
}
|
|
int spawnCount = (int)Mathf.Min(target - current, storePositions.Count - current);
|
|
for (int k = 0; k < spawnCount; k++)
|
|
{
|
|
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;
|
|
}
|
|
int removeCount = (int)(current - target);
|
|
for (int k = 0; k < removeCount; k++)
|
|
{
|
|
GameObject prod = (storeType == "fifo")
|
|
? storedProducts[0]
|
|
: storedProducts[storedProducts.Count - 1];
|
|
|
|
storedProducts.Remove(prod);
|
|
Destroy(prod);
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void GetData(string data)
|
|
{
|
|
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
|
|
switch (wrapclass._event)
|
|
{
|
|
case "store_status_update":
|
|
var storeStatus = JsonConvert.DeserializeObject<StoreStatusUpdateData>(wrapclass.data.ToString());
|
|
AdjustStore(storeStatus);
|
|
SetBubble(storeStatus.statistics.current_length);
|
|
SetBubbleDetail_Store(storeStatus.statistics);
|
|
break;
|
|
case "queue_status_update":
|
|
var queueStatus = JsonConvert.DeserializeObject<QueueStatusUpdateData>(wrapclass.data.ToString());
|
|
AdjustQueue(queueStatus);
|
|
SetBubble(queueStatus.statistics.current_length);
|
|
SetBubbleDetail_Queue(queueStatus.statistics);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override void SetBubble(object data)
|
|
{
|
|
string msg = data.ToString();
|
|
|
|
if (currentBubble == null)
|
|
{
|
|
// 생성
|
|
currentBubble = Instantiate(bubbleUIPrefab, FindAnyObjectByType<Canvas_Bubble>().transform);
|
|
currentBubble.target = DataBubbleSocket;
|
|
currentBubble.worldOffset = new Vector3(0, 0, 0); // 필요에 따라 조절
|
|
currentBubble.GetComponent<RectTransform>().SetAsFirstSibling();
|
|
}
|
|
// 텍스트 갱신
|
|
currentBubble.SetMessage(msg);
|
|
}
|
|
|
|
public void SetBubbleDetail_Store(object data)
|
|
{
|
|
if (currentBubble != null)
|
|
{
|
|
currentBubble.SetDetail(data, logicType.store, LogicUIManager.instance.GetItemLabelByID(nodeID));
|
|
}
|
|
}
|
|
|
|
public void SetBubbleDetail_Queue(object data)
|
|
{
|
|
if (currentBubble != null)
|
|
{
|
|
currentBubble.SetDetail(data, logicType.queue, LogicUIManager.instance.GetItemLabelByID(nodeID));
|
|
}
|
|
}
|
|
}
|