move
This commit is contained in:
@@ -1,22 +1,29 @@
|
||||
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;
|
||||
|
||||
#region class
|
||||
public interface IStatusUpdateStatistics
|
||||
{
|
||||
long current_length { get; }
|
||||
long capacity { get; }
|
||||
}
|
||||
|
||||
public class StoreStatusUpdateData
|
||||
{
|
||||
public StoreStatusUpdateDatastatistics statistics;
|
||||
}
|
||||
|
||||
public class StoreStatusUpdateDatastatistics
|
||||
public class StoreStatusUpdateDatastatistics : IStatusUpdateStatistics
|
||||
{
|
||||
public Int64 current_length;
|
||||
public Int64 capacity;
|
||||
public long current_length;
|
||||
public long capacity;
|
||||
|
||||
long IStatusUpdateStatistics.current_length => current_length;
|
||||
long IStatusUpdateStatistics.capacity => capacity;
|
||||
}
|
||||
|
||||
public class QueueStatusUpdateData
|
||||
@@ -24,20 +31,34 @@ public class QueueStatusUpdateData
|
||||
public QueueStatusUpdateDatastatistics statistics;
|
||||
}
|
||||
|
||||
public class QueueStatusUpdateDatastatistics
|
||||
public class QueueStatusUpdateDatastatistics : IStatusUpdateStatistics
|
||||
{
|
||||
public Int64 current_length;
|
||||
public Int64 capacity;
|
||||
public long current_length;
|
||||
public long capacity;
|
||||
|
||||
long IStatusUpdateStatistics.current_length => current_length;
|
||||
long IStatusUpdateStatistics.capacity => capacity;
|
||||
}
|
||||
|
||||
public enum StoreBehavior
|
||||
{
|
||||
FIFO,
|
||||
LIFO
|
||||
}
|
||||
#endregion
|
||||
|
||||
public class SimulationModelStore : SimulationModel
|
||||
{
|
||||
[Header("Configuration")]
|
||||
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>();
|
||||
|
||||
public List<GameObject> storedProducts = new List<GameObject>();
|
||||
public int maxCapacity;
|
||||
public StoreBehavior storeType = StoreBehavior.FIFO;
|
||||
|
||||
|
||||
|
||||
protected override void init()
|
||||
{
|
||||
base.init();
|
||||
@@ -46,124 +67,90 @@ public class SimulationModelStore : SimulationModel
|
||||
|
||||
public void StoreProduct(GameObject product)
|
||||
{
|
||||
if (product == null) return;
|
||||
|
||||
product.transform.parent = transform;
|
||||
for (int i = 0; i < storePositions.Count; i++)
|
||||
|
||||
Transform emptySlot = storePositions.FirstOrDefault(slot => slot.childCount == 0);
|
||||
if (emptySlot != null)
|
||||
{
|
||||
Transform storePos = storePositions[i];
|
||||
if (storePos.childCount == 0)
|
||||
{
|
||||
product.transform.parent = storePos;
|
||||
break;
|
||||
}
|
||||
product.transform.SetParent(emptySlot, false);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Transform GetTransporterPosition()
|
||||
{
|
||||
if (transporterPositions.Count == 0)
|
||||
if (transporterPositions == null || transporterPositions.Count == 0)
|
||||
return null;
|
||||
return transporterPositions[UnityEngine.Random.Range(0, transporterPositions.Count)];
|
||||
|
||||
int idx = UnityEngine.Random.Range(0, transporterPositions.Count);
|
||||
return transporterPositions[idx];
|
||||
}
|
||||
|
||||
void AdjustStore(StoreStatusUpdateData Store)
|
||||
private void AdjustToTarget(IStatusUpdateStatistics status)
|
||||
{
|
||||
if (status == null) return;
|
||||
|
||||
int current = storedProducts.Count;
|
||||
Int64 target = Store.statistics.current_length;
|
||||
long target = status.current_length;
|
||||
int capacity = storePositions.Count;
|
||||
|
||||
if (capacity <= 0)
|
||||
return;
|
||||
|
||||
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++)
|
||||
int availableSlots = capacity - current;
|
||||
int spawnCount = (int)Mathf.Min(target - current, availableSlots);
|
||||
for (int i = 0; i < spawnCount; i++)
|
||||
{
|
||||
Transform emptySlot = storePositions.FirstOrDefault(slot => slot.childCount == 0);
|
||||
if (emptySlot == null)
|
||||
break;
|
||||
|
||||
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++)
|
||||
for (int i = 0; i < removeCount; i++)
|
||||
{
|
||||
GameObject prod = (storeType == "fifo")
|
||||
? storedProducts[0]
|
||||
: storedProducts[storedProducts.Count - 1];
|
||||
GameObject prod = storeType == StoreBehavior.FIFO
|
||||
? storedProducts.FirstOrDefault()
|
||||
: storedProducts.LastOrDefault();
|
||||
|
||||
storedProducts.Remove(prod);
|
||||
Destroy(prod);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (prod == null)
|
||||
continue;
|
||||
|
||||
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();
|
||||
if (storeType == StoreBehavior.FIFO)
|
||||
storedProducts.RemoveAt(0);
|
||||
else
|
||||
storedProducts.RemoveAt(storedProducts.Count - 1);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -176,13 +163,13 @@ public class SimulationModelStore : SimulationModel
|
||||
{
|
||||
case "store_status_update":
|
||||
var storeStatus = JsonConvert.DeserializeObject<StoreStatusUpdateData>(wrapclass.data.ToString());
|
||||
AdjustStore(storeStatus);
|
||||
AdjustToTarget(storeStatus?.statistics);
|
||||
SetBubble(storeStatus.statistics.current_length);
|
||||
SetBubbleDetail_Store(storeStatus.statistics);
|
||||
break;
|
||||
case "queue_status_update":
|
||||
var queueStatus = JsonConvert.DeserializeObject<QueueStatusUpdateData>(wrapclass.data.ToString());
|
||||
AdjustQueue(queueStatus);
|
||||
AdjustToTarget(queueStatus?.statistics);
|
||||
SetBubble(queueStatus.statistics.current_length);
|
||||
SetBubbleDetail_Queue(queueStatus.statistics);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user