Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.60 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.IO;
  5.  
  6. public class BlockControl : MonoBehaviour
  7. {
  8.  
  9. public bool DisplayFeedback;//whether or not we tell them correct answer after the click -- needd for prac but not for exp
  10. public float FeedbackDuration;// How long to display feedback (seconds)
  11. public int NumberOfBlocks;//number of blocks per condition (for me, maybe 2 if allow 3 breaks total)
  12. public float ITI;// Inter-trial interval (seconds) between trials within block
  13. public string[] randomized;//a bunch of "arc" and "sin" intermixed randomly
  14. public float[] rand1;//random variable for variance
  15. public float[] rand2;//random variable for variance
  16. public float[] rand3;//random variable for variance
  17. public bool editDone;//have triangles been drawn? (i.e. has edittriangles been called?)
  18.  
  19. // Tracking stuff (recorded in output)
  20. public int CurrentBlock;//block number
  21. public string CurrentCondition;//arc or sin
  22. public int GlobalTrial;//global trial number
  23. public int TrialWithinBlock;//within-block trial number
  24. public string PracOrExp;//either "prac" or "exp"
  25. public string CompoundCondition;//"arclock" "arcleader" "curvylock" "curvyleader"
  26. public float x1,x2,x3,x4,x5,x6,x7,x8,x9,y1,y2,y3,y4,y5,y6,y7,y8,y9;//coordinates of 9 triangles
  27. public float StartTime;//start of trial
  28. public float OutClickTime1;//time of first click
  29. public float OutClickTime2;//time of second click
  30. public float VertClickPos;//y coordinate of second click
  31.  
  32. // Experiment Info
  33. private int nConditions;//2 conditions (leader or lock)
  34. public string[] SetOfConditions;//leader or lock
  35. public string[] ConditionList;//randomized order of leader or lock
  36. private string[] PracOrExpList;//preset with number of practice and number of experimental
  37. public int nPracBlocks;//1 practice block per condition
  38. public int nBlocks;//1 block per condition
  39. public int nPracTrials;//5 practice trials per block
  40. public int nTrialsPerBlock;//100 experimental trials per block
  41. private int[] trialCounts;//list of trial counts for each block -- used to know when block is over
  42. public int maxTrials;//total trials over all blocks (summation of trialcounts)
  43.  
  44. // Flow Control crap
  45. private bool startOfTrial = false;
  46. private bool waitingOnFeedback = false;
  47. private bool waitingOnITI = false;
  48. // public bool listenForResponse = false;
  49. private bool readyToStart = false;
  50. public bool EndOfTrial = false;
  51. public bool EndOfBlock = false;
  52.  
  53. public bool waitingOnCleanup = false;//routine that destroys all objects on screen -- bool controls cleanup
  54. private bool dotsOnScreen;
  55. private Vector2 clickLocation;
  56. private Vector2 intersectionPoint;//calculated center of mass
  57. private GameObject[] dotObjects;
  58.  
  59. // Links to other Classes
  60. public DisplayInstructions DisplayInstructions;
  61. private StartExp StartExp;
  62. private curvy_lock_script curvylock;
  63. private curvy_leader_script curvyleader;
  64. private arc_lock_script arclock;
  65. private arc_leader_script arcleader;
  66. //add all the other ones too? like endexperiment writeout do we need those?
  67.  
  68. private string[] SimpleOutputArray;//where simple output file lines are created
  69. private StreamWriter SimpleSW;//reference to other one inside StartExp
  70.  
  71. public float trialDuration; // Length of a movement trial in seconds // like 7-7.5 seconds
  72.  
  73. // Use this for initialization
  74. void Start ()
  75. {
  76. // Links to other Classes
  77. DisplayInstructions = this.GetComponentInParent<DisplayInstructions> ();
  78. StartExp = this.GetComponentInParent<StartExp> ();
  79. curvylock = this.GetComponentInParent<curvy_lock_script> () as curvy_lock_script;
  80. curvyleader = this.GetComponentInParent<curvy_leader_script> () as curvy_leader_script;
  81. arclock = this.GetComponentInParent<arc_lock_script> () as arc_lock_script;
  82. arcleader = this.GetComponentInParent<arc_leader_script> () as arc_leader_script;
  83.  
  84. // Default Values
  85. DisplayFeedback = true;
  86. FeedbackDuration = 1.0f; // seconds
  87. ITI = 0.5f;
  88. CurrentBlock = 0;
  89. readyToStart = true;
  90.  
  91. NumberOfBlocks = nConditions * (nPracBlocks + nBlocks);
  92.  
  93. int i = 0; // counter for blocks
  94. trialCounts = new int[NumberOfBlocks]; // list of trial counts for each block
  95. PracOrExpList = new string[NumberOfBlocks]; // Mark each block as practice or experiment
  96. ConditionList = new string[NumberOfBlocks]; // locklockleaderleder or vice versa
  97. for (int nc = 0; nc < nConditions; nc++) {
  98. for (int np = 1; np <= nPracBlocks; np++) {
  99. trialCounts [i] = nPracTrials;
  100. PracOrExpList [i] = "Practice";
  101. ConditionList[i] = SetOfConditions[nc];
  102. i++;
  103. }
  104. for (int ne = 1; ne <= nBlocks; ne++) {
  105. trialCounts [i] = nTrialsPerBlock;
  106. PracOrExpList [i] = "Experiment";
  107. ConditionList[i] = SetOfConditions[nc];
  108. i++;
  109. }
  110. }
  111.  
  112. //randomized list of 50 arcs and 50 curvys
  113. for(int j=1;j<=50;j++)
  114. randomized[j]="arc";
  115. for(int j=51;j<=100;j++)
  116. randomized[j]="curvy";
  117. System.Array.Sort(randomized, RandomSort);
  118. //now 1-100 are set with the experimental trials
  119. //but we want 1-5 to be practice, then 6-105 to be exp
  120. for(int j=105;j>=6;j--)
  121. randomized[j]=randomized[j-5];
  122.  
  123. //now populate 1-5
  124. for(int j=1;j<=5;j++)
  125. {
  126. if (j%2==0) randomized[j]="arc";
  127. if (j%2==1) randomized[j]="curvy";
  128. }
  129.  
  130. for(int j=1;j<=105;j++)
  131. {
  132. if (randomized[j]=="arc")
  133. {
  134. rand1[j] = Random.Range (0.050f,0.090f);
  135. rand2[j] = Random.Range (-0.007f,-0.009f);
  136. rand3[j] = Random.Range (1.0f,1.2f);
  137. }
  138. if (randomized[j]=="curvy")
  139. {
  140. rand1[j] = Random.Range (0.050f,0.090f);
  141. rand2[j] = Random.Range (0.130f,0.170f);
  142. rand3[j] = Random.Range (3.2f,4.8f);
  143. }
  144. }
  145. }
  146.  
  147. // Update is called once per frame
  148. void FixedUpdate ()
  149. {
  150. dotsOnScreen = false;
  151. if (curvylock.dotsOnScreen==true||curvyleader.dotsOnScreen==true||
  152. arclock.dotsOnScreen==true||arcleader.dotsOnScreen==true)
  153. dotsOnScreen=true;
  154.  
  155. //clickLocation = curvylock.clickLocation;
  156. //figure out how to make this work for everything
  157.  
  158. // Deal with GUI info and start the first block
  159. if (readyToStart && StartExp.DoneWithTheGUI) {
  160.  
  161. nConditions = SetOfConditions.Length;
  162. maxTrials = nConditions * ((nPracBlocks * nPracTrials) + (nBlocks * nTrialsPerBlock));
  163. // Debug.Log (initialTrialOrderList.Length);
  164.  
  165. StartCoroutine (StartBlock ());//startcoroutine starts another function but on another thread
  166. //startblock starts the block lol
  167. readyToStart = false;//already started, don't start something that's still going
  168. }
  169.  
  170. // End of Trial
  171. if (EndOfTrial == true & waitingOnCleanup == false & dotsOnScreen == true) {//trial's over, still stuff on screen we want to clean
  172. if (CompoundCondition=="curvylock") StartCoroutine (curvylock.cleanUpDots());
  173. if (CompoundCondition=="curvyleader") StartCoroutine (curvyleader.cleanUpDots());
  174. if (CompoundCondition=="arclock") StartCoroutine (arclock.cleanUpDots());
  175. if (CompoundCondition=="arcleader") StartCoroutine (arcleader.cleanUpDots());
  176. /*if (DisplayFeedback == true) {//if it's in practice
  177. StartCoroutine (WaitForFeedback ());//waits for feedback duration while LoadDotFamily shows feedback stuff
  178. }*/ // shouldn't need this because of wait function inside the 4 movement practice ifs
  179.  
  180. // Save the data
  181. //Vector2 pxIntersectionPoint = Camera.main.WorldToScreenPoint (intersectionPoint);//"correct" point of intersection, concerted from unity coord
  182. //to screen coord
  183. //Vector2 pxClickLocation = Camera.main.WorldToScreenPoint (clickLocation);//click location, converted from unity coord to screen cord
  184. //float errorDistance = Mathf.Abs (pxIntersectionPoint.y - pxClickLocation.y);//distance between true intersection and click
  185.  
  186. //Vector2 pxCenterOfEnclosingCircle = Camera.main.WorldToScreenPoint(LoadDotFamily.centerOfEnclosingCircle);
  187. //Vector2 pxCenterOfPointMass = Camera.main.WorldToScreenPoint(LoadDotFamily.centerOfPointMass);
  188.  
  189. //right now set up so that calculation of center of mass/convex hull is in data analysis
  190. //if have time this week, just do here
  191.  
  192. // HeaderLine = SimpleHeaderLine = "Experiment\tSID\tBlockNumber\tCondition\tPracOrExp\tGlobalTrialNo
  193. // \tTrialWithinBlock\
  194. // tx1\ty1\tx2\ty2\tx3\ty3\tx4\ty4" +
  195. // "x5\ty5\tx6\ty6\tx7\ty7\tx8\ty8\tx9\ty9\tTimeStart\tTimeClick1\tTimeClick2\tVertClickPos\tCenterOfMassVert"
  196. SimpleOutputArray = new string[] {
  197. StartExp.ExperimentName,
  198. StartExp.SID,
  199. CurrentBlock.ToString (),
  200. CurrentCondition,
  201. PracOrExp,
  202. GlobalTrial.ToString (),
  203. TrialWithinBlock.ToString (),
  204. x1.ToString (),y1.ToString (),x2.ToString (),y2.ToString (),x3.ToString (),y3.ToString (),
  205. x4.ToString (),y4.ToString (),x5.ToString (),y5.ToString (),x6.ToString (),y6.ToString (),
  206. x7.ToString (),y7.ToString (),x8.ToString (),y8.ToString (),x9.ToString (),y9.ToString (),
  207. StartTime.ToString (),
  208. OutClickTime1.ToString (),
  209. OutClickTime2.ToString(),
  210. VertClickPos.ToString()
  211. };
  212.  
  213. string SimpleLine = string.Join ("\t", SimpleOutputArray);
  214. // Debug.Log (trialLine);
  215. StartExp.SimpleSW.WriteLine (SimpleLine);//writes to buffer, then to file because of auto flush
  216. //simple stuff is done, move on to complex
  217.  
  218. //string[] dotArray = {
  219. // StartExp.ExperimentName,
  220. // StartExp.SID,
  221. // CurrentBlock.ToString (),
  222. // CurrentCondition,
  223. // PracOrExp,
  224. // GlobalTrial.ToString (),
  225. // TrialWithinBlock.ToString (),
  226. // dotObjects.Length.ToString ()
  227. //};
  228. //string dotOutputLine = string.Join ("\t", dotArray);
  229. //for (int i = 0; i < dotObjects.Length; i++) {
  230. // dotOutputLine = dotOutputLine + "\t" + dotObjects [i].transform.position.x.ToString () + "\t" + dotObjects [i].transform.position.y.ToString ();
  231. //}
  232. //StartExp.SimpleSW.WriteLine (dotOutputLine);
  233.  
  234. StartExp.ComplexSW.Flush(); // Ensure the eye tracking output gets written to disk at least once a trial
  235. // ComplexSW will be collecting stuff 60 Hz, but only actually go to disk
  236. // here, which is per-trial. this entire if statement is end of trial stuff
  237.  
  238. waitingOnCleanup = true;//CleanUpDots will change this to false in the background
  239. }
  240.  
  241. if (EndOfTrial == true & waitingOnCleanup == false & dotsOnScreen == false) {//now screen is cleared
  242. // Check for end of trial
  243. if (TrialWithinBlock == trialCounts [CurrentBlock - 1]) {//if block is over
  244. // Call for end of block
  245. EndOfBlock = true;//see next if
  246. } else {
  247. // On to the next trial
  248. StartCoroutine (WaitForITI ());
  249. startOfTrial = true;
  250. }
  251.  
  252. EndOfTrial = false;//we just started a new block or a new trial
  253. }
  254.  
  255. if (EndOfBlock == true) {
  256. StartCoroutine (EndBlock ());//ends block (probably)
  257. }
  258.  
  259. if (startOfTrial == true & waitingOnFeedback == false & waitingOnITI == false) {
  260. AdvanceTrial ();//no more feedback, no more waiting, move on to next trial
  261. }
  262.  
  263. }
  264.  
  265. IEnumerator StartBlock ()//starts block, "IEnumerator" is a coroutine thing :)
  266. {
  267. EndOfBlock = false;//block is starting
  268. CurrentBlock++;//started a new block, so increment
  269. CurrentCondition = ConditionList [CurrentBlock - 1];//matches condition to block number
  270. //CurrentCondition = "None"; <3 debug
  271.  
  272. PracOrExp = PracOrExpList [CurrentBlock - 1];//matches prac/exp to block number
  273.  
  274. TrialWithinBlock = 0;//reset
  275.  
  276. if (CurrentBlock == 1) {//the 0th block, but it got incremented at the start and became 1-indexed
  277. yield return StartCoroutine (DisplayInstructions.StartExperimentMessage ());//please wait for a click
  278. }
  279.  
  280. yield return StartCoroutine (DisplayInstructions.DisplayBlockInstructions (CurrentCondition, PracOrExp == "Practice"));
  281. //pracorexp is 1 or 0 thing
  282. yield return StartCoroutine (WaitForITI ());//chill for like a second
  283. AdvanceTrial ();//just first trial
  284. }
  285.  
  286. IEnumerator EndBlock ()//ends block
  287. {
  288. EndOfBlock = false;//because we're ending it
  289. if (CurrentBlock == NumberOfBlocks) {//then we're done with everything
  290. yield return StartCoroutine (DisplayInstructions.EndOfExperimentMessage ());//and we end the experiment
  291. this.GetComponentInParent<EndExperiment> ().EndExperimentCleanup ();//kill everything
  292. } else {//we're not done and we need another block
  293. yield return StartCoroutine (DisplayInstructions.EndOfBlockMessage ());//kill current block
  294. StartCoroutine (StartBlock ());//do next block
  295. }
  296. }
  297.  
  298. IEnumerator WaitForFeedback ()//waits for feedback
  299. {
  300. waitingOnFeedback = true;
  301. yield return new WaitForSeconds (FeedbackDuration);//wait for waitforseconds to finish (waits for return fro mthing)
  302. // FeedbackText.text = "";
  303. waitingOnFeedback = false;
  304. }
  305.  
  306. IEnumerator WaitForITI ()//waits for the inter trial thing
  307. {
  308. waitingOnITI = true;
  309. yield return new WaitForSeconds (ITI);//wait x seconds between trials
  310. waitingOnITI = false;
  311. }
  312.  
  313. void AdvanceTrial ()//go on to next trial
  314. {
  315. // Debug.Log ("AdvanceTrial started");
  316. GlobalTrial++;
  317. TrialWithinBlock++;//starts at 0, but gets incremented here to start at 1
  318. startOfTrial = false;//because we're starting it
  319. editDone = false;
  320.  
  321. //currentDotCount = 12;
  322.  
  323. //COME BACK AND FIX LATER WHEN OTHER SCRIPTS DONE
  324.  
  325. //some condition gets changed and then fixedupdate goes apeshit
  326.  
  327. //this.GetComponentInParent<InitiateTrial> ().StartTrial ();//starts trial
  328. //have something here that starts the animations and marks time at start of trial
  329. if (ConditionList[CurrentBlock]=="lock")//means lock
  330. //for now just use this to test curvylock, later will mean practice
  331. {
  332. if (randomized[TrialWithinBlock]=="arc")
  333. {
  334. CompoundCondition="arclock";
  335. }
  336. else if (randomized[TrialWithinBlock]=="curvy")
  337. {
  338. CompoundCondition="curvylock";
  339. }
  340. }
  341. if (ConditionList[CurrentBlock]=="leader")//means leader
  342. {
  343. if (randomized[TrialWithinBlock]=="arc")
  344. {
  345. CompoundCondition="arcleader";
  346. }
  347. else if (randomized[TrialWithinBlock]=="curvy")
  348. {
  349. CompoundCondition="curvyleader";
  350. }
  351. }
  352. if (PracOrExpList[CurrentBlock]=="Practice")
  353. {
  354. PracOrExp="Practice";
  355. }
  356. if (PracOrExpList[CurrentBlock]=="Experiment")
  357. {
  358. PracOrExp="Experiment";
  359. }
  360. }
  361.  
  362. int RandomSort (string a, string b)//-1 or 1 function
  363. {
  364. return UnityEngine.Random.Range (-1, 1);
  365. }
  366.  
  367. int[] reshuffle(int[] elements)//shuffle function
  368. {
  369. // Knuth shuffle algorithm :: courtesy of Wikipedia :)
  370. for (int t = 0; t < elements.Length; t++ )
  371. {
  372. int tmp = elements[t];
  373. int r = Random.Range(t, elements.Length);
  374. elements[t] = elements[r];
  375. elements[r] = tmp;
  376. }
  377. return elements;
  378. }
  379.  
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement