424 lines
8.7 KiB
C#
424 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using System.Linq;
|
|
using UnityEngine.UI;
|
|
|
|
public class MachineCoordinatesMovementScript : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
[Tooltip("XAxis Transform")]
|
|
Transform XAxis;
|
|
|
|
[SerializeField]
|
|
[Tooltip("YAxis Transform")]
|
|
Transform YAxis;
|
|
|
|
[SerializeField]
|
|
[Tooltip("ZAxis Transform")]
|
|
Transform ZAxis;
|
|
|
|
[SerializeField]
|
|
ControlButtonsScript ActionButtonsController;
|
|
|
|
[SerializeField]
|
|
Vector3 CurrentCords;
|
|
|
|
[SerializeField]
|
|
float CartXMaxSpeed = 1f;
|
|
float CartXSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
float CartYMaxSpeed = 1f;
|
|
float CartYSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
float CartZMaxSpeed = 1f;
|
|
float CartZSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
int MaterialsToInsert = 0;
|
|
|
|
[SerializeField]
|
|
bool TestProgramStart;
|
|
|
|
[SerializeField]
|
|
bool DebugStart = false;
|
|
|
|
[SerializeField]
|
|
bool RestartFlag = false;
|
|
|
|
// Ôëàã ðàáîòû, ïî íåìó ïðîâåðÿåòñÿ, âûïîëíÿåòñÿ ëè ñåé÷àñ êàêàÿ-ëèáî ïðîãðàììà
|
|
bool WorkInProgress = false;
|
|
bool PauseFlag = false;
|
|
bool ErrorFlag = false;
|
|
int StepToPause = -1;
|
|
|
|
[SerializeField]
|
|
List<bool> InsertedMaterials;
|
|
|
|
[SerializeField]
|
|
List<bool> InsertedReels;
|
|
|
|
public int InstalledReelCount => InsertedReels.Count(inserted => inserted);
|
|
public int AttachedTapeCount => InsertedMaterials.Count(inserted => inserted);
|
|
public bool IsWorking => WorkInProgress;
|
|
|
|
[SerializeField]
|
|
bool ReadyToWork = false;
|
|
|
|
bool ReelsReady = false;
|
|
|
|
[SerializeField]
|
|
List<Vector3> testProgr;
|
|
|
|
[SerializeField]
|
|
List<Vector3> PalmTreeProg;
|
|
|
|
[SerializeField]
|
|
List<Vector3> _ComponentsHoldersCords;
|
|
|
|
// Íîìåð îáúåêòà â CurrentProgram, ê êîòîðîìó äâèãàòüñÿ
|
|
static public int CurrentStep = 0 ;
|
|
static public List<Vector3> CurrentProgram { get; private set; }
|
|
static public List<Vector3> ComponentsHoldersCords { get; private set; }
|
|
|
|
[Serializable]
|
|
public class MachineStepEvent : UnityEvent<int> { }
|
|
public class MachineErrorEvent : UnityEvent<string> { }
|
|
|
|
public static MachineStepEvent OnStep = new();
|
|
|
|
public static MachineErrorEvent OnError = new();
|
|
|
|
// Ãðàíèöû ïî êîîðäèíàòàì: (0.435, 0.02, 0.4125)
|
|
|
|
[SerializeField]
|
|
GameObject Board;
|
|
|
|
void Start()
|
|
{
|
|
ComponentsHoldersCords = _ComponentsHoldersCords;
|
|
|
|
CurrentProgram = new(PalmTreeProg);
|
|
//DiagonalMoveDbg = false;
|
|
for (int i = 0; i < MaterialsToInsert; i++)
|
|
{
|
|
InsertedMaterials.Add(false);
|
|
InsertedReels.Add(false);
|
|
}
|
|
}
|
|
void FixedUpdate()
|
|
{
|
|
CurrentCords = new(XAxis.localPosition.x, YAxis.localPosition.y, ZAxis.localPosition.z);
|
|
|
|
if (DebugStart)
|
|
{
|
|
DebugStart = false;
|
|
StartWork(PalmTreeProg);
|
|
}
|
|
|
|
setActiveActionButtons();
|
|
|
|
if (RestartFlag == true)
|
|
{
|
|
if (MoveTo(Vector3.zero))
|
|
{
|
|
RestartFlag = false;
|
|
}
|
|
}
|
|
else if (WorkInProgress == true)
|
|
{
|
|
if (PauseFlag)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(CurrentStep <= CurrentProgram.Count - 1)
|
|
{
|
|
if (MoveTo(CurrentProgram[CurrentStep]))
|
|
{
|
|
if(YAxis.localPosition.y > 0) {
|
|
CurrentProgram[CurrentStep] = new(CurrentProgram[CurrentStep].x, 0, CurrentProgram[CurrentStep].z);
|
|
}
|
|
else
|
|
{
|
|
CurrentStep++;
|
|
OnStep?.Invoke(CurrentStep);
|
|
}
|
|
|
|
}
|
|
|
|
if(StepToPause != -1 && StepToPause == CurrentStep)
|
|
{
|
|
PauseFlag = true;
|
|
StepToPause = -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
resetFlags();
|
|
RestartFlag = true;
|
|
Board.GetComponent<BoardScript>().ComponentsPlaced();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setSpeedHor(Slider slider)
|
|
{
|
|
CartXSpeed = CartXMaxSpeed * (slider.value / 100);
|
|
CartZSpeed = CartZMaxSpeed * (slider.value / 100);
|
|
}
|
|
public void setSpeedVert(Slider slider)
|
|
{
|
|
CartYSpeed = CartYMaxSpeed * (slider.value / 100);
|
|
}
|
|
|
|
void setActiveActionButtons()
|
|
{
|
|
List<string> buttonsToActivate = new List<string>();
|
|
|
|
if (WorkInProgress)
|
|
{
|
|
buttonsToActivate.Add("Stop");
|
|
|
|
if (PauseFlag)
|
|
{
|
|
buttonsToActivate.Add("Run");
|
|
buttonsToActivate.Add("Step");
|
|
}
|
|
else
|
|
{
|
|
buttonsToActivate.Add("Pause");
|
|
}
|
|
}
|
|
else if (ErrorFlag)
|
|
{
|
|
buttonsToActivate.Add("Reset");
|
|
}
|
|
else
|
|
{
|
|
buttonsToActivate.Add("Run");
|
|
buttonsToActivate.Add("Step");
|
|
}
|
|
|
|
ActionButtonsController.SetActiveButtons(buttonsToActivate);
|
|
}
|
|
|
|
// Ôóíêöèÿ íà÷àëà ðàáîòû, ïðèíèìàåò ïðîãðàììó äëÿ ðàáîòû
|
|
public void StartWork(List<Vector3> program)
|
|
{
|
|
WorkInProgress = true;
|
|
RestartFlag = true;
|
|
|
|
CurrentProgram = new(program);
|
|
CurrentStep = 0;
|
|
OnStep?.Invoke(CurrentStep);
|
|
}
|
|
|
|
public void MaterialInserted(int index)
|
|
{
|
|
InsertedMaterials[index] = true;
|
|
|
|
foreach (bool mat in InsertedMaterials) {
|
|
if (!mat) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if(ReelsReady){
|
|
ReadyToWork = true;
|
|
}
|
|
}
|
|
|
|
public void ReelInserted(int index)
|
|
{
|
|
InsertedReels[index] = true;
|
|
|
|
foreach (bool reel in InsertedReels) {
|
|
if (!reel) {
|
|
return;
|
|
}
|
|
}
|
|
ReelsReady = true;
|
|
}
|
|
|
|
public void MaterialRemoved(int index)
|
|
{
|
|
InsertedMaterials[index] = false;
|
|
|
|
ReadyToWork = false;
|
|
}
|
|
|
|
public void ReelRemoved(int index)
|
|
{
|
|
InsertedReels[index] = false;
|
|
|
|
ReelsReady = false;
|
|
}
|
|
|
|
|
|
// Âîçâðàùàåò true, êîãäà íàõîäèòñÿ â êîîðäèíàòå, êîòîðóþ ñîîáùèëè
|
|
bool MoveTo(Vector3 cords)
|
|
{
|
|
float XMoveDistance = cords.x - XAxis.localPosition.x;
|
|
float YMoveDistance = cords.y - YAxis.localPosition.y;
|
|
float ZMoveDistance = cords.z - ZAxis.localPosition.z;
|
|
|
|
if (Mathf.Abs(XMoveDistance) < 0.0001f)
|
|
{
|
|
XMoveDistance = 0;
|
|
}
|
|
if (Mathf.Abs(YMoveDistance) < 0.0001f)
|
|
{
|
|
YMoveDistance = 0;
|
|
}
|
|
if (Mathf.Abs(ZMoveDistance) < 0.0001f)
|
|
{
|
|
ZMoveDistance = 0;
|
|
}
|
|
|
|
if (Mathf.Abs(XMoveDistance) > 0 || Mathf.Abs(ZMoveDistance) > 0)
|
|
{
|
|
float XStep = CartXSpeed * Time.deltaTime;
|
|
float XMoveAmount = Mathf.Min(XStep, Mathf.Abs(XMoveDistance));
|
|
|
|
float ZStep = CartZSpeed * Time.deltaTime;
|
|
float ZMoveAmount = Mathf.Min(ZStep, Mathf.Abs(ZMoveDistance));
|
|
|
|
|
|
if (XMoveDistance > 0)
|
|
{
|
|
XAxis.Translate(XMoveAmount, 0, 0);
|
|
}
|
|
else
|
|
{
|
|
XAxis.Translate(-XMoveAmount, 0, 0);
|
|
}
|
|
|
|
if (ZMoveDistance > 0)
|
|
{
|
|
ZAxis.Translate(0, 0, ZMoveAmount);
|
|
}
|
|
else
|
|
{
|
|
ZAxis.Translate(0, 0, -ZMoveAmount);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
else if (Mathf.Abs(YMoveDistance) > 0)
|
|
{
|
|
float YStep = CartYSpeed * Time.deltaTime;
|
|
float YMoveAmount = Mathf.Min(YStep, Mathf.Abs(YMoveDistance));
|
|
|
|
if (YMoveDistance > 0)
|
|
{
|
|
YAxis.Translate(0, -YMoveAmount, 0);
|
|
}
|
|
else
|
|
{
|
|
YAxis.Translate(0, YMoveAmount, 0);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
//Vector3.MoveTowards()
|
|
XAxis.localPosition = new(cords.x, XAxis.localPosition.y, XAxis.localPosition.z);
|
|
YAxis.localPosition = new(YAxis.localPosition.x, cords.y, YAxis.localPosition.z);
|
|
ZAxis.localPosition = new(ZAxis.localPosition.x, ZAxis.localPosition.y, cords.z);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Ãðàíèöû ïî êîîðäèíàòàì: (0.435, 0.02, 0.4125)
|
|
|
|
|
|
public void StartWorkFromButton()
|
|
{
|
|
if (!WorkInProgress)
|
|
{
|
|
if (ReadyToWork)
|
|
{
|
|
StartWork(PalmTreeProg);
|
|
}
|
|
else
|
|
{
|
|
OnError?.Invoke(DateTime.Now + " Error: " + "Materials not inserted: " + InsertedMaterials.Count(b => b) + " / 3.");
|
|
//Debug.Log(InsertedMaterials);
|
|
}
|
|
}
|
|
else if (PauseFlag)
|
|
{
|
|
StepToPause = -1;
|
|
PauseFlag = false;
|
|
}
|
|
}
|
|
|
|
public void PauseWorkFromButton()
|
|
{
|
|
if (WorkInProgress)
|
|
{
|
|
PauseFlag = !PauseFlag;
|
|
}
|
|
}
|
|
|
|
public void StopWorkFromButton()
|
|
{
|
|
resetFlags();
|
|
ErrorFlag = true;
|
|
|
|
OnError?.Invoke(DateTime.Now + " Error: " + "Stopped from button.");
|
|
}
|
|
|
|
public void ResetFromButton()
|
|
{
|
|
OnError?.Invoke("To start work again press Run, to continue press Step.");
|
|
|
|
resetFlags();
|
|
RestartFlag = true;
|
|
}
|
|
|
|
public void StepFromButton()
|
|
{
|
|
if (!WorkInProgress)
|
|
{
|
|
if (CurrentStep != 0) WorkInProgress = true; // Для продолжения работы после остановки
|
|
|
|
else if (ReadyToWork)
|
|
{
|
|
StartWork(PalmTreeProg);
|
|
|
|
StepToPause = CurrentStep + 1;
|
|
}
|
|
else
|
|
{
|
|
OnError?.Invoke(DateTime.Now + " Error: " + "Materials not inserted: " + InsertedMaterials.Count(b => b) + " / 3.");
|
|
//Debug.Log(InsertedMaterials);
|
|
}
|
|
}
|
|
else if (PauseFlag)
|
|
{
|
|
StepToPause = CurrentStep + 1;
|
|
|
|
PauseFlag = false;
|
|
}
|
|
}
|
|
|
|
private void resetFlags()
|
|
{
|
|
WorkInProgress = false;
|
|
ErrorFlag = false;
|
|
PauseFlag = false;
|
|
RestartFlag = false;
|
|
}
|
|
|
|
|
|
|
|
}
|