using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CSVReader;
namespace ObjectScripts
{
public class EventHandler : AbstractEventHandler
{
///
/// EventPrefab
///
[SerializeField] GameObject EventPrefab;
///
/// Manager Object
///
[SerializeField] AbstractManager Manager;
///
/// next Id to assign
///
private uint nextID = 0;
///
/// Event CSV Reader
///
EventCSVReader EventCSV;
///
/// EventBuffer
///
EventObject[] Events;
///
/// MapSensor for mapping local position to world position
///
IMapSensor MapSensor;
///
/// Control Coroutine
///
bool running;
///
/// number of known events
///
float KnownCount = 0;
void Start()
{
EventList = new List();
// EventCSV = new EventCSVReader(filename);
// EventObject[] events = EventCSV.ReadFile();
// Handle(events);
}
///
/// Set up EventHandler
///
public override void SetUp(EventCSVReader eventCSV, IMapSensor mapSensor)
{
EventCSV = eventCSV;
MapSensor = mapSensor;
running = true;
StartCoroutine(UpdateWithProzessorUpdateRate());
}
///
/// Read events every 1/updaterate seconds
///
IEnumerator UpdateWithProzessorUpdateRate()
{
while (running)
{
this.Events = this.EventCSV.ReadFile();
if (Events.Length > KnownCount)
{
Events = MapEventsToWorldposition(Events);
Handle(Events);
}
yield return new WaitForSecondsRealtime(this.Manager.UpdateRateInSeconds);
}
}
///
/// Map Event local position to global position
///
private EventObject[] MapEventsToWorldposition(EventObject[] events)
{
List eventPositions = new List();
List eventViewingPositions = new List();
foreach (EventObject e in events)
{
eventPositions.Add(new InputObject(0, 0, e.Pos.x, e.Pos.y, e.Pos.z, 0, 0, 0, 0, 0, 0, 0, 0, 0, e.ID));
eventViewingPositions.Add(new InputObject(0, 0, e.IdealViewingPos.x, e.IdealViewingPos.y, e.IdealViewingPos.z, 0, 0, 0, 0, 0, 0, 0, 0, 0, e.ID));
}
eventPositions = MapSensor.MapToWorldPositions(eventPositions);
eventViewingPositions = MapSensor.MapToWorldPositions(eventViewingPositions);
int len = eventPositions.Count;
for (int i = 0; i < len; i++)
{
InputObject eventPosition = eventPositions[i];
InputObject eventViewingPosition = eventViewingPositions[i];
events[i].Pos = new Vector3(eventPosition.Pos.x, eventPosition.Pos.y, eventPosition.Pos.z);
events[i].setIdealViewingPosition(eventViewingPosition.Pos.x, eventViewingPosition.Pos.y, eventViewingPosition.Pos.z);
}
return events;
}
///
/// Handle new events
///
public override void Handle(EventObject[] events)
{
if (events.Length > EventList.Count)
for (int i = EventList.Count; i < events.Length; i++)
{
EventObject e = events[i];
createEvent(e);
}
}
///
/// Create new Events
///
void createEvent(EventObject e)
{
GameObject eventGameObject = Instantiate(EventPrefab);
eventGameObject.name = "Event" + e.TimestampStart + " " + e.Type;
AbstractEventScript eventScript = eventGameObject.GetComponent();
eventScript.Camera = this.Camera;
eventScript.Event = e;
eventScript.EventHandler = this;
eventScript.ID = nextID;
nextID++;
EventList.Add(eventScript);
KnownCount++;
}
// Update is called once per frame
void Update()
{
this.CurrentTimestamp = this.Manager.GameTimestamp;
}
private void OnDestroy()
{
running = false;
}
}
}