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
///
/// timestamp of last update
///
public double Timestamp;
///
/// Position to rotate towards
///
public Vector3 Rot;
///
/// Position too look at
///
public Vector3 HeadRot;
///
/// Top Color
///
public uint TopColor;
///
/// Bottom Color
///
public uint BotColor;
///
/// Entitiy Type
///
public CSVReader.EntityType Type;
///
/// ID
///
public int ID { get; set; }
///
/// ObjectHandler which handles the object
///
public AbstractObjectHandler Handler { get; internal set; }
///
/// The time since the last update occurred.
/// Updated every timerRateInSeconds.
///
public float TimeSinceLastUpdate { get; internal set; }
///
/// Maximum Time the object can live without an update
///
public float TimeToLive { get; internal set; }
///
/// Rate for Updating TimeSinceLastUpdate
///
public float TimerRateInSeconds { get; internal set; }
///
/// Position which the GameObject travels to in UpdateRate Time
///
public Vector3 TargetPos { get; internal set; }
///
/// Position from which the GameObject started moving
///
public Vector3 StartPos { get; internal set; }
///
/// Direction which the GameObject will face
///
public Quaternion TargetRotation { get; internal set; }
///
/// Direction which the GameObject faces
///
public Quaternion StartRotation { get; internal set; }
///
/// Speed with which the GameObjects moves
///
public double Velocity { get; internal set; }
private Object LockObject;
///
/// Control Coroutine
///
public bool Alive;
///
/// Reference to Sensor.
/// Should be the Sensor "sensing" the "Object"
///
public GameObject Sensor { get; set; }
///
///change Values that probably won't change anymore
///
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());
}
///
/// Increase TimeSinceLastUpdate (+= timerRateInSeconds )
/// and start the Handler deleteprocess if the TimeSinceLastUpdate is greater than the TimeToLive
///
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;
}
}
}
///
/// Sets TargetPosition
///
///
/// Also sets the StartPos to last TargetPosition
///
public void SetNewTargetPos(Vector3 newPos)
{
if (float.IsNaN(newPos.x) || float.IsNaN(newPos.y) || float.IsNaN(newPos.z))
{
return;
}
StartPos = TargetPos;
TargetPos = newPos;
}
///
/// Maps input data
///
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;
}
}
}