using System.Collections; using UnityEngine; public class FireBehaviour : MonoBehaviour, IListener { [SerializeField] int hitsToDie = 100; [SerializeField][Min(1f)] float timeTillMissed = 6f; [SerializeField][Min(1f)] float timeTillIgnored = 10f; public int hits = 0; public bool IsOnFire { get; private set; } = false; public bool IsMissed { get; private set; } = false; public bool IsIgnored { get; private set; } = false; private ParticleSystem[] pSystems; private ParticleSystemRenderer[] psRenderers; private Collider extinguishCol; private float time = 0f; private float ignoringTime = 0f; private void Start() { pSystems = GetComponentsInChildren<ParticleSystem>(); psRenderers = GetComponentsInChildren<ParticleSystemRenderer>(); extinguishCol = GetComponent<Collider>(); StopAllCoroutines(); extinguishCol.enabled = false; for (int i = 0; i < pSystems.Length; i++) pSystems[i].Stop(); EventManager.Instance.AddListener(EVENT_TYPE.ON_FIRE_ALPHA_CHANGED_LEFT, this); EventManager.Instance.AddListener(EVENT_TYPE.ON_FIRE_ALPHA_CHANGED_RIGHT, this); } public void StartFire() { hits = 0; time = 0f; extinguishCol.enabled = true; for (int i = 0; i < pSystems.Length; i++) pSystems[i].Play(); IsOnFire = true; StartCoroutine(FireMissedTimer()); StartCoroutine(FireIgnoredTimer()); EventManager.Instance.PostNotification<Object>(EVENT_TYPE.ON_FIRE_STARTED, this); } public void LookAt() { ignoringTime = 0; IsIgnored = false; } private void OnParticleCollision(GameObject other) { hits++; time = 0; IsMissed = false; if (hits >= hitsToDie && IsOnFire) { IsOnFire = false; StopFire(); } } private void StopFire() { StopAllCoroutines(); extinguishCol.enabled = false; for (int i = 0; i < pSystems.Length; i++) pSystems[i].Stop(); EventManager.Instance.PostNotification<Object>(EVENT_TYPE.ON_FIRE_EXTINGUISHED, this); } private IEnumerator FireMissedTimer() { while(IsOnFire) { if (time < timeTillMissed) time += Time.deltaTime; else if (!IsMissed) { IsMissed = true; EventManager.Instance.PostNotification<Object>(EVENT_TYPE.ON_FIRE_MISSED, this); //Debug.Log("POŻAR!!!"); time = 0; } yield return null; } } private IEnumerator FireIgnoredTimer() { while(IsOnFire) { if (ignoringTime < timeTillIgnored) ignoringTime += Time.deltaTime; else if (!IsIgnored) { IsIgnored = true; ignoringTime = 0; } yield return null; } } private void OnEyeAlphaChange(string varName, float alpha) { for (int i = 0; i < psRenderers.Length; i++) { psRenderers[i].material.SetFloat(varName, alpha); } } public void OnEvent<T>(EVENT_TYPE eventType, Component Sender, T param = default) { switch (eventType) { case EVENT_TYPE.ON_FIRE_ALPHA_CHANGED_LEFT: OnEyeAlphaChange("_AlphaLeftEye", (dynamic)param); break; case EVENT_TYPE.ON_FIRE_ALPHA_CHANGED_RIGHT: OnEyeAlphaChange("_AlphaRightEye", (dynamic)param); break; default: break; } } }