U3D_TobaccoWarehouseISMDTSy.../Assets/Scripts/ExplodeOnClick.cs

483 lines
15 KiB
C#

using DefaultNamespace;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
public enum Equipment
{
Battery,
Car,
Hoister,
Hoister2
}
public class ExplodeOnClick : MonoBehaviour
{
public Equipment equipment = Equipment.Battery;
public GameObject parentObject;
[Header("换电池设备")]
public GameObject batteryobj;
[Header("穿梭车")]
public GameObject carobj;
[Header("提升机1")]
public GameObject hoister;
[Header("提升机2")]
public GameObject hoister2;
[Header("需要生成的位置")]
public Transform point;
/// <summary>
/// 展示对应设备
/// </summary>
public List<GameObject> games = new List<GameObject>();
private Vector3[] originalPositions; // 存储原始位置
private bool isExploded = false; // 标识是否已爆炸
public List<Vector3> points;
public List<GameObject> Replacebattery = new List<GameObject>();
private bool isp = false;
private GameObject Opjgame;
public List<Vector3> colidpoints;
public bool Blowups = false;
private void Awake()
{
parentObject = Instantiate(carobj, point);
for (int i = 0; i < parentObject.transform.childCount; i++)
{
points.Add(parentObject.transform.GetChild(i).localPosition);
Replacebattery.Add(parentObject.transform.GetChild(i).gameObject);
}
}
/// <summary>
/// 展示对应设备
/// </summary>
public void Showshebi(string name)
{
if (games.Count > 0)
{
for (int i = 0; i < games.Count; i++)
{
if (games[i].name == name)
{
games[i].SetActive(true);
}
else
{
games[i].SetActive(false);
}
}
}
}
/// <summary>
/// 清理设备
/// </summary>
/// <param name="game"></param>
/// <param name="vectors"></param>
public void Deletes(List<GameObject> game, List<Vector3> vectors)
{
if (game.Count > 0)
{
for (int i = 0; i < game.Count; i++)
{
if (game[i].gameObject)
{
game.Remove(game[i].gameObject);
}
}
game.Clear();
}
if (vectors.Count > 0)
{
vectors.Clear();
}
}
/// <summary>
/// 换电池方法
/// </summary>
public void Battery()
{
if (Equipment.Battery != equipment)
{
if (parentObject)
{
Destroy(parentObject);
isExploded = false;
isp = false;
parentObject = Instantiate(batteryobj, point);
Deletes(Replacebattery, points);
for (int i = 0; i < parentObject.transform.childCount; i++)
{
points.Add(parentObject.transform.GetChild(i).localPosition);
Replacebattery.Add(parentObject.transform.GetChild(i).gameObject);
}
equipment = Equipment.Battery;
// Showshebi(parentObject.name+ "(Clone)");
}
}
else
{
Debug.Log("相同设备不需要切换");
}
}
/// <summary>
/// 穿梭车
/// </summary>
public void Shuttlebus()
{
if (Equipment.Car != equipment)
{
if (parentObject)
{
Destroy(parentObject);
isExploded = false;
isp = false;
parentObject = Instantiate(carobj, point);
Deletes(Replacebattery, points);
for (int i = 0; i < parentObject.transform.childCount; i++)
{
points.Add(parentObject.transform.GetChild(i).localPosition);
Replacebattery.Add(parentObject.transform.GetChild(i).gameObject);
}
equipment = Equipment.Car;
//Showshebi(parentObject.name+ "(Clone)");
}
}
else
{
Debug.Log("相同设备不需要切换");
}
}
/// <summary>
/// 提升机1
/// </summary>
public void Hoister()
{
if (Equipment.Hoister != equipment)
{
if (parentObject)
{
Destroy(parentObject);
isExploded = false;
isp = false;
parentObject = Instantiate(hoister, point);
Deletes(Replacebattery, points);
for (int i = 0; i < parentObject.transform.childCount; i++)
{
points.Add(parentObject.transform.GetChild(i).localPosition);
Replacebattery.Add(parentObject.transform.GetChild(i).gameObject);
}
equipment = Equipment.Hoister;
// Showshebi(parentObject.name + "(Clone)");
}
}
else
{
Debug.Log("相同设备不需要切换");
}
}
/// <summary>
/// 提升机2
/// </summary>
public void Hoister2()
{
if (Equipment.Hoister2 != equipment)
{
if (parentObject)
{
Destroy(parentObject);
isExploded = false;
isp = false;
parentObject = Instantiate(hoister2, point);
Deletes(Replacebattery, points);
for (int i = 0; i < parentObject.transform.childCount; i++)
{
points.Add(parentObject.transform.GetChild(i).localPosition);
Replacebattery.Add(parentObject.transform.GetChild(i).gameObject);
}
equipment = Equipment.Hoister2;
// Showshebi(parentObject.name + "(Clone)");
}
}
else
{
Debug.Log("相同设备不需要切换");
}
}
void Update()
{
if (Input.GetMouseButtonDown(0) && isExploded) // 鼠标左键点击
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (Namedata(hit.collider.gameObject.transform.parent.name))
{
Debug.Log("有相同名字");
Debug.Log(hit.collider.gameObject.transform.parent.name);
Showdata(hit.collider.gameObject.transform.parent.name);
Opjgame = hit.collider.gameObject.transform.parent.gameObject;
Blowups = true;
Debug.Log("小图爆炸" + Blowups);
Application.ExternalCall("modelStatus", Judgment());
}
}
}
}
public bool Judgment()
{
return Blowups;
}
/// <summary>
/// 激活所有父物体
/// </summary>
public void Show()
{
if (Replacebattery.Count > 0)
{
for (int i = 0; i < Replacebattery.Count; i++)
{
Replacebattery[i].gameObject.SetActive(true);
}
}
}
/// <summary>
/// 子物体实现爆炸效果
/// </summary>
public void Blowup()
{
if (!isp && Opjgame)
{
foreach (Transform child in Opjgame.transform)
{
colidpoints.Add(child.localPosition);
//float moveDistance = CalculateMoveDistance(2);
float moveDistance = 0;
if (equipment == Equipment.Hoister2)
{
moveDistance = Opjgame.GetComponent<GameDisComponent>().value / 100;
}
else
{
moveDistance = Opjgame.GetComponent<GameDisComponent>().value;
}
Vector3 targetPosition = GetTargetPosition2(child.transform, moveDistance);
StartCoroutine(MoveToPosition(child, targetPosition));
}
isp = true;
}
}
/// <summary>
/// 还原子物体
/// </summary>
public void Restore()
{
if (isp && Opjgame)
{
for (int i = 0; i < Opjgame.transform.childCount; i++)
{
Transform child = Opjgame.transform.GetChild(i);
StartCoroutine(MoveToPosition(child, colidpoints[i]));
}
isp = false;
StartCoroutine(Bulkreduction());
Blowups = false;
Debug.Log("小车还原" + Blowups);
}
}
public void Explode()
{
if (!isExploded)
{
foreach (Transform child in parentObject.transform)
{
float moveDistance = CalculateMoveDistance(0); // 假设为第一层
Vector3 targetPosition = GetTargetPosition(child, moveDistance);
// 这里可以实现动画效果,比如使用协程来移动物体
StartCoroutine(MoveToPosition(child, targetPosition));
}
isExploded = true; // 标记为已爆炸
}
}
public void ResetPositions() // 复原位置的方法
{
if (isExploded)
{
for (int i = 0; i < parentObject.transform.childCount; i++)
{
Transform child = parentObject.transform.GetChild(i);
StartCoroutine(MoveToPosition(child, points[i]));
}
isExploded = false; // 标记为未爆炸
}
}
private IEnumerator MoveToPosition(Transform child, Vector3 targetPosition)
{
float elapsedTime = 0f;
Vector3 startingPosition = child.localPosition;
while (elapsedTime < 1f) // 1秒内完成移动
{
child.localPosition = Vector3.Lerp(startingPosition, targetPosition, elapsedTime);
elapsedTime += Time.deltaTime;
yield return null; // 等待下一帧
}
child.localPosition = targetPosition; // 确保最终位置
}
private Vector3 GetTargetPosition(Transform child, float distance)
{
string direction = child.GetComponent<ModelChilderComponent>()?.Direction;
float dis = child.GetComponent<GameDisComponent>().value;
switch (direction)
{
case "前":
return child.localPosition + new Vector3(0, 0, dis);
case "后":
return child.localPosition + new Vector3(0, 0, -dis);
case "左":
return child.localPosition + new Vector3(-dis, 0, 0);
case "右":
return child.localPosition + new Vector3(dis, 0, 0);
case "上":
return child.localPosition + new Vector3(0, dis, 0);
case "下":
return child.localPosition + new Vector3(0, -dis, 0);
case "前上":
return child.localPosition + new Vector3(0, dis, dis);
case "前下":
return child.localPosition + new Vector3(0, -dis, dis);
case "后上":
return child.localPosition + new Vector3(0, dis, -dis);
case "后下":
return child.localPosition + new Vector3(0, -dis, -dis);
case "左上":
return child.localPosition + new Vector3(-dis, dis, 0);
case "左下":
return child.localPosition + new Vector3(-dis, -dis, 0);
case "右上":
return child.localPosition + new Vector3(dis, dis, 0);
case "右下":
return child.localPosition + new Vector3(dis, -dis, 0);
default:
return child.localPosition; // 默认不移动
}
}
private Vector3 GetTargetPosition2(Transform child, float distance)
{
string direction = child.GetComponent<ModelChilderComponent>()?.Direction;
//float dis=child.GetComponent<GameDisComponent>().value;
switch (direction)
{
case "前":
return child.localPosition + new Vector3(0, 0, distance);
case "后":
return child.localPosition + new Vector3(0, 0, -distance);
case "左":
return child.localPosition + new Vector3(-distance, 0, 0);
case "右":
return child.localPosition + new Vector3(distance, 0, 0);
case "上":
return child.localPosition + new Vector3(0, distance, 0);
case "下":
return child.localPosition + new Vector3(0, -distance, 0);
case "前上":
return child.localPosition + new Vector3(0, distance, distance);
case "前下":
return child.localPosition + new Vector3(0, -distance, distance);
case "后上":
return child.localPosition + new Vector3(0, distance, -distance);
case "后下":
return child.localPosition + new Vector3(0, -distance, -distance);
case "左上":
return child.localPosition + new Vector3(-distance, distance, 0);
case "左下":
return child.localPosition + new Vector3(-distance, -distance, 0);
case "右上":
return child.localPosition + new Vector3(distance, distance, 0);
case "右下":
return child.localPosition + new Vector3(distance, -distance, 0);
default:
return child.localPosition; // 默认不移动
}
}
IEnumerator Bulkreduction()
{
yield return new WaitForSeconds(1.1f);
if (Opjgame)
{
Opjgame = null;
}
if (colidpoints.Count > 0)
{
colidpoints.Clear();
}
if (Replacebattery.Count > 0)
{
for (int i = 0; i < Replacebattery.Count; i++)
{
Replacebattery[i].gameObject.SetActive(true);
}
}
}
private float CalculateMoveDistance(int level)
{
if (level == 0)
return 3; // 自定义第一层的移动距离
else if (level == 1)
return 0.1f; // 第二层的移动距离
else if (level == 2)
return 0.1f; // 第三层的移动距离
return 0f; // 更深层次不移动
}
/// <summary>
/// 判断链表是否有这个名字
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private bool Namedata(string name)
{
if (Replacebattery.Count > 0)
{
for (int i = 0; i < Replacebattery.Count; i++)
{
if (Replacebattery[i].name == name)
{
return true;
}
}
}
return false;
}
/// <summary>
/// 展示对应的子物体
/// </summary>
/// <param name="name"></param>
public void Showdata(string name)
{
if (Replacebattery.Count > 0)
{
for (int i = 0; i < Replacebattery.Count; i++)
{
if (Replacebattery[i].name == name)
{
Replacebattery[i].SetActive(true);
}
else
{
Replacebattery[i].SetActive(false);
}
}
}
}
}