298 lines
5.8 KiB
C#
298 lines
5.8 KiB
C#
using Palmmedia.ReportGenerator.Core;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Rendering;
|
|
|
|
public class MachineCoordinatesMovementScript : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
[Tooltip("XAxis Transform")]
|
|
Transform XAxis;
|
|
|
|
[SerializeField]
|
|
[Tooltip("YAxis Transform")]
|
|
Transform YAxis;
|
|
|
|
[SerializeField]
|
|
[Tooltip("ZAxis Transform")]
|
|
Transform ZAxis;
|
|
|
|
[SerializeField]
|
|
Vector3 CurrentCords;
|
|
|
|
[SerializeField]
|
|
float CartXSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
float CartYSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
float CartZSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
int MaterialsToInsert = 0;
|
|
|
|
[SerializeField]
|
|
bool TestProgramStart;
|
|
|
|
[SerializeField]
|
|
bool DebugStart = false;
|
|
|
|
[SerializeField]
|
|
bool Restart = false;
|
|
|
|
// Ôëàã ðàáîòû, ïî íåìó ïðîâåðÿåòñÿ, âûïîëíÿåòñÿ ëè ñåé÷àñ êàêàÿ-ëèáî ïðîãðàììà
|
|
bool WorkInProgress = false;
|
|
|
|
[SerializeField]
|
|
List<bool> InsertedMaterials;
|
|
|
|
[SerializeField]
|
|
List<bool> InsertedReels;
|
|
|
|
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> { }
|
|
|
|
[Tooltip("Event called when step updated")]
|
|
public static MachineStepEvent OnStep = new MachineStepEvent();
|
|
|
|
// Ãðàíèöû ïî êîîðäèíàòàì: (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);
|
|
}
|
|
|
|
if (TestProgramStart == true)
|
|
{
|
|
TestProgramStart = false;
|
|
|
|
if (ReadyToWork)
|
|
{
|
|
StartWork(PalmTreeProg);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log(InsertedMaterials);
|
|
}
|
|
}
|
|
|
|
if (Restart == true)
|
|
{
|
|
if (MoveTo(Vector3.zero))
|
|
{
|
|
Restart = false;
|
|
}
|
|
}
|
|
else if (WorkInProgress == true)
|
|
{
|
|
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);
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
WorkInProgress = false;
|
|
Restart = true;
|
|
Board.GetComponent<BoardScript>().ComponentsPlaced();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ôóíêöèÿ íà÷àëà ðàáîòû, ïðèíèìàåò ïðîãðàììó äëÿ ðàáîòû
|
|
public void StartWork(List<Vector3> program)
|
|
{
|
|
WorkInProgress = true;
|
|
Restart = 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)
|
|
{
|
|
TestProgramStart = true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|