FittsLaw/Assets/Scripts/ExperimentController.cs
2020-05-27 02:10:41 -04:00

204 lines
6.0 KiB
C#

using Assets.Scripts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
public class ExperimentController : MonoBehaviour {
GameObject startDot;
GameObject nextDot;
int trialIndex;
int trialDotIndex;
Participant participant;
List<GameObject> dots = new List<GameObject>();
Stopwatch stopwatch = new Stopwatch();
GameObject playerCamera;
GameObject text;
DataSet dataSet = new DataSet();
string platform;
// Use this for initialization
void Start () {
#if UNITY_ANDROID
platform = "cardboard";
#endif
#if UNITY_EDITOR
platform = "mouse";
#endif
#if UNITY_STANDALONE_WIN
platform = "oculus";
#endif
UnityEngine.Debug.Log(platform);
string response = "";
UnityEngine.Debug.Log(Application.persistentDataPath);
//Load Trial data from server
using (WebClient client = new WebClient())
{
string endpoint = "http://<URL OF SERVER INSTANCE>" + platform;
response = client.DownloadString(endpoint);
UnityEngine.Debug.Log("Received Response: " + response);
}
participant = new Participant(response);
startDot = GameObject.Find("StartButton");
playerCamera = GameObject.FindGameObjectsWithTag("MainCamera")[0];
trialIndex = 0;
trialDotIndex = 0;
//Hide all the non-start dots and the DONE text
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Dot"))
{
dots.Add(go);
Hide(go);
};
text = GameObject.Find("Text");
text.SetActive(false);
}
void Hide(GameObject dot)
{
dot.SetActive(false);
}
void Show(GameObject dot)
{
dot.SetActive(true);
}
public void NextDot()
{
//Stop the timer if it is running.
if (stopwatch.IsRunning)
{
stopwatch.Stop();
double time = stopwatch.Elapsed.TotalSeconds;
stopwatch.Reset();
double angle = Vector3.Angle(nextDot.transform.position, playerCamera.transform.position);
//double distance = Vector3.Distance(nextDot.transform.position, startDot.transform.position);
//Save a sample
SaveSample(angle, time, false);
}
//Advance to the next dot and make it visible, while hiding the start dot.
if (trialDotIndex < participant.trials[trialIndex].Length)
{
nextDot = FindDot(participant.trials[trialIndex][trialDotIndex]);
trialDotIndex++;
Show(nextDot);
Hide(startDot);
}
//Advance to the next trial
else if(trialIndex < participant.trials.Length)
{
trialIndex++;
trialDotIndex = 0;
//Save data if we aren't in practice mode.
if(participant.participantId != 9999) dataSet.SaveDataSet();
dataSet.Samples.Clear();
if (trialIndex < participant.trials.Length && participant.participantId != 9999)
{
nextDot = FindDot(participant.trials[trialIndex][trialDotIndex]);
Show(nextDot);
Hide(startDot);
}
else
{
text.SetActive(true);
}
}
else
{
//Save data if we aren't in practice mode.
if (participant.participantId != 9999) dataSet.SaveDataSet();
}
stopwatch.Start();
}
//Hide current dot, Show start dot, and reset the timer.
public void BackToStart()
{
stopwatch.Stop();
double time = stopwatch.Elapsed.TotalSeconds;
stopwatch.Reset();
double angle = Vector3.Angle(nextDot.transform.position, playerCamera.transform.position);
//double distance = Vector3.Distance(nextDot.transform.position, startDot.transform.position);
//Save a sample
SaveSample(angle, time, true);
Hide(nextDot);
Show(startDot);
stopwatch.Start();
}
//Wrapper method to convert the Cardboard way of handling things to the Oculus one.
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");
}
}
void SaveSample(double angle, double time, bool direction)
{
Sample sample = new Sample();
sample.Angle = angle;
//Fill in dot distances.
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;
}
else
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);
}
GameObject FindDot(string name)
{
foreach (GameObject dot in dots)
{
if (dot.name == name) return dot;
}
return null;
}
// Update is called once per frame
void Update () {
}
}