U3D_TobaccoWarehouseISMDTSy.../Assets/Scripts/YL/Carinformation.cs

286 lines
11 KiB
C#

using DefaultNamespace;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using UnityEngine;
using static InterfaceManager;
public class Carinformation : MonoBehaviour
{
/// <summary>
/// 小车ID
/// </summary>
public string ID;
/// <summary>
/// 小车名字
/// </summary>
public string Name;
/// <summary>
/// 需要渲染的箱子0
/// </summary>
public GameObject BoxID;
/// <summary>
/// 普通实箱
/// </summary>
public Material Commonfruitbox;
/// <summary>
/// 普通空箱
/// </summary>
public Material Normalairbox;
/// <summary>
/// 特殊实箱
/// </summary>
public Material Specialkernelbox;
/// <summary>
/// 特殊空箱
/// </summary>
public Material Specialbox;
/// <summary>
/// 是否初始化
/// </summary>
private bool isInit = false;
/// <summary>
/// 小车速度
/// </summary>
public float Speed = 3.5f;
void Start()
{
}
void Update()
{
}
/// <summary>
/// 点位,初始发,是否装箱子,是否卸箱子
/// </summary>
/// <param name="carequipment"></param>
public void Setbox(Carequipment carequipment)
{
string pointName = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer;
if (MqttManager.Instance.GetTransformByName(pointName))
{
Transform currentPoint = MqttManager.Instance.GetTransformByName(pointName);
if (currentPoint && !isInit)
{
SetPoint(currentPoint);
if (carequipment.CarState_CarPlt == "2" && currentPoint.childCount == 0 && transform.childCount == 0 && carequipment.JobState_BoxCode != "0")
{
Initbox(carequipment.JobState_BoxCode, carequipment.CarState_Car_Layer);
}
isInit = true;
}
else
{
Designateddisplacement(currentPoint.position, Speed);
}
if (carequipment.CarState_CarPlt == "2")
{
Encasement(currentPoint);
}
else if (carequipment.CarState_CarPlt == "1")
{
UnloadBox(currentPoint);
}
}
}
/// <summary>
/// 小车移动动画
/// </summary>
/// <param name="dis"></param>
/// <param name="speed"></param>
public void Designateddisplacement(Vector3 dis, float speed)
{
Debug.Log("小车运动了");
float times = Vector3.Distance(transform.position, dis) / speed;
transform.DOMove(dis, times).SetEase(Ease.InOutQuad);
}
/// <summary>
/// 初始化赋值
/// </summary>
/// <param name="point"></param>
public void SetPoint(Transform point)
{
transform.position = point.position;
}
/// <summary>
/// 初始化箱子把箱子放到小车上并判断生成箱子颜色
/// </summary>
/// <param name="boxdata"></param>
public void Initbox(string boxdata, string layer)
{
string url = Initializes + boxdata;
Debug.Log("箱子ID" + boxdata);
Debug.Log("箱子请求路径" + url);
StartCoroutine(Getstring(url, (data) =>
{
Debug.Log("初始发对应的箱子数据" + data);
ShelfBoxModel shelfBoxModel = JsonUtility.FromJson<ShelfBoxModel>(data);
if (shelfBoxModel != null && BoxID)
{
GameObject box = Instantiate(BoxID, transform);
Boxinformation boxinformation = box.AddComponent<Boxinformation>();
if (boxinformation != null)
{
boxinformation.ID = shelfBoxModel.result[0].id;
boxinformation.type = shelfBoxModel.result[0].type;
boxinformation.locationId = shelfBoxModel.result[0].locationId;
boxinformation.description = shelfBoxModel.result[0].description;
boxinformation.locationState = shelfBoxModel.result[0].locationState;
boxinformation.storageState = shelfBoxModel.result[0].storageState;
boxinformation.layer = shelfBoxModel.result[0].layer;
boxinformation.row = shelfBoxModel.result[0].row;
boxinformation.column = shelfBoxModel.result[0].column;
boxinformation.specialFlag = shelfBoxModel.result[0].specialFlag;
boxinformation.palletNum = shelfBoxModel.result[0].palletNum;
boxinformation.itemType = shelfBoxModel.result[0].itemType;
boxinformation.isSpecial = shelfBoxModel.result[0].isSpecial;
switch (shelfBoxModel.result[0].isSpecial)
{
case 0:
if (shelfBoxModel.result[0].itemType == "EmptyBox")
{
Renderer renderer = box.GetComponent<Renderer>();
Renderer renderer1 = box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer != null)
{
renderer.material = Specialbox;
}
if (renderer1 != null)
{
renderer1.material = Specialbox;
}
}
else
{
Renderer renderer2 = box.GetComponent<Renderer>();
Renderer renderer3 = box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer2 != null)
{
renderer2.material = Specialkernelbox;
}
if (renderer3 != null)
{
renderer3.material = Specialkernelbox;
}
}
break;
case 1:
if (shelfBoxModel.result[0].itemType == "EmptyBox")
{
Renderer renderer = box.GetComponent<Renderer>();
Renderer renderer1 = box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer != null)
{
renderer.material = Normalairbox;
}
if (renderer1 != null)
{
renderer1.material = Normalairbox;
}
}
else
{
Renderer renderer2 = box.GetComponent<Renderer>();
Renderer renderer3 = box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer2 != null)
{
renderer2.material = Commonfruitbox;
}
if (renderer3 != null)
{
renderer3.material = Commonfruitbox;
}
}
break;
default:
break;
}
Collider triggerCollider = GetComponent<Collider>();
// 确保触发器物体有碰撞体
if (triggerCollider != null)
{
// 计算触发器物体顶部的世界坐标
Vector3 triggerPosition = triggerCollider.bounds.center;
Vector3 triggerTop = triggerPosition + Vector3.up * (triggerCollider.bounds.extents.y);
// 设置物体的位置
box.gameObject.transform.localEulerAngles = new Vector3(90, 0, 0);
box.gameObject.transform.position = triggerTop;
box.gameObject.transform.SetParent(transform, false);
Debug.Log("箱子位置初始化");
MqttManager.Instance.Deletebox(boxinformation.ID, boxinformation);
}
else
{
Debug.LogError("触发器没有碰撞体");
}
}
if (layer.Equals(1))
{
GameObject childbox = box.transform.GetChild(0).gameObject;
MqttManager.Instance.firstbox.Add(childbox);
MqttManager.Instance.firstbox.Add(box);
}
else
{
GameObject childbox = box.transform.GetChild(0).gameObject;
MqttManager.Instance.secondbox.Add(childbox);
MqttManager.Instance.secondbox.Add(box);
}
}
}));
}
/// <summary>
/// 执行上箱
/// </summary>
/// <param name="point"></param>
public void Encasement(Transform point)
{
if (point.childCount > 0)
{
Collider collider = point.GetChild(0).gameObject.GetComponent<Collider>();
if (collider != null)
{
collider.isTrigger = true;
}
}
}
/// <summary>
/// 执行卸箱
/// </summary>
public void UnloadBox(Transform point)
{
if (transform.childCount > 0)
{
GameObject box = transform.GetChild(0).gameObject;
if (box != null)
{
box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.2f).SetEase(Ease.InOutQuad).OnComplete(() =>
{
if (box.GetComponent<Collider>())
{
Collider collider = box.GetComponent<Collider>();
if (collider != null)
{
collider.isTrigger = false;
box.gameObject.transform.SetParent(null);
}
box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 0.6f).SetEase(Ease.InOutQuad).OnComplete(() =>
{
if (point)
{
box.gameObject.transform.SetParent(point);
}
});
}
});
}
}
}
}