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

316 lines
11 KiB
C#

using DefaultNamespace;
using DG.Tweening;
using HighlightPlus;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
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)
{
if (carequipment.CarState_Err01 != "" && carequipment.CarState_Err01 != "0")
{
HighlightEffect highlightEffect = transform.GetComponent<HighlightEffect>();
highlightEffect.highlighted = true;
}
else
{
HighlightEffect highlightEffect = transform.GetComponent<HighlightEffect>();
highlightEffect.highlighted = false;
}
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 == "1" && currentPoint.childCount == 0 && transform.childCount == 0 && carequipment.JobState_BoxCode.Length > 0)
{
//小车初始化
Initbox(carequipment.JobState_BoxCode, carequipment.CarState_Car_Layer);
}
isInit = true;
}
else
{
Designateddisplacement(currentPoint.position, Speed);
}
if (carequipment.CarState_CarPlt == "1" && isInit)
{
if (carequipment.JobState_BoxCode != "0")
{
Encasement(carequipment.JobState_BoxCode, currentPoint);
}
}
else if (carequipment.CarState_CarPlt == "2" && isInit)
{
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 urls = Tasknumber + boxdata;
StartCoroutine(Getstring(urls, (data) =>
{
//Debug.Log("返回数据" + data);
Initdata initdata = JsonUtility.FromJson<Initdata>(data);
if (initdata != null && BoxID)
{
GameObject box = Instantiate(BoxID, transform);
Boxtasknumber boxtasknumber = box.GetComponent<Boxtasknumber>();
boxtasknumber.ID = boxdata;
//Debug.Log("小车初始化" + boxtasknumber.ID);
switch (initdata.result[0].palletType)
{
case "Qbox":
if (initdata.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;
case "Box":
if (initdata.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;
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(string id, Transform point)
{
if (point.childCount > 0)
{
GameObject box = point.GetChild(0).gameObject;
if (true)
{
Boxtasknumber boxtasknumber = box.GetComponent<Boxtasknumber>();
Boxinformation boxinformation = box.GetComponent<Boxinformation>();
if (boxtasknumber && boxinformation)
{
Pointid(id, boxtasknumber, boxinformation);
}
}
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);
}
});
}
});
}
}
}
/// <summary>
/// 接收箱子身上id
/// </summary>
/// <param name="boxtasknumber"></param>
public void Pointid(string id, Boxtasknumber boxtasknumber, Boxinformation boxinformation)
{
string url = Tasknumber + id;
//Debug.Log("小车上箱子请求接口ID" + boxtasknumber.ID);
StartCoroutine(Getstring(url, (data) =>
{
//Debug.Log("小车上箱请求接口返回的数据" + data);
Initdata initdata = JsonUtility.FromJson<Initdata>(data);
boxinformation.locationId = initdata.result[0].dloc;
boxtasknumber.ID = null;
//Debug.Log("箱子上箱完毕小车身上箱子id" + boxtasknumber.ID + "小车身上locationId" + boxinformation.locationId);
}));
}
}