123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace ObjectScripts
- {
- public class EventScript : AbstractEventScript
- {
- ///<summary>
- /// GameObject to indicate event from far away
- ///</summary>
- GameObject EventFlag;
- ///<summary>
- ///
- ///</summary>
- GameObject EventFlagpole;
- ///<summary>
- /// Prefab to indicate ideal viewing point
- ///</summary>
- GameObject EventViewingPoint;
- ///<summary>
- /// is event flag visible
- ///</summary>
- bool EventFlagIsVisible = true;
- ///<summary>
- ///
- ///</summary>
- [SerializeField] string nameOfCamera;
- ///<summary>
- /// EventFlag prefab
- ///</summary>
- [SerializeField] GameObject Flag;
- ///<summary>
- /// time an event is shown earlier and longer than start- and endtimestamp
- ///</summary>
- [SerializeField] double InTimeOffset = 3;
- ///<summary>
- /// Array of Sprites indicating each Eventtype
- ///</summary>
- [SerializeField] Sprite[] EventImages;
- ///<summary>
- /// not used
- ///</summary>
- Sprite EventImage;
- ///<summary>
- /// not used
- ///</summary>
- SpriteRenderer EventSpriteRenderer;
- ///<summary>
- /// Position of the flage. Changes with the Player
- ///</summary>
- Vector3 flagPosition;
- ///<summary>
- /// event is in time. timestamp of event +-offset reached
- ///</summary>
- 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<SpriteRenderer>().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);
- }
- ///<summary>
- /// Handle Flag Positon. Flag will have the same y-coordinate as the player
- ///</summary>
- 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<SpriteRenderer>().enabled = false;
- }
- else
- {
- if (!this.EventFlag.GetComponent<SpriteRenderer>().enabled)
- {
- this.EventFlag.GetComponent<SpriteRenderer>().enabled = true;
- }
- if (this.EventFlag.transform.localPosition.y < -1)
- {
- this.EventFlag.GetComponent<SpriteRenderer>().flipY = true;
- }
- else
- {
- this.EventFlag.GetComponent<SpriteRenderer>().flipY = false;
- }
- }
- }
- }
- ///<summary>
- /// currenttimestamp in intervall [TimestampStart - InTimeOffset ,TimestampEnd + InTimeOffset ]
- ///</summary>
- 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
- }
- ///<summary>
- /// set Event visible or invisible
- ///</summary>
- private void ChangeEventVisibilityTo(bool visible)
- {
- foreach (Renderer renderer in this.GetComponentsInChildren<Renderer>(visible))
- {
- renderer.enabled = visible;
- }
- this.EventIsVisible = visible;
- }
- ///<summary>
- ///
- ///</summary>
- private void DrawPole(GameObject poleObject, Vector3 eventPointposition, Vector3 eventFlagposition)
- {
- float distancesBetweenPointAndFlag = eventFlagposition.y - eventPointposition.y;
- Renderer poleObjectRenderer = poleObject.GetComponent<Renderer>();
- 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);
- }
- ///<summary>
- ///
- ///</summary>
- private void FixedUpdate()
- {
- // SetEventPointIsVisible(CheckObjectVisible(EventPoint, Camera, ignoreNames));
- // EventFlagIsVisible = CheckObjectVisible(EventFlag, Camera, ignoreNames);
- }
- ///<summary>
- ///
- ///</summary>
- private bool CheckObjectVisible(GameObject obj, GameObject Camera, string ignoreNames)
- {
- RaycastHit raycastHit;
- if (obj.GetComponent<Renderer>().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;
- }
- }
- }
- }
|