FittsLaw/Assets/Scripts/ExperimentController.cs

214 lines
6.3 KiB
C#
Raw Permalink Normal View History

2018-11-28 19:09:50 -05:00
using Assets.Scripts;
using System;
2018-10-14 23:33:23 -04:00
using System.Collections.Generic;
2018-10-30 19:07:29 -04:00
using System.Diagnostics;
2018-11-29 13:14:03 -05:00
using System.Net;
2018-10-14 19:55:46 -04:00
using System.Text;
using UnityEngine;
2018-10-30 19:07:29 -04:00
using UnityEngine.EventSystems;
2018-10-14 19:55:46 -04:00
public class ExperimentController : MonoBehaviour {
GameObject startDot;
GameObject nextDot;
int trialIndex;
int trialDotIndex;
Participant participant;
2018-10-14 23:33:23 -04:00
List<GameObject> dots = new List<GameObject>();
2018-10-30 19:07:29 -04:00
Stopwatch stopwatch = new Stopwatch();
GameObject playerCamera;
2018-11-29 19:18:09 -05:00
GameObject text;
2018-11-28 19:09:50 -05:00
DataSet dataSet = new DataSet();
string platform;
2018-10-14 19:55:46 -04:00
// Use this for initialization
void Start () {
2018-11-28 19:09:50 -05:00
#if UNITY_ANDROID
platform = "cardboard";
#endif
#if UNITY_EDITOR
platform = "mouse";
#endif
#if UNITY_STANDALONE_WIN
2018-11-29 13:14:03 -05:00
platform = "oculus";
2018-11-28 19:09:50 -05:00
#endif
2020-06-22 13:30:08 -04:00
platform = "mouse";
2018-11-29 19:18:09 -05:00
UnityEngine.Debug.Log(platform);
2018-11-29 13:14:03 -05:00
string response = "";
2020-05-27 01:49:37 -04:00
UnityEngine.Debug.Log(Application.persistentDataPath);
2020-05-27 02:10:41 -04:00
//Load Trial data from server
2018-11-29 13:14:03 -05:00
using (WebClient client = new WebClient())
{
2020-06-22 13:30:08 -04:00
string endpoint = "http://<SERVER INSTANCE URL>/" + platform;
2018-11-29 13:14:03 -05:00
response = client.DownloadString(endpoint);
UnityEngine.Debug.Log("Received Response: " + response);
}
participant = new Participant(response);
2018-10-14 19:55:46 -04:00
startDot = GameObject.Find("StartButton");
2018-10-30 19:07:29 -04:00
playerCamera = GameObject.FindGameObjectsWithTag("MainCamera")[0];
2018-10-14 19:55:46 -04:00
trialIndex = 0;
trialDotIndex = 0;
2020-05-27 02:10:41 -04:00
//Hide all the non-start dots and the DONE text
2018-10-14 19:55:46 -04:00
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Dot"))
{
2018-10-14 23:33:23 -04:00
dots.Add(go);
2018-10-14 19:55:46 -04:00
Hide(go);
};
2018-11-29 19:18:09 -05:00
text = GameObject.Find("Text");
text.SetActive(false);
2018-10-14 19:55:46 -04:00
}
void Hide(GameObject dot)
{
2018-10-14 23:33:23 -04:00
dot.SetActive(false);
2018-10-14 19:55:46 -04:00
}
void Show(GameObject dot)
{
2018-10-14 23:33:23 -04:00
dot.SetActive(true);
2018-10-14 19:55:46 -04:00
}
public void NextDot()
{
2020-05-27 02:10:41 -04:00
//Stop the timer if it is running.
2018-10-30 19:07:29 -04:00
if (stopwatch.IsRunning)
{
stopwatch.Stop();
double time = stopwatch.Elapsed.TotalSeconds;
stopwatch.Reset();
2018-11-28 19:09:50 -05:00
double angle = Vector3.Angle(nextDot.transform.position, playerCamera.transform.position);
2020-05-27 01:49:37 -04:00
//double distance = Vector3.Distance(nextDot.transform.position, startDot.transform.position);
2020-05-27 02:10:41 -04:00
//Save a sample
2020-05-27 01:49:37 -04:00
SaveSample(angle, time, false);
2018-11-28 19:09:50 -05:00
}
2020-05-27 02:10:41 -04:00
//Advance to the next dot and make it visible, while hiding the start dot.
2018-11-28 19:09:50 -05:00
if (trialDotIndex < participant.trials[trialIndex].Length)
{
nextDot = FindDot(participant.trials[trialIndex][trialDotIndex]);
trialDotIndex++;
Show(nextDot);
Hide(startDot);
}
2020-05-27 02:10:41 -04:00
//Advance to the next trial
2018-11-28 19:09:50 -05:00
else if(trialIndex < participant.trials.Length)
{
trialIndex++;
trialDotIndex = 0;
2020-05-27 02:10:41 -04:00
//Save data if we aren't in practice mode.
2018-11-30 15:32:34 -05:00
if(participant.participantId != 9999) dataSet.SaveDataSet();
2018-11-28 19:09:50 -05:00
dataSet.Samples.Clear();
2018-11-30 15:32:34 -05:00
if (trialIndex < participant.trials.Length && participant.participantId != 9999)
2018-11-29 19:18:09 -05:00
{
nextDot = FindDot(participant.trials[trialIndex][trialDotIndex]);
Show(nextDot);
Hide(startDot);
}
else
{
text.SetActive(true);
}
2018-10-30 19:07:29 -04:00
}
else
{
2020-05-27 02:10:41 -04:00
//Save data if we aren't in practice mode.
2018-11-30 15:32:34 -05:00
if (participant.participantId != 9999) dataSet.SaveDataSet();
2018-10-30 19:07:29 -04:00
}
2018-11-28 19:09:50 -05:00
stopwatch.Start();
2018-10-14 19:55:46 -04:00
}
2020-05-27 02:10:41 -04:00
//Hide current dot, Show start dot, and reset the timer.
2018-10-14 19:55:46 -04:00
public void BackToStart()
{
2018-10-30 19:07:29 -04:00
stopwatch.Stop();
double time = stopwatch.Elapsed.TotalSeconds;
stopwatch.Reset();
double angle = Vector3.Angle(nextDot.transform.position, playerCamera.transform.position);
2020-05-27 01:49:37 -04:00
//double distance = Vector3.Distance(nextDot.transform.position, startDot.transform.position);
2020-05-27 02:10:41 -04:00
//Save a sample
2020-05-27 01:49:37 -04:00
SaveSample(angle, time, true);
2018-10-14 19:55:46 -04:00
Hide(nextDot);
Show(startDot);
2018-11-28 19:09:50 -05:00
stopwatch.Start();
2018-10-30 19:07:29 -04:00
2018-10-14 19:55:46 -04:00
2018-10-30 19:07:29 -04:00
}
2020-05-27 02:10:41 -04:00
//Wrapper method to convert the Cardboard way of handling things to the Oculus one.
2018-10-30 19:07:29 -04:00
public void CardboardWrapper(GameObject go, PointerEventData eventData)
{
UnityEngine.Debug.Log(eventData.pointerPressRaycast.worldPosition);
if (go != null)
{
UnityEngine.Debug.Log(go.name);
if (go.name == "StartButton")
{
NextDot();
}
else
{
BackToStart();
}
}
else
{
UnityEngine.Debug.Log("Missed");
}
2018-10-14 19:55:46 -04:00
}
2020-05-27 01:49:37 -04:00
void SaveSample(double angle, double time, bool direction)
2018-11-28 19:09:50 -05:00
{
Sample sample = new Sample();
sample.Angle = angle;
2020-05-27 02:10:41 -04:00
//Fill in dot distances.
2020-05-27 01:49:37 -04:00
if(nextDot.name.Contains("Short")){
sample.Distance = 500;
}
else if(nextDot.name.Contains("Medium")){
sample.Distance = 1000;
}
else if(nextDot.name.Contains("Long")){
sample.Distance = 1500;
}
2020-06-22 13:30:08 -04:00
if (nextDot.name.Contains("Up")){
sample.Angle = 90;
}
else if (nextDot.name.Contains("Right"))
{
sample.Angle = 0;
}else if (nextDot.name.Contains("Left"))
{
sample.Angle = 180;
}
2018-11-28 19:09:50 -05:00
sample.Time = time;
sample.Direction = direction;
sample.DotName = nextDot.name;
sample.DotIndex = trialDotIndex;
sample.TrialIndex = trialIndex + 1;
sample.ParticipantID = participant.participantId;
sample.Platform = platform;
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
sample.Timestamp = (long)t.TotalMilliseconds;
dataSet.Samples.Add(sample);
}
2018-10-14 23:33:23 -04:00
GameObject FindDot(string name)
{
2018-11-28 19:09:50 -05:00
foreach (GameObject dot in dots)
2018-10-14 23:33:23 -04:00
{
if (dot.name == name) return dot;
}
return null;
}
2018-11-28 19:09:50 -05:00
// Update is called once per frame
void Update () {
}
2018-10-14 19:55:46 -04:00
}