FittsLaw/Assets/Scripts/ExperimentController.cs
2018-11-28 19:09:50 -05:00

169 lines
5.7 KiB
C#

using Assets.Scripts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
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;
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 = "rift";
#endif
//FOR THE LOVE OF GOD FUCKING DELETE THIS, SERIOUSLY CHRIS DON'T YOU DARE FORGET
string fuck = "ewoidHJpYWxzIiA6IFsKICBbIkxlZnRMb25nIiwiUmlnaHRNZWRpdW0iLCJMZWZ0U2hvcnQiLCJVcE1lZGl1bSIsIlVwTG9uZyIsIlJpZ2h0U2hvcnQiXSwKICBbIlVwU2hvcnQiLCJSaWdodExvbmciLCJMZWZ0TWVkaXVtIiwiUmlnaHRNZWRpdW0iLCJMZWZ0TG9uZyIsIlVwTG9uZyJdLAogIFsiUmlnaHRTaG9ydCIsIlVwTWVkaXVtIiwiTGVmdE1lZGl1bSIsIkxlZnRTaG9ydCIsIlJpZ2h0TG9uZyIsIlVwU2hvcnQiXSwKICBbIkxlZnRMb25nIiwiUmlnaHRNZWRpdW0iLCJMZWZ0U2hvcnQiLCJVcE1lZGl1bSIsIlVwTG9uZyIsIlJpZ2h0U2hvcnQiXSwKICBbIlVwU2hvcnQiLCJSaWdodExvbmciLCJMZWZ0TWVkaXVtIiwiUmlnaHRNZWRpdW0iLCJMZWZ0TG9uZyIsIlVwTG9uZyJdLAogIFsiUmlnaHRTaG9ydCIsIlVwTWVkaXVtIiwiTGVmdE1lZGl1bSIsIkxlZnRTaG9ydCIsIlJpZ2h0TG9uZyIsIlVwU2hvcnQiXSwKICBbIkxlZnRMb25nIiwiUmlnaHRNZWRpdW0iLCJMZWZ0U2hvcnQiLCJVcE1lZGl1bSIsIlVwTG9uZyIsIlJpZ2h0U2hvcnQiXSwKICBbIlVwU2hvcnQiLCJSaWdodExvbmciLCJMZWZ0TWVkaXVtIiwiUmlnaHRNZWRpdW0iLCJMZWZ0TG9uZyIsIlVwTG9uZyJdLAogIFsiUmlnaHRTaG9ydCIsIlVwTWVkaXVtIiwiTGVmdE1lZGl1bSIsIkxlZnRTaG9ydCIsIlJpZ2h0TG9uZyIsIlVwU2hvcnQiXQpdLAoicGFydGljaXBhbnRJZCIgOiAxLAoiaW5wdXRNZXRob2QiIDogImNhcmRib2FyZCIKfQo=";
byte[] decodedBytes = Convert.FromBase64String(fuck);
string decodedText = Encoding.UTF8.GetString(decodedBytes);
UnityEngine.Debug.Log(decodedText);
participant = new Participant(decodedText);
startDot = GameObject.Find("StartButton");
playerCamera = GameObject.FindGameObjectsWithTag("MainCamera")[0];
trialIndex = 0;
trialDotIndex = 0;
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Dot"))
{
dots.Add(go);
Hide(go);
};
}
void Hide(GameObject dot)
{
dot.SetActive(false);
}
void Show(GameObject dot)
{
dot.SetActive(true);
}
public void NextDot()
{
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);
SaveSample(angle, distance, time, false);
}
if (trialDotIndex < participant.trials[trialIndex].Length)
{
nextDot = FindDot(participant.trials[trialIndex][trialDotIndex]);
trialDotIndex++;
Show(nextDot);
Hide(startDot);
}
else if(trialIndex < participant.trials.Length)
{
// NEXT TRIAL SHIT
trialIndex++;
trialDotIndex = 0;
dataSet.SaveDataSet();
dataSet.Samples.Clear();
nextDot = FindDot(participant.trials[trialIndex][trialDotIndex]);
Show(nextDot);
Hide(startDot);
}
else
{
dataSet.SaveDataSet();
}
stopwatch.Start();
}
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);
SaveSample(angle, distance, time, true);
Hide(nextDot);
Show(startDot);
stopwatch.Start();
}
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 distance, double time, bool direction)
{
Sample sample = new Sample();
sample.Angle = angle;
sample.Distance = distance;
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 () {
}
}