123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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<float>, List<Vector3>, List<Quaternion>, List<float>>[][] 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<InstantiatePrefab>().humanGameObject;
- NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
- stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
- // Read from CSV file and save time, position, rotation in matrix
- int index = gameObject.GetComponent<WriteInCSV>().index;
- string dir = Directory.GetCurrentDirectory();
- string reference = @"\Assets\Data_position\Walk" + index + ".csv";
- timePosRotList = gameObject.GetComponent<ReadFromCSV>().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<MeshRenderer>().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<MeshRenderer>().material.color = Color.green;
- // count++;
- // }
- //}
- //currentStartPoint++;
- GameObject newPath = new GameObject();
- LineRenderer drawPath = newPath.AddComponent<LineRenderer>();
- 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<Vector3> newList = new List<Vector3>();
- 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;
- }
- }
|