Controller van de trein minigame
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class TrainMovementController : MonoBehaviour
{
[SerializeField]
private GameObject Train;
[SerializeField]
private Transform endPoint;
[SerializeField]
private Transform startPointTrain;
[SerializeField]
private GameObject Sensor;
[SerializeField]
private GameObject greenLight;
[SerializeField]
private GameObject redLight;
[SerializeField]
private GameObject winLight;
[SerializeField]
private GameObject ToDoListItem;
private float afstand;
private bool usingGreaterThen;
private float conditionValue;
private string condition;
private string actionOneTag;
private string actionTwoTag;
private string elseActionOneTag;
private string elseActionTwoTag;
[SerializeField]
private GameObject CodeManager;
private List<string> ifSocketBlocks = new List<string>();
private bool isPlaying = false;
private bool checkScript = false;
private bool firstCheck = false;
private bool lastCheck = false;
[SerializeField]
private AudioSource audioSource;
private Vector3 trainStartPosition;
[SerializeField]
private List<GameObject> uiElements;
private void Start()
{
trainStartPosition = Train.transform.position;
}
void Update()
{
afstand = Vector3.Distance(startPointTrain.position, Sensor.transform.position) * 100;
if (checkScript)
{
IF();
Else();
}
if(isPlaying)
{
checkTrainScript();
checkLights();
if(greenLight.activeSelf && !redLight.activeSelf)
{
moveTrainToEndpoint();
}
}
}
public void OnPlay()
{
checkScript = true;
isPlaying = true;
audioSource.Play();
}
private void checkLights()
{
//Check if the lights are on or off based on if the gameobjec tis active or not
if (afstand > 20) {
if (greenLight.activeSelf && !redLight.activeSelf)
{
firstCheck = true;
}
}
else {
if (!greenLight.activeSelf && redLight.activeSelf)
{
lastCheck = true;
}
}
if (lastCheck && firstCheck)
{
Debug.Log("Win");
uiElements.ForEach(uiElement => uiElement.SetActive(false));
winLight.SetActive(true);
ToDoListItem.SetActive(true);
}
}
private void moveTrainToEndpoint()
{
if (isPlaying)
{
Train.transform.position = Vector3.Lerp(Train.transform.position, endPoint.position, Time.deltaTime * 0.5f);
// Check if the train is close to the endpoint
if (Vector3.Distance(Train.transform.position, endPoint.position) < 0.02f)
{
// If green light is on, reset the train
if (greenLight.activeSelf)
{
isPlaying = false;
Reset();
}
// If green light is off and red light is on, stop movement but maintain position
else if (redLight.activeSelf)
{
isPlaying = false;
}
else if (!greenLight.activeSelf && !redLight.activeSelf)
{
isPlaying = false;
Reset();
}
}
}
}
private void Reset()
{
Train.transform.position = trainStartPosition;
isPlaying = false;
firstCheck = false;
lastCheck = false;
greenLight.SetActive(true);
redLight.SetActive(true);
}
private void checkTrainScript()
{
if (usingGreaterThen)
{
if (afstand > conditionValue)
{
if (actionOneTag != null) Invoke(actionOneTag, 0f);
if (actionTwoTag != null) Invoke(actionTwoTag, 0f);
}
else
{
if (elseActionOneTag != null) Invoke(elseActionOneTag, 0f);
if (elseActionTwoTag != null) Invoke(elseActionTwoTag, 0f);
}
}
else
{
if (afstand < conditionValue)
{
if (actionOneTag != null) Invoke(actionOneTag, 0f);
if (actionTwoTag != null) Invoke(actionTwoTag, 0f);
}
else
{
if (elseActionOneTag != null) Invoke(elseActionOneTag, 0f);
if (elseActionTwoTag != null) Invoke(elseActionTwoTag, 0f);
}
}
checkScript = false;
}
private void IF()
{
foreach (Transform child in CodeManager.transform)
{
//look for the gameobject with the name IF
if (child.name == "IF")
{
foreach (Transform socket in child)
{
ifSocketBlocks.Add(socket.tag);
}
}
}
condition = ifSocketBlocks.Count > 0 ? ifSocketBlocks[0] : null;
usingGreaterThen = ifSocketBlocks.Count > 1 && ifSocketBlocks[1] != "Smaller";
conditionValue = ifSocketBlocks.Count > 2 ? float.Parse(ifSocketBlocks[2]) : 0;
actionOneTag = ifSocketBlocks.Count > 3 ? ifSocketBlocks[3] : null;
actionTwoTag = ifSocketBlocks.Count > 4 ? ifSocketBlocks[4] : null;
// clear list
ifSocketBlocks.Clear();
}
private void Else()
{
foreach (Transform child in CodeManager.transform)
{
//look for the gameobject with the name IF
if (child.name == "Else")
{
foreach (Transform socket in child)
{
ifSocketBlocks.Add(socket.tag);
}
}
}
elseActionOneTag = ifSocketBlocks.Count > 0 ? ifSocketBlocks[0] : null;
elseActionTwoTag = ifSocketBlocks.Count > 1 ? ifSocketBlocks[1] : null;
// clear list
ifSocketBlocks.Clear();
}
private void GreenOff()
{
greenLight.SetActive(false);
}
private void GreenOn()
{
greenLight.SetActive(true);
}
private void RedOff()
{
redLight.SetActive(false);
}
private void RedOn()
{
redLight.SetActive(true);
}
}