using System.Collections; using System.Collections.Generic; using UnityEngine; // TODO: Fix sound when in warn mode // TODO: Clamp light to slightly above the terrain // TODO: Increase fuel consumption on ground /* * 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 bool Extinguished = false; public float TorchDuration; public float TorchWarnDuration; [Header("Burning indicators")] public GameObject StickObject; public GameObject DestroyedOnExtinguish; public GameObject TorchObject; public ParticleSystem FireParticles; public Light TorchLight; public Color WarnColor; public GameObject FireVolume; public AudioSource TorchSound; public AudioClip[] FireClips; [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 (Extinguished) { return; } 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 ((TorchSound.clip.length - TorchSound.time) <= Time.deltaTime || !TorchSound.isPlaying) { TorchSound.clip = FireClips[Random.Range(0, FireClips.Length)]; TorchSound.time = 0; TorchSound.Play(); } float FuelFactor = 1f / (Mathf.Max(0, TorchDuration + 0.5f - TorchFuel) * 4f + 1); TorchSound.volume = 0.4f + 0.6f * FuelFactor - (TorchFuel < TorchWarnDuration ? 0.3f : 0); if (TorchFuel <= 0) { FireParticles.Stop(); FireVolume.layer = gameObject.layer; TorchLight.range = Mathf.Lerp(TorchLight.range, 0, 10f * Time.deltaTime); if (TorchLight.range < 0.01f) { Destroy(DestroyedOnExtinguish); Extinguished = true; } } } else if (!Ignited && !Burnable.BurningInCampfire && 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; } } } }