-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGameManager.cs
More file actions
200 lines (173 loc) · 6.83 KB
/
Copy pathGameManager.cs
File metadata and controls
200 lines (173 loc) · 6.83 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma warning disable 0618 // Ignore obsolete script warning
using System.Collections.Concurrent;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
namespace C2M2
{
using Interaction.VR;
using NeuronalDynamics.Interaction;
using NeuronalDynamics.Interaction.UI;
using NeuronalDynamics.Simulation;
using Simulation;
/// <summary>
/// Stores many global variables, handles pregame initializations
/// </summary>
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
public static int simID = 0; // current simulation ID
// vectors used in SparseSolve; for loading
public double[] U;
public double[] Upre;
public Dictionary<string, double[]> currentStates;
public Dictionary<string, double[]> previousStates;
// for loading a file
private bool loading = false;
public bool Loading
{
get { return loading; }
set { loading = value; }
}
public int mainThreadId { get; private set; } = -1;
public VRDeviceManager vrDeviceManager = null;
public bool VRActive { get { return vrDeviceManager.VRActive; } }
public GameObject cellPreviewer = null;
/// <summary>
/// Default gradient for coloring the mesh's surface
/// </summary>
public Gradient defaultGradient;
public List<Interactable> activeSims = new List<Interactable>();
public readonly object activeSimsLock = new object();
public static bool isQuitting { get; private set; } = false;
public GameObject clampManagerPrefab = null;
public GameObject clampManagerL = null;
public GameObject clampManagerR = null;
public GameObject synapseManagerPrefab = null;
public GameObject graphManagerPrefab = null;
/// <summary>
/// Allows solver threads to be synched
/// </summary>
public Barrier solveBarrier = new Barrier(0);
public NDSimulationManager simulationManager = null;
public GameObject simulationSpace = null;
[Header("Environment")]
public int roomSelected = 0;
public Room[] roomOptions = null;
public Color wallColor = Color.white;
[Header("Materials")]
public Material vertexColorationMaterial = null;
public Material lineRendMaterial = null;
[Tooltip("Used as an anchor point for neuron diameter control panel")]
public Transform whiteboard = null;
public Vector3 objScaleDefault = new Vector3(2f, 2f, 2f);
public Vector3 objScaleMax = new Vector3(4f, 4f, 4f);
public Vector3 objScaleMin = new Vector3(0.3f, 0.3f, 0.3f);
[Header("OVR Player Controller")]
public GameObject ovrRightHandAnchor = null;
public GameObject ovrLeftHandAnchor = null;
public OVRPlayerController ovrPlayerController { get; set; } = null;
public GameObject nonVRCamera { get; set; } = null;
[Header("FPS Counter")]
public Utils.DebugUtils.FPSCounter fpsCounter;
private bool isRunning = false;
private void Awake()
{
// Initialize the GameManager
DontDestroyOnLoad(gameObject);
if (instance == null) { instance = this; }
else if (instance != this) { Destroy(this); }
mainThreadId = Thread.CurrentThread.ManagedThreadId;
if(roomOptions != null && roomOptions.Length > 0)
{
roomSelected = Mathf.Clamp(roomSelected, 0, roomOptions.Length - 1);
// Only enable selected room, disable all others
for(int i = 0; i < roomOptions.Length; i++)
{
roomOptions[i].gameObject.SetActive(i == roomSelected);
}
// Apply wall color to selected room's walls
if (roomOptions[roomSelected].walls != null && roomOptions[roomSelected].walls.Length > 0)
{
foreach (MeshRenderer wall in roomOptions[roomSelected].walls)
{
if (wall != null)
{
wall.material.color = wallColor;
}
else
{
Debug.LogWarning("Wall's meshrenderer was null on " + roomOptions[roomSelected].name);
}
}
}
}
if (cellPreviewer != null)
{
cellPreviewer = GameObject.Instantiate(cellPreviewer);
}
else
{
Debug.LogError("No cell previewer prefab given!");
}
isRunning = true;
}
private void Update()
{
string msg;
while (logQ.TryDequeue(out msg)) { Debug.Log(msg); }
while (eLogQ.TryDequeue(out msg)) { Debug.LogError(msg); }
}
private readonly ConcurrentQueue<string> logQ = new ConcurrentQueue<string>();
private readonly int logQCap = 100;
/// <summary>
/// Allows other threads to submit messages to be printed at the start of the next frame
/// </summary>
/// <remarks>
/// Making any Unity API call from another thread is not safe. This method is a quick hack
/// to avoid mkaing a Unity API call from another thread.
/// </remarks>
public void DebugLogSafe(string s)
{
if (isRunning)
{
if (logQ.Count > logQCap)
{
return;
}
logQ.Enqueue(s);
}
}
public void DebugLogThreadSafe<T>(T t) => DebugLogSafe(t.ToString());
private readonly ConcurrentQueue<string> eLogQ = new ConcurrentQueue<string>();
private readonly int eLogQCap = 100;
/// <summary>
/// Allows other threads to submit messages to be printed at the start of the next frame
/// </summary>
/// <remarks>
/// Making any Unity API call from another thread is not safe. This method is a quick hack
/// to avoid making a Unity API call from another thread.
/// </remarks>
public void DebugLogErrorSafe(string s)
{
if (isRunning)
{
if (eLogQ.Count > eLogQCap)
{
return;
}
eLogQ.Enqueue(s);
}
}
public void DebugLogErrorThreadSafe<T>(T t) => DebugLogErrorSafe(t.ToString());
private void OnApplicationQuit()
{
isQuitting = true;
isRunning = false;
}
private void OnApplicationPause(bool pause)
{
isRunning = !pause;
}
}
}