move
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Events;
|
||||
using Octopus.Simulator.Networks;
|
||||
using Newtonsoft.Json;
|
||||
using Octopus.Simulator;
|
||||
|
||||
#region class
|
||||
[Serializable]
|
||||
public class MoveDataClass_start
|
||||
{
|
||||
@@ -142,6 +141,7 @@ public class statisticsMove
|
||||
{
|
||||
public int total_moved;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public class SimulationModelMove : SimulationModel
|
||||
{
|
||||
@@ -150,131 +150,111 @@ public class SimulationModelMove : SimulationModel
|
||||
string eventUnloading = "move_unloading";
|
||||
string eventSpeed = "move_speed_factor_applied";
|
||||
string eventReturning = "move_returning";
|
||||
|
||||
[Header("Transport")]
|
||||
public List<GameObject> listProducts = new List<GameObject>();
|
||||
public Transform productPos;
|
||||
public Vector3 productDistance = new Vector3(0, 0.321f, 0);
|
||||
public UnityEvent onMove;
|
||||
public UnityEvent onWait;
|
||||
|
||||
int productCount = 0;
|
||||
int currentCount = 0;
|
||||
int totalMoved = 0;
|
||||
bool isFirstTransport = true;
|
||||
|
||||
Transform origin = null;
|
||||
public Transform destination = null;
|
||||
|
||||
public float arrivalTime = 0;
|
||||
public float elapsedTime = 0;
|
||||
|
||||
Vector3 originalPos;
|
||||
Vector3 prevPos;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (simulationUI.isplaying)
|
||||
if (!simulationUI.isplaying)
|
||||
return;
|
||||
|
||||
UpdateProductStacking();
|
||||
UpdateMovement();
|
||||
}
|
||||
|
||||
private void UpdateProductStacking()
|
||||
{
|
||||
if (productCount != listProducts.Count)
|
||||
{
|
||||
int interval = 0;
|
||||
if (productCount != listProducts.Count)
|
||||
productCount = listProducts.Count;
|
||||
// 단순하게 모두 같은 offset으로 배치 (필요 시 i 기반 offset으로 확장)
|
||||
foreach (var gb in listProducts)
|
||||
{
|
||||
productCount = listProducts.Count;
|
||||
for (int i = 0; i < productCount; i++)
|
||||
if (gb != null)
|
||||
{
|
||||
GameObject gb = listProducts[i];
|
||||
gb.transform.localPosition = productDistance;
|
||||
interval++;
|
||||
}
|
||||
}
|
||||
if (destination != null)
|
||||
{
|
||||
elapsedTime += Time.deltaTime * WebParameters.speed;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isFirstTransport = true;
|
||||
private void UpdateMovement()
|
||||
{
|
||||
if (destination == null)
|
||||
return;
|
||||
|
||||
elapsedTime += Time.deltaTime * WebParameters.speed;
|
||||
|
||||
if (arrivalTime <= 0f)
|
||||
return;
|
||||
|
||||
float t = elapsedTime / arrivalTime;
|
||||
if (t > 1f)
|
||||
{
|
||||
transform.position = destination.position;
|
||||
transform.forward = destination.forward;
|
||||
destination = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3 newPos = Vector3.Lerp(originalPos, destination.position, t);
|
||||
transform.position = newPos;
|
||||
|
||||
Vector3 dir = newPos - prevPos;
|
||||
dir.y = 0;
|
||||
if (dir.sqrMagnitude > 0.0001f)
|
||||
{
|
||||
dir.Normalize();
|
||||
transform.forward = dir;
|
||||
}
|
||||
|
||||
prevPos = newPos;
|
||||
}
|
||||
}
|
||||
|
||||
public override void GetData(string data)
|
||||
{
|
||||
var wrapclass = JsonConvert.DeserializeObject<SimulationDefaultJson>(data);
|
||||
|
||||
if (wrapclass._event.Contains(eventLoading))
|
||||
if (wrapclass == null || string.IsNullOrEmpty(wrapclass._event))
|
||||
return;
|
||||
/*
|
||||
string eventLoading = "move_loading";
|
||||
string eventMove = "move_moving";
|
||||
string eventUnloading = "move_unloading";
|
||||
string eventSpeed = "move_speed_factor_applied";
|
||||
string eventReturning = "move_returning";
|
||||
*/
|
||||
switch (wrapclass._event)
|
||||
{
|
||||
var moveData_loading = JsonConvert.DeserializeObject<MoveDataClass_loading>(wrapclass.data.ToString());
|
||||
if (moveData_loading.source_queues != null && (moveData_loading.source_queues.Count >= 1))
|
||||
{
|
||||
string queueID = moveData_loading.source_queues[0].ToString();
|
||||
SimulationModel model = DataManager.I.GetModel(queueID);
|
||||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||||
currentCount = moveData_loading.loaded_count;
|
||||
SetBubble(currentCount);
|
||||
|
||||
|
||||
if ( isFirstTransport)
|
||||
{
|
||||
SimulationModelStore source = DataManager.I.GetModel(moveData_loading.source_queues[0]) as SimulationModelStore;
|
||||
this.transform.position = source.GetTransporterPosition().position;
|
||||
|
||||
isFirstTransport = false;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < moveData_loading.loaded_count; i++)
|
||||
{
|
||||
GameObject product = null;
|
||||
product = ProductManager.Instance.SpawnProduct();
|
||||
if (product != null)
|
||||
{
|
||||
product.transform.parent = productPos;
|
||||
product.transform.localPosition = Vector3.zero;
|
||||
product.transform.localRotation = Quaternion.identity;
|
||||
listProducts.Add(product);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (moveData_loading.source_stores != null && (moveData_loading.source_stores.Count >= 1))
|
||||
{
|
||||
string storeID = moveData_loading.source_stores[0].ToString();
|
||||
SimulationModel model = DataManager.I.GetModel(storeID);
|
||||
SimulationModelStore storeModel = (SimulationModelStore)model;
|
||||
currentCount = moveData_loading.loaded_count;
|
||||
SetBubble(currentCount);
|
||||
|
||||
if ( isFirstTransport)
|
||||
{
|
||||
SimulationModelStore source = DataManager.I.GetModel(moveData_loading.source_stores[0]) as SimulationModelStore;
|
||||
this.transform.position = source.GetTransporterPosition().position;
|
||||
|
||||
isFirstTransport = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < moveData_loading.loaded_count; i++)
|
||||
{
|
||||
GameObject product = null;
|
||||
product = ProductManager.Instance.SpawnProduct();
|
||||
if (product != null)
|
||||
{
|
||||
product.transform.parent = productPos;
|
||||
product.transform.localPosition = Vector3.zero;
|
||||
product.transform.localRotation = Quaternion.identity;
|
||||
listProducts.Add(product);
|
||||
}
|
||||
}
|
||||
}
|
||||
case "move_loading":
|
||||
LoadingCargo(wrapclass.data.ToString());
|
||||
break;
|
||||
case "move_moving":
|
||||
Moving(wrapclass.data.ToString());
|
||||
break;
|
||||
case "move_unloading":
|
||||
break;
|
||||
case "move_speed_factor_applied":
|
||||
break;
|
||||
case "move_returning":
|
||||
break;
|
||||
}
|
||||
if (wrapclass._event.Contains(eventMove))
|
||||
{
|
||||
@@ -393,6 +373,93 @@ public class SimulationModelMove : SimulationModel
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadingCargo(string data)
|
||||
{
|
||||
var loading = JsonConvert.DeserializeObject<MoveDataClass_loading>(data);
|
||||
if (loading == null)
|
||||
return;
|
||||
|
||||
currentCount = loading.loaded_count;
|
||||
SetBubble(currentCount);
|
||||
|
||||
// 처음 운송이면 출발 위치 세팅
|
||||
if (isFirstTransport)
|
||||
{
|
||||
string sourceId = GetFirstNonEmpty(loading.source_queues, loading.source_stores);
|
||||
if (!string.IsNullOrEmpty(sourceId))
|
||||
{
|
||||
var sourceModel = DataManager.I.GetModel(sourceId) as SimulationModelStore;
|
||||
if (sourceModel != null)
|
||||
{
|
||||
transform.position = sourceModel.GetTransporterPosition()?.position ?? transform.position;
|
||||
}
|
||||
}
|
||||
isFirstTransport = false;
|
||||
}
|
||||
|
||||
AddProducts(loading.loaded_count);
|
||||
}
|
||||
|
||||
private void Moving(string data)
|
||||
{
|
||||
var moveData = JsonConvert.DeserializeObject<MoveDataClass_Move_Moving>(data);
|
||||
if (moveData == null)
|
||||
return;
|
||||
|
||||
elapsedTime = 0f;
|
||||
arrivalTime = moveData.time;
|
||||
originalPos = transform.position;
|
||||
prevPos = transform.position;
|
||||
|
||||
// origin 설정 (입력 큐 / 스토어)
|
||||
string inputId = GetFirstNonEmpty(moveData.input?.queues, moveData.input?.stores);
|
||||
if (!string.IsNullOrEmpty(inputId))
|
||||
{
|
||||
var model = DataManager.I.GetModel(inputId) as SimulationModelStore;
|
||||
if (model != null)
|
||||
origin = model.GetTransporterPosition();
|
||||
}
|
||||
|
||||
// destination 설정 (출력 큐 / 스토어)
|
||||
if (!string.IsNullOrEmpty(moveData.output?.queue))
|
||||
{
|
||||
var model = DataManager.I.GetModel(moveData.output.queue) as SimulationModelStore;
|
||||
if (model != null)
|
||||
destination = model.GetTransporterPosition();
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(moveData.output?.store))
|
||||
{
|
||||
var model = DataManager.I.GetModel(moveData.output.store) as SimulationModelStore;
|
||||
if (model != null)
|
||||
destination = model.GetTransporterPosition();
|
||||
}
|
||||
}
|
||||
|
||||
private string GetFirstNonEmpty(List<string> listA, List<string> listB)
|
||||
{
|
||||
if (listA != null && listA.Count > 0)
|
||||
return listA[0];
|
||||
if (listB != null && listB.Count > 0)
|
||||
return listB[0];
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private void AddProducts(int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var product = ProductManager.Instance.SpawnProduct();
|
||||
if (product == null)
|
||||
continue;
|
||||
|
||||
product.GetComponent<SimulationModelProduct>().SetParent(this.nodeID);
|
||||
product.transform.parent = productPos;
|
||||
product.transform.localPosition = Vector3.zero;
|
||||
product.transform.localRotation = Quaternion.identity;
|
||||
listProducts.Add(product);
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetBubble(object data)
|
||||
{
|
||||
string msg = $"{currentCount}/{totalMoved}";
|
||||
|
||||
Reference in New Issue
Block a user