using System.Collections; using System.Collections.Generic; using UnityEngine; /* * A replacement for the Item class in VR contexts. */ [RequireComponent(typeof(Burnable))] public class ItemVR : MonoBehaviour { public BurnQuality IgnitedQuality; public float IgniteDuration; public bool Ignited = false; public float TorchDuration; public float TorchWarnDuration; [Header("Burning indicators")] public GameObject StickObject; public GameObject TorchObject; public ParticleSystem FireParticles; public Light TorchLight; public Color WarnColor; public GameObject FireVolume; [Header("Runtime values")] public float TorchFuel; private Burnable Burnable; private int BurningSourceCount = 0; private float IgnitionStartTime = -1; private Color NormalColor; private void Start() { Burnable = GetComponent(); NormalColor = TorchLight.color; TorchObject.SetActive(Ignited); StickObject.SetActive(!Ignited); } private void Update() { if (Ignited && TorchLight.enabled) { TorchFuel -= Time.deltaTime; if (TorchFuel < TorchWarnDuration) { TorchLight.color = Color.Lerp(TorchLight.color, WarnColor, 5f * Time.deltaTime); } else { TorchLight.color = Color.Lerp(TorchLight.color, NormalColor, 5f * Time.deltaTime); } if (TorchFuel <= 0) { FireParticles.Stop(); FireVolume.layer = gameObject.layer; TorchLight.range = Mathf.Lerp(TorchLight.range, 0, 10f * Time.deltaTime); if (TorchLight.range < 0.01f) { TorchLight.enabled = false; } } } else if (!Ignited && IgnitionStartTime != -1 && Time.time - IgnitionStartTime >= IgniteDuration) { Ignited = true; TorchFuel = TorchDuration; TorchObject.SetActive(true); FireParticles.Play(); StickObject.SetActive(false); Burnable.Quality = IgnitedQuality; } } private void OnTriggerEnter(Collider collider) { if (!Ignited && collider.gameObject.layer == LayerMask.NameToLayer("Fire")) { if (BurningSourceCount == 0) { IgnitionStartTime = Time.time; } BurningSourceCount++; } } private void OnTriggerExit(Collider collider) { if (!Ignited && collider.gameObject.layer == LayerMask.NameToLayer("Fire")) { BurningSourceCount--; if (BurningSourceCount == 0) { IgnitionStartTime = -1; } } } }