UnityGameProjectsCode / NightwatchGame / MachineRandomizer.cs
MachineRandomizer.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MachineRandomizer : MonoBehaviour
{
    //choose random machines to turn on when this script is active

    LaundryMachine[] allMachines = new LaundryMachine[32];

    public struct machineRunSet
    {
        public float waitTimeBeforeRun;
        public float runTime;
        public float turnOnMin, turnOnMax;
        public bool runComplete;
    }

    public List<machineRunSet> allRuns = new List<machineRunSet>();

    private float timeWaited;
    private float timeBeforeNextTurnOn;
    private float turnOnMin, turnOnMax;
    private bool randomizerActive;
    private int runsCompleted = 0;
    private machineRunSet activeRunSet = new machineRunSet();

    public bool RandomizerActive { get => randomizerActive; set => randomizerActive = value; }
    public float TurnOnMin { get => turnOnMin; set => turnOnMin = value; }
    public float TurnOnMax { get => turnOnMax; set => turnOnMax = value; }

    private void Awake()
    {
        machineRunSet firstSet = new machineRunSet();
        firstSet.waitTimeBeforeRun = 15;
        firstSet.runTime = 20;
        firstSet.turnOnMin = 2.5f;
        firstSet.turnOnMax = 6.0f;

        //firstSet.waitTimeBeforeRun = 1; //TEMP VALUES
        //firstSet.runTime = 1;
        //firstSet.turnOnMin = 2.5f;
        //firstSet.turnOnMax = 6.0f;

        firstSet.runComplete = false;
        machineRunSet secondSet = new machineRunSet();
        secondSet.waitTimeBeforeRun = 0;
        secondSet.runTime = 30;
        secondSet.turnOnMin = 1.5f;
        secondSet.turnOnMax = 3.0f;

        //secondSet.waitTimeBeforeRun = 0; //TEMP VALUES
        //secondSet.runTime = 1;
        //secondSet.turnOnMin = 1.5f;
        //secondSet.turnOnMax = 3.0f;

        secondSet.runComplete = false;
        machineRunSet thirdSet = new machineRunSet();
        thirdSet.waitTimeBeforeRun = 0;
        thirdSet.runTime = float.MaxValue;
        thirdSet.turnOnMin = 0.25f;
        thirdSet.turnOnMax = 1.0f;
        thirdSet.runComplete = false;

        allRuns.Add(firstSet);
        allRuns.Add(secondSet);
        allRuns.Add(thirdSet);

        GameObject[] tempArray = GameObject.FindGameObjectsWithTag("Machine");

        for (int i = 0; i < tempArray.Length; i++)
        {
            allMachines[i] = tempArray[i].GetComponent<LaundryMachine>();
        }
    }

    private void Update()
    {
        timeWaited += Time.deltaTime;

        if (timeWaited >= timeBeforeNextTurnOn && randomizerActive)
        {
            ActivateRandomMachine();
            timeWaited = 0.0f;
            timeBeforeNextTurnOn = Random.Range(turnOnMin, turnOnMax);
        }
    }

    public IEnumerator RunSet()
    {
        activeRunSet.runTime = 0;
        activeRunSet.waitTimeBeforeRun = 0;
        activeRunSet.turnOnMin = 0;
        activeRunSet.turnOnMax = 0;
        activeRunSet.runComplete = false;

        for (int i = 0; i < allRuns.Count; i++)
        {
            if (!allRuns[i].runComplete)
            {
                activeRunSet = allRuns[i];
                break;
            }
        }

        if (activeRunSet.runTime == 0)
            yield return null;

        turnOnMin = activeRunSet.turnOnMin;
        turnOnMin = activeRunSet.turnOnMax;

        yield return new WaitForSecondsRealtime(activeRunSet.waitTimeBeforeRun);

        randomizerActive = true;

        yield return new WaitForSecondsRealtime(activeRunSet.runTime);

        activeRunSet.runComplete = true;
        randomizerActive = false;
        allRuns[runsCompleted] = activeRunSet;
        runsCompleted++;
    }

    public IEnumerator ResumeSet()
    {
        yield return new WaitForSecondsRealtime(activeRunSet.waitTimeBeforeRun);

        randomizerActive = true;

        yield return new WaitForSecondsRealtime(activeRunSet.runTime);

        activeRunSet.runComplete = true;
        randomizerActive = false;
        allRuns[runsCompleted] = activeRunSet;
        runsCompleted++;
    }

    private void ActivateRandomMachine()
    {
        int randomMachine;
        bool allMachinesAlreadyRunning = true;

        foreach (LaundryMachine machine in allMachines)
        {
            if (!machine.MachineRunning)
                allMachinesAlreadyRunning = false;
        }

        if (allMachinesAlreadyRunning)
            return;

        do
        {
            randomMachine = Random.Range(0, 32);
        }
        while (allMachines[randomMachine].MachineRunning);

        allMachines[randomMachine].RunMachine();
    }

    public bool CheckAllMachinesStopped()
    {
        bool allMachinesStopped = true;

        foreach (LaundryMachine machine in allMachines)
        {
            if (machine.MachineRunning)
                allMachinesStopped = false;
        }

        return allMachinesStopped;
    }

    public bool MostMachinesRunning()
    {
        int numberOfMachinesRunning = 0;

        foreach (LaundryMachine machine in allMachines)
        {
            if (machine.MachineRunning)
                numberOfMachinesRunning++;
        }

        if (numberOfMachinesRunning >= 17)
            return true;
        else
            return false;
    }

    public void PauseRandomizer()
    {
        StopAllCoroutines();
        randomizerActive = false;

        foreach (LaundryMachine machine in allMachines)
        {
            machine.TurnOffMachine();
        }
    }

    public void TurnOnAllMachines()
    {
        randomizerActive = true;

        foreach (LaundryMachine machine in allMachines)
        {
            machine.TurnOnMachine();
        }
    }

    public bool GetActiveRunSetComplete()
    {
        return activeRunSet.runComplete;
    }

    public int GetRunsCompleted()
    {
        return runsCompleted;
    }
}