using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ObjectScripts
{
public class EventScript : AbstractEventScript
{
///
/// GameObject to indicate event from far away
///
GameObject EventFlag;
///
///
///
GameObject EventFlagpole;
///
/// Prefab to indicate ideal viewing point
///
GameObject EventViewingPoint;
///
/// is event flag visible
///
bool EventFlagIsVisible = true;
///
///
///
[SerializeField] string nameOfCamera;
///
/// EventFlag prefab
///
[SerializeField] GameObject Flag;
///
/// time an event is shown earlier and longer than start- and endtimestamp
///
[SerializeField] double InTimeOffset = 3;
///
/// Array of Sprites indicating each Eventtype
///
[SerializeField] Sprite[] EventImages;
///
/// not used
///
Sprite EventImage;
///
/// not used
///
SpriteRenderer EventSpriteRenderer;
///
/// Position of the flage. Changes with the Player
///
Vector3 flagPosition;
///
/// event is in time. timestamp of event +-offset reached
///
public bool inTime = true;
// Start is called before the first frame update
void Start()
{
//Create Flag and EventPoint
EventFlag = Instantiate(Flag, Vector3.zero, new Quaternion());
EventPoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
// set positions
EventPoint.transform.SetParent(this.gameObject.transform);
EventFlag.transform.SetParent(this.EventPoint.transform);
EventFlag.transform.Find("EventImage").gameObject.GetComponent().sprite = EventImages[(int)Event.Type];
EventPoint.name = "EventPoint";
EventFlag.name = "EventFlag";
this.gameObject.transform.position = Event.Pos;
// if ideal viewing position is set. indicate ideal viewing postion
flagPosition = this.EventFlag.transform.position;
if (!Event.IdealViewingPos.Equals(Event.Pos))
{
EventViewingPoint = GameObject.CreatePrimitive(PrimitiveType.Cube);
EventViewingPoint.transform.SetParent(this.EventPoint.transform);
EventViewingPoint.transform.position = Event.IdealViewingPos;
EventViewingPoint.name = "EventViewingPoint";
}
Position2D = new Vector2(EventPoint.transform.position.x, EventPoint.transform.position.z);
// SetEventPointIsVisible(false);
EventIsVisible = true;
}
void Update()
{
//If event in time then object will be visible
if (EventInTime())
{
if (!inTime)
{
inTime = true;
ChangeEventVisibilityTo(true);
}
}
else
{
if (inTime)
{
inTime = false;
ChangeEventVisibilityTo(false);
}
}
HandleFlagBehavior();
//DrawPole(this.EventFlagpole, this.EventPoint.transform.position, this.EventFlag.transform.position);
}
///
/// Handle Flag Positon. Flag will have the same y-coordinate as the player
///
private void HandleFlagBehavior()
{
this.EventFlag.transform.LookAt(this.Camera.transform);
flagPosition = this.EventFlag.transform.position;
this.EventFlag.transform.position = new Vector3(flagPosition.x, this.Camera.transform.position.y, flagPosition.z);
if (inTime)
{
if (this.EventFlag.transform.localPosition.y < 1 && this.EventFlag.transform.localPosition.y > -1)
{
this.EventFlag.GetComponent().enabled = false;
}
else
{
if (!this.EventFlag.GetComponent().enabled)
{
this.EventFlag.GetComponent().enabled = true;
}
if (this.EventFlag.transform.localPosition.y < -1)
{
this.EventFlag.GetComponent().flipY = true;
}
else
{
this.EventFlag.GetComponent().flipY = false;
}
}
}
}
///
/// currenttimestamp in intervall [TimestampStart - InTimeOffset ,TimestampEnd + InTimeOffset ]
///
private bool EventInTime()
{
return (this.EventHandler.CurrentTimestamp >= this.Event.TimestampStart - InTimeOffset
&& (this.EventHandler.CurrentTimestamp <= this.Event.TimestampEnd + InTimeOffset || ((this.Event.TimestampEnd == -1 && this.EventHandler.CurrentTimestamp <= this.Event.TimestampStart + InTimeOffset)))); // || this.Event.TimestampEnd == -1
}
///
/// set Event visible or invisible
///
private void ChangeEventVisibilityTo(bool visible)
{
foreach (Renderer renderer in this.GetComponentsInChildren(visible))
{
renderer.enabled = visible;
}
this.EventIsVisible = visible;
}
///
///
///
private void DrawPole(GameObject poleObject, Vector3 eventPointposition, Vector3 eventFlagposition)
{
float distancesBetweenPointAndFlag = eventFlagposition.y - eventPointposition.y;
Renderer poleObjectRenderer = poleObject.GetComponent();
Vector3 poleObjectCurrentScale = poleObject.transform.localScale;
Vector3 poleObjectSize = poleObjectRenderer.bounds.size;
float poleObjectHeight = poleObjectSize.y / poleObjectCurrentScale.y;
float amountPoleObjects = distancesBetweenPointAndFlag / poleObjectHeight;
Vector3 polePos = poleObject.transform.position;
poleObject.transform.localPosition = new Vector3(polePos.x, amountPoleObjects + 2 * poleObjectHeight, polePos.z);
poleObject.transform.localScale = new Vector3(poleObjectCurrentScale.x, amountPoleObjects - 2 * poleObjectHeight, poleObjectCurrentScale.z);
}
///
///
///
private void FixedUpdate()
{
// SetEventPointIsVisible(CheckObjectVisible(EventPoint, Camera, ignoreNames));
// EventFlagIsVisible = CheckObjectVisible(EventFlag, Camera, ignoreNames);
}
///
///
///
private bool CheckObjectVisible(GameObject obj, GameObject Camera, string ignoreNames)
{
RaycastHit raycastHit;
if (obj.GetComponent().isVisible)
{
if (Physics.Raycast(Camera.transform.position, obj.transform.position - Camera.transform.position, out raycastHit))
{
string hitName = raycastHit.collider.gameObject.name;
if (ignoreNames.Contains(hitName))
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
else
{
return false;
}
}
}
}