123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System.Collections;
- using System.Collections.Generic;
- using CSVReader;
- using Logger;
- using UnityEngine;
- namespace ObjectScripts
- {
- public class DataObject : MonoBehaviour
- {
- // Damit DataObject threadsafe ist, so wenig berechnungen wie möglich
- // Mann kann nicht Threadsafe auf Unity Objects außerhalb des Unity Main Threads zugreifen
- ///<summary>
- /// timestamp of last update
- ///</summary>
- public double Timestamp;
- ///<summary>
- /// Position to rotate towards
- ///</summary>
- public Vector3 Rot;
- ///<summary>
- /// Position too look at
- ///</summary>
- public Vector3 HeadRot;
- ///<summary>
- /// Top Color
- ///</summary>
- public uint TopColor;
- ///<summary>
- /// Bottom Color
- ///</summary>
- public uint BotColor;
- ///<summary>
- /// Entitiy Type
- ///</summary>
- public CSVReader.EntityType Type;
- ///<summary>
- /// ID
- ///</summary>
- public int ID { get; set; }
- ///<summary>
- /// ObjectHandler which handles the object
- ///</summary>
- public AbstractObjectHandler Handler { get; internal set; }
- ///<summary>
- /// The time since the last update occurred.
- /// Updated every timerRateInSeconds.
- ///</summary>
- public float TimeSinceLastUpdate { get; internal set; }
- ///<summary>
- /// Maximum Time the object can live without an update
- ///</summary>
- public float TimeToLive { get; internal set; }
- ///<summary>
- /// Rate for Updating TimeSinceLastUpdate
- ///</summary>
- public float TimerRateInSeconds { get; internal set; }
- /// <summary>
- /// Position which the GameObject travels to in UpdateRate Time
- /// </summary>
- public Vector3 TargetPos { get; internal set; }
- /// <summary>
- /// Position from which the GameObject started moving
- /// </summary>
- public Vector3 StartPos { get; internal set; }
- /// <summary>
- /// Direction which the GameObject will face
- /// </summary>
- public Quaternion TargetRotation { get; internal set; }
- /// <summary>
- /// Direction which the GameObject faces
- /// </summary>
- public Quaternion StartRotation { get; internal set; }
- /// <summary>
- /// Speed with which the GameObjects moves
- /// </summary>
- public double Velocity { get; internal set; }
- private Object LockObject;
- /// <summary>
- /// Control Coroutine
- /// </summary>
- public bool Alive;
- /// <summary>
- /// Reference to Sensor.
- /// Should be the Sensor "sensing" the "Object"
- /// </summary>
- public GameObject Sensor { get; set; }
- ///<summary>
- ///change Values that probably won't change anymore
- ///</summary>
- public void SetUp(int id, AbstractObjectHandler handler, CSVReader.EntityType type, float timeToLive, float gameVelocity)
- {
- LockObject = new Object();
- Alive = true;
- ID = id;
- Handler = handler;
- Type = type;
- TimeSinceLastUpdate = 0f;
- TimeToLive = timeToLive;
- StartCoroutine(UpdateTimer());
- }
- ///<summary>
- /// Increase TimeSinceLastUpdate (+= timerRateInSeconds )
- /// and start the Handler deleteprocess if the TimeSinceLastUpdate is greater than the TimeToLive
- ///</summary>
- private IEnumerator UpdateTimer()
- {
- float timeDiff, timeTemp = Time.unscaledTime;
- while (this.Alive)
- {
- //calculate the time since last iteration
- timeDiff = Time.unscaledTime - timeTemp;
- timeTemp += timeDiff;
- //and only add it when the simulation is running
- //otherwise all objects would be destroyed
- if (this.Handler.GameVelocity > 0)
- {
- //add the time to TimeSinceLastUpdate
- TimeSinceLastUpdate += timeDiff;
- //if we want that an object will be destroyed after e.g. 1 second
- //the timeSinceLastUpdate as to be greater than 1
- //if the GameVelocity is 1/2 we want to wait 2 seconds (1/(1/2))
- if (TimeSinceLastUpdate > TimeToLive / this.Handler.GameVelocity)
- {
- CityLogger.Log(string.Format("Destroy {0} after {1} seconds", this.ID, TimeSinceLastUpdate), LogLevel.DEBUG);
- //Start DeleteProcess
- Handler.DeleteObject(this);
- }
- //wait
- yield return new WaitForSeconds((float)this.Handler.UpdateRate);
- }
- else
- {
- //wait till GameVelocity > 0
- yield return null;
- }
- }
- }
- /// <summary>
- /// Sets TargetPosition
- /// </summary>
- /// <remarks>
- /// Also sets the StartPos to last TargetPosition
- /// </remarks>
- public void SetNewTargetPos(Vector3 newPos)
- {
- if (float.IsNaN(newPos.x) || float.IsNaN(newPos.y) || float.IsNaN(newPos.z))
- {
- return;
- }
- StartPos = TargetPos;
- TargetPos = newPos;
- }
- ///<summary>
- /// Maps input data
- ///</summary>
- public void MapInputObjekt(InputObject input, GameObject sensor)
- {
- //lock (LockObject)
- //{ //set TimeSinceLastUpdate to zero
- this.TimeSinceLastUpdate = 0f;
- this.Timestamp = input.Time;
- this.Rot = input.Rot;
- this.HeadRot = input.HeadRot;
- this.BotColor = input.BotColor;
- this.TopColor = input.TopColor;
- this.Type = input.Type;
- this.Sensor = sensor;
- this.SetNewTargetPos(input.Pos);
- //}
- }
- private void OnDestroy()
- {
- this.Alive = false;
- }
- }
- }
|