Newer
Older
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";
13035516
committed
public TriggerBoundary DestTriggerBoundary;
public SpriteRenderer PlayerSprite;
public GameObject[] Goals;
13035516
committed
public GameObject Base;
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);
13035516
committed
private TriggerBoundary BaseTriggerBoundary;
private float OnsetDev = 0;
private int GoalSet = 0;
private const int NumGoals = 2;
13035516
committed
// If exceeds this time including hold time, then the player loses
private const float FailTime = 4.0f;
// Player must keep their sprite in the goal for this long in order to complete the task
private const float HoldTime = 1.0f;
// Time the player must spend in the base to be considered end of trial
private const float BaseHoldTime = 0.2f;
private bool TrialState = false; // 0: inactive trial, 1: active trial
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);
13035516
committed
BaseTriggerBoundary = Base.GetComponent<TriggerBoundary>();
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;
13035516
committed
// Timer started ticking? Then the trial has started
if (Timer > 0) {
TrialState = true;
}
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
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
13035516
committed
DestTriggerBoundary = Goals[GoalSet].GetComponent<TriggerBoundary>();
// If the player hasn't achieved the hold time in the allotted time
// they fail
if (Timer > FailTime)
{
TrialResult = false;
13035516
committed
TrialState = false;
13035516
committed
else if (Timer <= FailTime && DestTriggerBoundary.GetStayTime() >= HoldTime)
{
TrialResult = true;
13035516
committed
TrialState = false;
13035516
committed
if (!TrialState)
13035516
committed
// Don't begin the new trial until the user has returned to the
// base position
if ( DestTriggerBoundary.GetStayTime() >= BaseHoldTime ) {
Debug.Log("BaseStayTime: " + DestTriggerBoundary.GetStayTime());
EndTrialSequence();
}
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
201
202
203
204
205
206
207
208
209
}
}
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;
13035516
committed
}
// Check for any fail conditions here
// Incl: Taking too long to finish, leaving early (disqualification)
private bool FailConditions() {
return true;