//This function initializes the Data.singleton files public static bool ExternalActivation(string inputFile) { if (!inputFile.Contains(".json")) { return(false); } DS.Load(inputFile); Directory.CreateDirectory(C.OutputDirectory); return(true); }
// Load the next trial public virtual void Progress() { Debug.Log("Progressing..."); // Exiting current trial TrialProgress.PreviousTrial = this; if (TrialProgress.Instructional != 1) { var blockData = DS.GetData().Blocks[BlockID]; if (blockData.TrialFunction != null) { var trialFunction = blockData.TrialFunction; var func = typeof(Functions).GetMethod(trialFunction, BindingFlags.Static | BindingFlags.Public); var result = func != null && (bool)func.Invoke(null, new object[] { TrialProgress }); if (!result) { var tmp = next; while (!tmp.isTail) { tmp = tmp.next; } Loader.Get().CurrTrial = tmp.next; tmp.next.PreEntry(TrialProgress); return; } } // Data on how to choose the next trial will be selected here. if (isTail) { if (blockData.BlockFunction != null) { var tmp = blockData.BlockFunction; var func = typeof(Functions).GetMethod(tmp, BindingFlags.Static | BindingFlags.Public); var result = func != null && (bool)func.Invoke(null, new object[] { TrialProgress }); if (result) { Loader.Get().CurrTrial = head; head.PreEntry(TrialProgress, false); return; } } } } Loader.Get().CurrTrial = next; next.PreEntry(TrialProgress); progressionComplete = true; }
public static void LogHeaders() { using (var writer = new StreamWriter("Assets/OutputFiles~/" + DS.GetData().OutputFile, false)) { writer.Write( "StartField1, StartField2, StartField3, StartField4, TimeStamp, BlockIndex, TrialIndex, TrialInBlock, Instructional, 2D, Scene, Enclosure, PositionX, PositionY, PositionZ, RotationY, " + "Collision, GoalX, GoalZ, LastX, LastZ, UpArrow, DownArrow," + " LeftArrow, RightArrow, Space" + "\n"); writer.Flush(); writer.Close(); } }
// Start the character. If init from enclosure, this allows "s" to determine the start position public void ExternalStart(float pickX, float pickY, bool useEnclosure = false) { while (!_isStarted) { Thread.Sleep(20); } TrialProgress.GetCurrTrial().TrialProgress.TargetX = pickX; TrialProgress.GetCurrTrial().TrialProgress.TargetY = pickY; // No start pos specified so make it random. if (E.Get().CurrTrial.trialData.StartPosition.Count == 0) { // Try to randomly place the character, checking for proximity // to the pickup location var i = 0; while (i++ < 100) { var CurrentTrialRadius = DS.GetData().Enclosures[E.Get().CurrTrial.TrialProgress.CurrentEnclosureIndex].Radius; var v = Random.insideUnitCircle * CurrentTrialRadius * 0.9f; var mag = Vector3.Distance(v, new Vector2(pickX, pickY)); if (mag > DS.GetData().CharacterData.DistancePickup) { transform.position = new Vector3(v.x, 0.5f, v.y); var camPos = Cam.transform.position; camPos.y = DS.GetData().CharacterData.Height; Cam.transform.position = camPos; return; } } Debug.LogError("Could not randomly place player. Probably due to" + " a pick up location setting"); } else { var p = E.Get().CurrTrial.trialData.StartPosition; if (useEnclosure) { p = new List <float>() { pickX, pickY }; } transform.position = new Vector3(p[0], 0.5f, p[1]); var camPos = Cam.transform.position; camPos.y = DS.GetData().CharacterData.Height; Cam.transform.position = camPos; } }
// Generates the landmarks, pretty similar to the data in pickup. private void GenerateLandmarks() { foreach (var p in E.Get().CurrTrial.trialData.LandMarks) { var d = DS.GetData().Landmarks[p - 1]; GameObject prefab; GameObject landmark; if (d.Type.ToLower().Equals("3d")) { prefab = (GameObject)Resources.Load("3D_Objects/" + d.Object, typeof(GameObject)); landmark = Instantiate(prefab); landmark.AddComponent <RotateBlock>(); landmark.GetComponent <Renderer>().material.color = Data.GetColour(d.Color); } else { // Load the "2D" prefab here, so we have the required components prefab = (GameObject)Resources.Load("3D_Objects/2D", typeof(GameObject)); landmark = Instantiate(prefab); var spriteName = d.Object; var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + spriteName); landmark.GetComponent <SpriteRenderer>().sprite = pic; } landmark.transform.localScale = d.ScaleVector; try { landmark.transform.position = d.PositionVector; } catch (Exception _) { landmark.transform.position = new Vector3(d.PositionVector.x, 0.5f, d.PositionVector.z); } //landmark.transform.Rotate(new Vector3(0, 1, 0), d.InitialRotation); landmark.transform.Rotate(d.RotationVector); landmark.GetComponent <Renderer>().material.color = Data.GetColour(d.Color); var sprite = d.ImageLoc; if (sprite != null) { var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + sprite); landmark.GetComponent <SpriteRenderer>().sprite = pic; } _created.Add(landmark); } }
public static void LogData(TrialProgress s, long trialStartTime, Transform t, int targetFound = 0) { // Don't output anything if the Y position is at default (avoids incorrect output data) if (t.position.y != -1000 && (targetFound == 1 || _timer > 1f / (DS.GetData().OutputTimesPerSecond == 0 ? 1000 : DS.GetData().OutputTimesPerSecond))) { using (var writer = new StreamWriter("Assets/OutputFiles~/" + DS.GetData().OutputFile, true)) { var PositionX = t.position.x.ToString(); var PositionZ = t.position.z.ToString(); var PositionY = t.position.y.ToString(); var RotationY = t.eulerAngles.y.ToString(); var timeSinceExperimentStart = DateTimeOffset.Now.ToUnixTimeMilliseconds() - DataSingleton.GetData().ExperimentStartTime; var timeSinceTrialStart = DateTimeOffset.Now.ToUnixTimeMilliseconds() - trialStartTime; if (s.Instructional == 1) { PositionX = "NA"; PositionZ = "NA"; PositionY = "NA"; RotationY = "NA"; } var str = string.Format( "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, " + "{12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}", s.Field1, s.Field2, s.Field3, s.Field4, DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), s.BlockID + 1, s.TrialID + 1, s.TrialNumber + 1, s.Instructional, s.TwoDim, s.EnvironmentType, s.CurrentEnclosureIndex + 1, PositionX, PositionY, PositionZ, RotationY, targetFound, s.TargetX, s.TargetY, s.LastX, s.LastY, Input.GetKey(KeyCode.UpArrow) ? 1 : 0, Input.GetKey(KeyCode.DownArrow) ? 1 : 0, Input.GetKey(KeyCode.LeftArrow) ? 1 : 0, Input.GetKey(KeyCode.RightArrow) ? 1 : 0, Input.GetKey(KeyCode.Space) ? 1 : 0); writer.Write(str + "\n"); writer.Flush(); writer.Close(); } _timer = 0; } _timer += Time.deltaTime; }
// Use this for initialization private void Start() { var gen = GameObject.Find("WallCreator").GetComponent <GenerateGenerateWall>(); _destroy = new List <GameObject>(); //This initializes the food object destroy list var activeGoals = E.Get().CurrTrial.trialData.ActiveGoals; var inactiveGoals = E.Get().CurrTrial.trialData.InactiveGoals; var invisibleGoals = E.Get().CurrTrial.trialData.InvisibleGoals; var inactiveSet = new HashSet <int>(inactiveGoals); var invisibleSet = new HashSet <int>(invisibleGoals); var activeSet = new HashSet <int>(activeGoals); var merged = new List <int>(); merged.AddRange(activeGoals); merged.AddRange(inactiveGoals); merged.AddRange(invisibleGoals); Data.Point p = new Data.Point { X = 0, Y = 0, Z = 0 }; foreach (var val in merged) { var goalItem = DS.GetData().Goals[Mathf.Abs(val) - 1]; UnityEngine.Debug.Log(goalItem); // Position is not set in the config file if (goalItem.Position.Count == 0) { p = ReadFromExternal(goalItem.PythonFile); } else { try { p = new Data.Point { X = goalItem.PositionVector.x, Y = goalItem.PositionVector.y, Z = goalItem.PositionVector.z }; } catch (Exception _) { p = new Data.Point { X = goalItem.PositionVector.x, Y = 0.5f, Z = goalItem.PositionVector.z }; } } GameObject prefab; GameObject obj; var spriteName = ""; if (goalItem.Type.ToLower().Equals("3d")) { prefab = (GameObject)Resources.Load("3D_Objects/" + goalItem.Object, typeof(GameObject)); obj = Instantiate(prefab); obj.AddComponent <RotateBlock>(); } else { // Load the "2D" prefab here, so we have the required components prefab = (GameObject)Resources.Load("3D_Objects/" + goalItem.Type.ToUpper(), typeof(GameObject)); obj = Instantiate(prefab); spriteName = goalItem.Object; } obj.transform.Rotate(goalItem.RotationVector); obj.transform.localScale = goalItem.ScaleVector; obj.transform.position = new Vector3(p.X, p.Y, p.Z); obj.AddComponent <PickupSound>(); obj.GetComponent <PickupSound>().Sound = Resources.Load <AudioClip>("Sounds/" + goalItem.Sound); if (!string.IsNullOrEmpty(spriteName)) { var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + spriteName); obj.GetComponent <SpriteRenderer>().sprite = pic; } var color = Data.GetColour(goalItem.Color); try { obj.GetComponent <Renderer>().material.color = color; obj.GetComponent <Renderer>().enabled = !invisibleSet.Contains(val); obj.GetComponent <Collider>().enabled = !inactiveSet.Contains(val); if (activeSet.Contains(val) || invisibleSet.Contains(val)) { obj.tag = "Pickup"; obj.GetComponent <Collider>().isTrigger = true; } } catch (Exception _) { print("Visibility not working"); } _destroy.Add(obj); } GameObject.Find("Participant").GetComponent <PlayerController>().ExternalStart(p.X, p.Z); }
// Use this for initialization private void Start() { var m = L.Get().CurrTrial.trialData.Map; var y = m.TopLeft[1]; // Goes through each map and initializes it based on stuff. foreach (var row in m.Map) { var x = m.TopLeft[0]; foreach (var col in row.ToCharArray()) { if (col == 'w') { var obj = GameObject.CreatePrimitive(PrimitiveType.Cube); obj.GetComponent <Renderer>().sharedMaterial.color = Data.GetColour(m.Color); obj.transform.localScale = new Vector3(m.TileWidth, L.Get().CurrTrial.enclosure.WallHeight, m.TileWidth); obj.transform.position = new Vector3(x, L.Get().CurrTrial.enclosure.WallHeight * 0.5f, y); } else if (col == 's') { Debug.Log(x + " " + y); GameObject.Find("Participant").GetComponent <PlayerController>().ExternalStart(x, y, true); } else if (col != '0') { // TODO: This code should be shared with PickupGenerator.cs - this class might also just be deadcode. var val = col - '0'; var goalItem = DS.GetData().Goals[val - 1]; GameObject prefab; GameObject obj; var spriteName = ""; if (goalItem.Type.ToLower().Equals("3d")) { prefab = (GameObject)Resources.Load("3D_Objects/" + goalItem.Object, typeof(GameObject)); obj = Instantiate(prefab); obj.AddComponent <RotateBlock>(); obj.GetComponent <Renderer>().material.color = Data.GetColour(goalItem.Color); } else { // Load the "2D" prefab here, so we have the required components prefab = (GameObject)Resources.Load("3D_Objects/" + goalItem.Type.ToUpper(), typeof(GameObject)); obj = Instantiate(prefab); spriteName = goalItem.Object; } obj.transform.localScale = goalItem.ScaleVector; obj.transform.position = new Vector3(x, 0.5f, y); if (!string.IsNullOrEmpty(spriteName)) { var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + spriteName); obj.GetComponent <SpriteRenderer>().sprite = pic; } } x += m.TileWidth; } y -= m.TileWidth; } }
private void Start() { try { var trialText = GameObject.Find("TrialText").GetComponent <Text>(); var blockText = GameObject.Find("BlockText").GetComponent <Text>(); var currBlockId = E.Get().CurrTrial.BlockID; // This section sets the text trialText.text = E.Get().CurrTrial.trialData.DisplayText; blockText.text = DS.GetData().Blocks[currBlockId].DisplayText; if (!string.IsNullOrEmpty(E.Get().CurrTrial.trialData.DisplayImage)) { var filePath = DS.GetData().SpritesPath + E.Get().CurrTrial.trialData.DisplayImage; var displayImage = GameObject.Find("DisplayImage").GetComponent <RawImage>(); displayImage.enabled = true; displayImage.texture = Img2Sprite.LoadTexture(filePath); } } catch (NullReferenceException e) { Debug.LogWarning("Goal object not set: running an instructional trial"); } Random.InitState(DateTime.Now.Millisecond); _currDelay = 0; // Choose a random starting angle if the value is not set in config if (E.Get().CurrTrial.trialData.StartFacing == -1) { _iniRotation = Random.Range(0, 360); } else { _iniRotation = E.Get().CurrTrial.trialData.StartFacing; } transform.Rotate(0, _iniRotation, 0); try { _controller = GetComponent <CharacterController>(); _gen = GameObject.Find("WallCreator").GetComponent <GenerateGenerateWall>(); Cam.transform.Rotate(0, 0, 0); } catch (NullReferenceException e) { Debug.LogWarning("Can't set controller object: running an instructional trial"); } _waitTime = E.Get().CurrTrial.trialData.Rotate; _reset = false; localQuota = E.Get().CurrTrial.trialData.Quota; // This has to happen here for output to be aligned properly TrialProgress.GetCurrTrial().TrialProgress.TrialNumber++; TrialProgress.GetCurrTrial().TrialProgress.Instructional = TrialProgress.GetCurrTrial().trialData.Instructional; TrialProgress.GetCurrTrial().TrialProgress.EnvironmentType = TrialProgress.GetCurrTrial().trialData.Scene; TrialProgress.GetCurrTrial().TrialProgress.CurrentEnclosureIndex = TrialProgress.GetCurrTrial().trialData.Enclosure - 1; TrialProgress.GetCurrTrial().TrialProgress.BlockID = TrialProgress.GetCurrTrial().BlockID; TrialProgress.GetCurrTrial().TrialProgress.TrialID = TrialProgress.GetCurrTrial().TrialID; TrialProgress.GetCurrTrial().TrialProgress.TwoDim = TrialProgress.GetCurrTrial().trialData.TwoDimensional; TrialProgress.GetCurrTrial().TrialProgress.LastX = TrialProgress.GetCurrTrial().TrialProgress.TargetX; TrialProgress.GetCurrTrial().TrialProgress.LastY = TrialProgress.GetCurrTrial().TrialProgress.TargetY; TrialProgress.GetCurrTrial().TrialProgress.TargetX = 0; TrialProgress.GetCurrTrial().TrialProgress.TargetY = 0; _isStarted = true; }
//We are gonna generate all the trials here. private void GenerateTrials() { Debug.Log("GenerateTrial"); AbstractTrial currentTrial = this; var i = BlockID; var block = DS.GetData().Blocks[i]; var n = block.RandomlySelect.Count; var randomSelection = Random.Range(0, n); var d = block.RandomlySelect[randomSelection]; if (block.Replacement == 0) { block.RandomlySelect.Remove(d); } Debug.Log("RANDOM TRIAL CREATION"); //Need this to remove the previously generated random trials. while (_numGenerated > 0) { //next = next.next; _numGenerated--; } var trueNext = next; var tCnt = 0; foreach (var j in d.Order) { //Here we decide what each trial is, I guess we could do this with a function map, but later. //here we have a picture as a trial. var trialData = DS.GetData().Trials[j - 1]; //Control flow here is for deciding what Trial gets spat out from the config AbstractTrial t; if (trialData.FileLocation != null) { Debug.Log("Creating new Loading Screen Trial"); t = new InstructionalTrial(i, j - 1); } else if (trialData.TwoDimensional == 1) { Debug.Log("Creating new 2D Screen Trial"); t = new TwoDTrial(i, j - 1); } else { Debug.Log("Creating new 3D Screen Trial"); t = new ThreeDTrial(i, j - 1); } t.isTail = tCnt == d.Order.Count - 1 && isTail; t.head = head; currentTrial.next = t; currentTrial = currentTrial.next; _numGenerated++; tCnt++; } currentTrial.next = trueNext; }