Newer
Older
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random=System.Random;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Reflection;
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct TimingSetting {
public float TrialStart;
public float TargetOnset;
public float TargetOnsetDev;
public float Go;
public float MoveMax;
public float HoldTime;
};
public class GameController : MonoBehaviour
{
public string TimingSettingFile = @"\Tables\Timings.csv";
public TriggerBoundary mTriggerBoundary;
public SpriteRenderer PlayerSprite;
public GameObject[] Goals;
public bool TrialResult {get; private set;}
private TimingSetting mTimingSetting;
private string[] TableHeader;
private float[] TableData;
private ChangeColour[] GoalsChangeColour;
private int ExperimentTrial = 0;
private float Timer = 0f;
private Random GoalSetter = new System.Random(300);
private Random OnsetDeviator = new System.Random(100);
private float OnsetDev = 0;
private int GoalSet = 0;
private const int NumGoals = 2;
private const int FailTime = 4000; // If exceeds this time including hold time, then the player loses
private const int HoldTime = 1000; // Player must keep their sprite in the goal for this long in order to complete the task
private bool TrialEnded = false;
private GameObject Goal;
void Awake()
{
GoalsChangeColour = new ChangeColour[Goals.Length];
}
// Start is called before the first frame update
void Start()
{
int length = Marshal.SizeOf (typeof(TimingSetting));
TableHeader = GetTableHeader(TimingSettingFile);
TableData = GetTableFloat(TimingSettingFile);
mTimingSetting = FloatArrayToStruct(TableData,mTimingSetting);
PlayerSprite.enabled = true;
for (int i = 0; i < Goals.Length; i++)
{
GoalsChangeColour[i] = Goals[i].GetComponent<ChangeColour>();
}
/* GoalSet = GoalSetter.Next(NumGoals); */
/* OnsetDev = OnsetDeviator.Next(mTimingSetting.TargetOnsetDev); */
Debug.Log("GoalSet: " + GoalSet);
}
// Update is called once per frame
void Update()
{
float dt = Time.deltaTime;
// measure in ms
Timer += dt*1000;
if (Timer > mTimingSetting.TrialStart)
{
bool[] states = CheckGoalStates(GoalsChangeColour);
bool flag = false;
for (int i = 0; i < NumGoals; i++)
{
if (states[i])
{
flag = true;
}
}
if (!flag)
{
GoalSet = GoalSetter.Next(NumGoals);
GoalsChangeColour[GoalSet].GoalState = 1;
OnsetDev = OnsetDeviator.Next((int)mTimingSetting.TargetOnsetDev*2) -
mTimingSetting.TargetOnsetDev;
Debug.Log("OnsetDev: " + OnsetDev);
}
}
if (Timer > (mTimingSetting.TrialStart +
mTimingSetting.TargetOnset + OnsetDev) )
{
GoalsChangeColour[GoalSet].GoalState = 2;
}
// If the player is in the target start counting
if (Timer > FailTime)
{
TrialResult = false;
TrialEnded = true;
}
else if (Timer <= FailTime && mTriggerBoundary.GetStayTime() >= HoldTime)
{
TrialResult = true;
TrialEnded = true;
}
if (TrialEnded)
{
EndTrialSequence();
}
// If the player hasn't achieved the hold time in the allotted time
// they fail
}
private string[] GetTableHeader(string filename)
{
string[] header;
string table_file = @".\Assets\Scripts";
table_file = table_file + filename;
using (var reader = new StreamReader(table_file)) {
var line = reader.ReadLine();
header = line.Split(',');
}
return header;
}
private float[] GetTableFloat(string filename)
{
float[] data = new float[6];
string table_file = @".\Assets\Scripts";
table_file = table_file + filename;
using (var reader = new StreamReader(table_file)) {
reader.ReadLine();
var line = reader.ReadLine();
string []values = line.Split(',');
for (int i = 0; i < values.Length; i++) {
data[i] = Single.Parse(values[i]);
}
}
return data;
}
private TimingSetting FloatArrayToStruct(float[] floatarray, TimingSetting structureObj) {
int length = floatarray.Length;
IntPtr ptr = Marshal.AllocHGlobal (length);
Marshal.Copy (floatarray, 0, ptr, length);
structureObj = (TimingSetting)Marshal.PtrToStructure (ptr, structureObj.GetType());
Marshal.FreeHGlobal (ptr);
return structureObj;
}
private bool[] CheckGoalStates(ChangeColour[] GoalColours)
{
bool[] states = new bool[GoalColours.Length];
for (int i = 0 ; i < GoalColours.Length; i++)
{
if (GoalColours[i].GoalState > 0)
{
states[i] = true;
}
else
{
states[i] = false;
}
}
return states;
}
private void EndTrialSequence()
{
ExperimentTrial++;
GoalsChangeColour[GoalSet].GoalState = 0;
Timer = 0;
TrialEnded = false;
}
}