Untitled

 avatar
unknown
plain_text
a year ago
5.6 kB
20
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class GameManager : MonoBehaviour
{
    public GameObject left_box, right_box, cube; // Assign these in the Inspector
    public Material highlightMaterial;
    private Vector3 cubeStartPosition;
    private List<TrialData> trials = new List<TrialData>();
    private string originalFilePath = "trials.csv";
    private string copyFilePath = "results.csv";
    private bool selectionMade = false; // Flag to check if a selection has been made

    void Start()
    {
        originalFilePath = Path.Combine(Application.dataPath, originalFilePath);
        copyFilePath = Path.Combine(Application.dataPath, copyFilePath);
        cubeStartPosition = cube.transform.position;
        ReadCsvFile(originalFilePath);
        CreateCopyWithResponsesColumn(copyFilePath);
        StartCoroutine(RunTrials());
    }
    IEnumerator RunTrials()
    {
        foreach (var trial in trials)
        {
            selectionMade = false; // Reset the flag at the start of each trial
            GameObject objectToHighlight = trial.isProbeLeft ? left_box : right_box;  // StartCoroutine(HighlightObject(objectToHighlight)); it should be deleted
            GameObject referenceBox = trial.isProbeLeft ? right_box : left_box;
            StartCoroutine(HighlightObject(objectToHighlight, highlightMaterial));

            yield return new WaitForSeconds(Random.Range(0.3f, 0.5f)); // Wait for highlight duration
            // Make the object disappear for half a second and then reappear

            yield return StartCoroutine(ToggleObjectVisibility(objectToHighlight, 2.5f));
            yield return StartCoroutine(ToggleObjectVisibility(referenceBox, 0.0333f * 2));
            // Wait here until selectionMade becomes true
            Debug.Log($"here SO: {objectToHighlight}, {trial.SOA / 100f} ");

            yield return new WaitUntil(() => selectionMade);

            // Logic to process the selection
            // ...
            ResetCubePosition();
            yield return new WaitForSeconds(1.0f); // Short break before the next trial
        }
        // Handle all trials completed here
    }

    void ResetCubePosition()
    {

        cube.transform.position = cubeStartPosition;
    }


    // This method is now called directly from DraggableObject when a selection is made
    public void RegisterUserSelection(GameObject selectedObject)
    {
        // Logic to determine if the selected object is the correct one
        Debug.Log($"Selected object: {selectedObject.name}");
        // Implement logic to check if the selected object matches the expected answer
        // Then process the result and prepare for the next trial as necessary

        selectionMade = true; // Indicate that a selection has been made to continue the trials
    }

    // New method to toggle object visibility
    IEnumerator ToggleObjectVisibility(GameObject obj, float delay)
    {
        obj.SetActive(false);
        yield return new WaitForSeconds(delay);
        obj.SetActive(true);

    }
 

    IEnumerator HighlightObject(GameObject obj, Material highlightMaterial)
{
    Renderer objRenderer = obj.GetComponent<Renderer>();

    // Speichere das ursprüngliche Material, um es später wiederherzustellen
    Material originalMaterial = objRenderer.material;

    // Setze das Hervorhebungs-Material
    objRenderer.material = highlightMaterial;

    yield return new WaitForSeconds(0.5f); // Hervorhebungsdauer

        // Stelle das ursprüngliche Material wieder her, it should be deleted
        //objRenderer.material = originalMaterial; it should be deleted
    }

    void ResetTrialSetup()
    {
        // Implement resetting logic as needed, for instance re-enabling renderers
    }



    void ReadCsvFile(string filePath)
        {
            Debug.Log("here read CSV.");

        try
        {
            string[] lines = File.ReadAllLines(filePath);
            for (int i = 1; i < lines.Length; i++) // Start at 1 to skip the header row
            {
                string[] columns = lines[i].Split(',');
                if (columns.Length == 2)
                {
                    float soa = float.Parse(columns[0]);
                    bool isProbeLeft = columns[1] == "1";
                    trials.Add(new TrialData(soa, isProbeLeft));
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Error reading CSV file: {e.Message}");
        }
    }

    void CreateCopyWithResponsesColumn(string copyFilePath)
    {
        Debug.Log("here copy CSV.");

        try
        {
            string[] lines = File.ReadAllLines(originalFilePath);
            using (StreamWriter sw = new StreamWriter(copyFilePath))
            {
                // Write header with an extra 'response' column
                sw.WriteLine($"{lines[0]},response");

                // Write the rest of the lines as is, but with an extra comma for the new column
                for (int i = 1; i < lines.Length; i++)
                {
                    sw.WriteLine($"{lines[i]},3");
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Error creating copy of CSV file: {e.Message}");
        }
    }

    // Define a structure to hold the trial data
    public struct TrialData
    {
        public float SOA;
        public bool isProbeLeft;

        public TrialData(float soa, bool isProbeLeft)
        {
            this.SOA = soa;
            this.isProbeLeft = isProbeLeft;
  }
}

}
Editor is loading...
Leave a Comment