-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineManager.cs
More file actions
46 lines (43 loc) · 1.26 KB
/
MachineManager.cs
File metadata and controls
46 lines (43 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections;
using UnityEngine;
//! This class controls machine update functions via coroutine .
//! One machine is updated per frame.
public class MachineManager : MonoBehaviour
{
private bool busy;
private Coroutine machineUpdateCoroutine;
//! Called once per frame by unity engine.
public void Update()
{
if (busy == false && GetComponent<StateManager>().initMachines == true)
{
machineUpdateCoroutine = StartCoroutine(MachineUpdateCoroutine());
}
}
//! Calls the UpdateMachine function on each machine in the world, yielding after each call.
private IEnumerator MachineUpdateCoroutine()
{
busy = true;
int interval = 0;
Machine[] machines = FindObjectsOfType<Machine>();
foreach (Machine machine in machines)
{
try
{
machine.UpdateMachine();
}
catch (Exception e)
{
Debug.Log(e.Message);
}
interval++;
if (interval >= machines.Length * GetComponent<GameManager>().simulationSpeed)
{
yield return null;
interval = 0;
}
}
busy = false;
}
}