using UnityEngine; using UnityEngine.UI; using UnityEngine.AI; using System.Collections.Generic; using System.IO; using System; [DefaultExecutionOrder(40)] [RequireComponent(typeof(InstantiatePrefab), typeof(WriteInCSV), typeof(ReadFromCSV))] public class SpatialTimeSimulation : MonoBehaviour { // Waypoints and Rotations public Tuple, List, List, List>[][] timePosRotList; // Private Journey Settings private int currentStartPoint; private float startTime; // Global GameObjects private GameObject[][] humansGO; // Playback variables private int[] maxIJ; private int indexChangeRate; [HideInInspector] public bool rewind, pause, play; //PlaybackController // Slider Settings public Slider slider; private float prevSliderValue; // Thief Settings private Vector3 stealHere; // Testing public float thickness = 0.1f; private void Start() { // Get information from InstatiatePrefab humansGO = gameObject.GetComponent().humanGameObject; NavMeshAgent[][] humansNMA = gameObject.GetComponent().humanNavMeshAgent; stealHere = gameObject.GetComponent().stealHere; // Read from CSV file and save time, position, rotation in matrix int index = gameObject.GetComponent().index; string dir = Directory.GetCurrentDirectory(); string reference = @"\Assets\Data_position\Walk" + index + ".csv"; timePosRotList = gameObject.GetComponent().ReadFromCSVFile(dir + reference); // Set initial position and rotation currentStartPoint = 0; for (int i = 0; i < humansGO.Length; ++i) { for (int j = 0; j < humansGO[i].Length; ++j) { humansGO[i][j].SetActive(false); humansNMA[i][j].enabled = false; } } // Set the Slider settings if (slider == null) { Debug.Log("No Slider instance attached"); return; } maxIJ = GetHumanWithLongestScreenTime(); float maxTime = (float)Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2); slider.minValue = 0; slider.maxValue = (maxTime / Time.deltaTime); slider.wholeNumbers = true; prevSliderValue = slider.value; //SetTransform(currentStartPoint); //currentStartPoint++; slider.value = currentStartPoint; startTime = Time.time; int count = 0; // Create all cubes for every human and disable/enable them with time. GameObject[][] cubes = new GameObject[humansGO.Length][]; for (int i = 0; i < cubes.Length; ++i) { cubes[i] = new GameObject[humansGO[i].Length]; for (int j = 0; j < cubes[i].Length; ++j) { currentStartPoint = 0; while (currentStartPoint < timePosRotList[i][j].Item2.Count) { //if(currentStartPoint == 0 || currentStartPoint >= timePosRotList[i][j].Item2.Count - 1) //{ // cubes[i][j] = GameObject.CreatePrimitive(PrimitiveType.Cube); // cubes[i][j].transform.position = new Vector3(timePosRotList[i][j].Item2[currentStartPoint].x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], timePosRotList[i][j].Item2[currentStartPoint].z); // cubes[i][j].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); // cubes[i][j].GetComponent().material.color = Color.green; // count++; //} //else if(currentStartPoint < timePosRotList[i][j].Item2.Count - 1) //{ // if (Vector3.Distance(timePosRotList[i][j].Item2[currentStartPoint - 1], timePosRotList[i][j].Item2[currentStartPoint]) <= 0.2f) // { // cubes[i][j] = GameObject.CreatePrimitive(PrimitiveType.Cube); // cubes[i][j].transform.position = new Vector3(timePosRotList[i][j].Item2[currentStartPoint].x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], timePosRotList[i][j].Item2[currentStartPoint].z); // cubes[i][j].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); // cubes[i][j].GetComponent().material.color = Color.green; // count++; // } //} //currentStartPoint++; GameObject newPath = new GameObject(); LineRenderer drawPath = newPath.AddComponent(); drawPath.material = new Material(Shader.Find("Sprites/Default")); drawPath.startColor = Color.green; drawPath.endColor = Color.green; drawPath.startWidth = thickness; drawPath.endWidth = thickness; drawPath.positionCount = timePosRotList[i][j].Item2.Count; List newList = new List(); foreach(var vec in timePosRotList[i][j].Item2) { var z = new Vector3(vec.x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], vec.z); newList.Add(z); currentStartPoint++; } drawPath.SetPositions(newList.ToArray()); } } } Debug.Log("Count: " + count); } //private void FixedUpdate() //{ // if (pause) // indexChangeRate = 0; // if (play) // indexChangeRate = 1; // if (rewind) // indexChangeRate = -1; // if (play || rewind) // { // if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0) // { // if (slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play // { // prevSliderValue = slider.value; // currentStartPoint += indexChangeRate; // slider.value = currentStartPoint; // } // else // { // prevSliderValue = slider.value; // currentStartPoint = (int)slider.value; // } // SetTransform(currentStartPoint); // } // else if (slider.value - prevSliderValue != -1 || slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1) // { // currentStartPoint = (int)slider.value; // } // } // else // { // // upper else-Statement only for pause // if (slider.value - prevSliderValue != 0) // { // //prevSliderValue = slider.value; // currentStartPoint = (int)slider.value; // } // for (int i = 0; i < humansGO.Length; ++i) // { // for (int j = 0; j < humansGO[i].Length; ++j) // { // if (currentStartPoint < timePosRotList[i][j].Item2.Count) // { // humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position // humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation // } // } // } // } //} //private void SetTransform(int currentStartPoint) //{ // for (int i = 0; i < humansGO.Length; ++i) // { // for (int j = 0; j < humansGO[i].Length; ++j) // { // // if so ArgumentOutOfRangeException // if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0) // { // continue; // } // Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint]; // Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + indexChangeRate]; // Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint]; // Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + indexChangeRate]; // float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint]; // float fracJourney = 1f; // float journeyLength = Vector3.Distance(startPos, endPos); // if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength; // // Animation Idle, if startPoint == endPoint and startRot == endRot // //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count) // //{ // // humansA[i][j].SetBool("isWalking", false); // //} // //else if (journeyLength > 0) // //{ // // humansA[i][j].SetBool("isWalking", true); // //} // //humansA[i][j].speed = 2*(journeyLength / 0.2f); // humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney); // humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney); // } // } //} private int[] GetHumanWithLongestScreenTime() { int[] maxIJ = new int[2] { 0, 0 }; float currentMaxTime = 0; for (int i = 0; i < humansGO.Length; ++i) { for (int j = 0; j < humansGO[i].Length; ++j) { if (currentMaxTime < timePosRotList[i][j].Item1.Count) { currentMaxTime = timePosRotList[i][j].Item1.Count; maxIJ[0] = i; maxIJ[1] = j; } } } return maxIJ; } }