ND_SimulationAutomaticControl/Assets/Scripts/Line/LineManager.cs

221 lines
8.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Tweening;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using TMPro;
using UnityEngine;
using UnityEngine.Tilemaps;
public class LineManager : MonoBehaviour
{
public static LineManager instance;
public Transform point1;
public Transform point2;
public GameObject Tips;
public TextMeshProUGUI TipTexts;
public List<GameObject> Models = new List<GameObject>();
LineData lineData = new LineData();
private void Awake()
{
instance = this;
}
void Start()
{
Initialization();
}
private void Initialization()
{
if (PlayerPrefs.GetString("LineData") != "")
{
lineData = JsonConvert.DeserializeObject<LineData>(PlayerPrefs.GetString("LineData"));
for (int i = 0; i < lineData.Modelname.Count; i++)
{
for (int j = 0; j < Models.Count; j++)
{
if (lineData.Modelname[i].Contains(Models[j].name))
{
Models[j].SetActive(true);
}
if (lineData.Modelname[i].Equals(Models[j].name) && Models[j].name.Equals("diannaoxian"))
{
Models[j].transform.DOLocalMove(new Vector3(-0.56f, -0.293f, -0.163f), 0.5f);
}
}
}
}
}
void Update()
{
if (Input.GetMouseButtonDown(2))
{
point1 = null;
point2 = null;
PlayerPrefs.SetString("LineData", "");
}
}
/// <summary>
/// Êó±êµã»÷µÄÎïÌå
/// </summary>
/// <param name="point"></param>
public void ClickPoint(Transform point)
{
//if (point1 != null)
//{
// point2 = point;
//}
//if (point1 == null)
//{
// point1 = point;
//}
//if (point1 != null && point2 != null)
//{
// //StartCoroutine(Checkconnection(point1, point2));
//}
if (point.name.Equals("diannaoxian"))
{
if (!lineData.Modelname.Contains(point.name))
{
lineData.Modelname.Add(point.name);
string json = JsonConvert.SerializeObject(lineData);
PlayerPrefs.SetString("LineData", json);
}
point.DOLocalMove(new Vector3(-0.56f, -0.293f, -0.163f), 0.5f);
}
}
public IEnumerator Checkconnection(Transform transform, Transform transform2)
{
LineConnect line = transform.GetComponent<LineConnect>();
LineConnect line2 = transform2.GetComponent<LineConnect>();
if (line != null && line2 != null)
{
if (!JudgmentLine(line, line2))
{
Tips.SetActive(true);
yield return new WaitForSeconds(2);
Tips.SetActive(false);
}
}
}
public bool JudgmentLine(LineConnect line1, LineConnect line2)
{
Init();
if (line1.interfaceType == InterfaceType.¸º12V && line2.interfaceType != InterfaceType.¸º12V ||
line2.interfaceType == InterfaceType.¸º12V && line1.interfaceType != InterfaceType.¸º12V)
{
TipTexts.text = "Á¬½Ó´íÎó£¬-12VÓ¦¸ÃÓë-12VÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if (line1.interfaceType == InterfaceType.Õý12V && line2.interfaceType != InterfaceType.Õý12V
|| line2.interfaceType == InterfaceType.Õý12V && line1.interfaceType != InterfaceType.Õý12V
)
{
TipTexts.text = "Á¬½Ó´íÎó£¬12VÓ¦¸ÃÓë12VÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
//else if (line1.interfaceType == InterfaceType.ÁãV && line2.interfaceType != InterfaceType.GND
// || line2.interfaceType == InterfaceType.GND && line1.interfaceType != InterfaceType.ÁãV)
//{
// TipTexts.text = "Á¬½Ó´íÎó£¬0VÓ¦¸ÃÓëGNDÏàÁ¬£¡";
// // WireDrawingSystem.instance.currentState = WireDrawingSystem.DrawingState.Idle;
// //WireDrawingSystem.instance.HandleDelete();
// //WireDrawingSystem.instance.ClearSnapPreview();
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
//}
else if (line1.interfaceType == InterfaceType.IN2_200K && line2.interfaceType != InterfaceType.OUT1
|| line2.interfaceType == InterfaceType.IN2_200K && line1.interfaceType != InterfaceType.OUT1)
{
TipTexts.text = "Á¬½Ó´íÎó£¬200KÓ¦¸ÃÓëOUT1ÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if (line1.interfaceType == InterfaceType.OUT1 && (line2.interfaceType != InterfaceType.IN4 && line2.interfaceType != InterfaceType.IN2_200K)
|| line2.interfaceType == InterfaceType.OUT1 && line1.interfaceType != InterfaceType.IN4)
{
TipTexts.text = "Á¬½Ó´íÎó£¬OUT1Ó¦¸ÃÓëIN4ÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if (line1.interfaceType == InterfaceType.AD1 && line2.interfaceType != InterfaceType.OUT2
|| line2.interfaceType == InterfaceType.AD1 && line1.interfaceType != InterfaceType.OUT2)
{
TipTexts.text = "Á¬½Ó´íÎó£¬AD1Ó¦¸ÃÓëOUT2ÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if ((line1.interfaceType == InterfaceType.ÁãV && (line2.interfaceType != InterfaceType.GND && line2.interfaceType != InterfaceType.III)
|| line2.interfaceType == InterfaceType.GND && line1.interfaceType != InterfaceType.ÁãV)
)
{
TipTexts.text = "Á¬½Ó´íÎó£¬0VÓ¦¸ÃÓëGNDÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if (line1.interfaceType == InterfaceType.OUT && line2.interfaceType != InterfaceType.IN1
|| line2.interfaceType == InterfaceType.OUT && line1.interfaceType != InterfaceType.IN1)
{
TipTexts.text = "Á¬½Ó´íÎó£¬OUTÓ¦¸ÃÓëIN1ÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if (line1.interfaceType == InterfaceType.AD2 && line2.interfaceType != InterfaceType.IN1
|| line2.interfaceType == InterfaceType.AD2 && line1.interfaceType != InterfaceType.IN1)
{
TipTexts.text = "Á¬½Ó´íÎó£¬AD2Ó¦¸ÃÓëIN1ÏàÁ¬£¡";
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else if (line1.IsNotLine == true || line2.IsNotLine == true)
{
TipTexts.text = "Á¬½Ó´íÎ󣬲»ÐèÒª´ËÁ¬Ïß";
// WireDrawingSystem.instance.currentState = WireDrawingSystem.DrawingState.Idle;
//WireDrawingSystem.instance.HandleDelete();
//WireDrawingSystem.instance.ClearSnapPreview();
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return false;
}
else
{
for (int i = 0; i < Models.Count; i++)
{
string[] splitname = Models[i].name.Split('Á¬');
if (splitname[0] == line1.interfaceType.ToString() && splitname[1] == line2.interfaceType.ToString() ||
splitname[0] == line2.interfaceType.ToString() && splitname[1] == line1.interfaceType.ToString())
{
if (!lineData.Modelname.Contains(Models[i].name))
{
lineData.Modelname.Add(Models[i].name);
string json = JsonConvert.SerializeObject(lineData);
PlayerPrefs.SetString("LineData", json);
}
Models[i].SetActive(true);
}
}
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return true;
}
}
public void Init()
{
point2 = null;
point1 = null;
}
}
public class LineData
{
public List<string> Modelname = new List<string>();
}