campfire/Assets/Scripts/ItemVR.cs

79 lines
2.5 KiB
C#
Raw Normal View History

2020-04-30 02:26:18 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* <summary>A replacement for the Item class in VR contexts.</summary>
*/
public class ItemVR : MonoBehaviour {
public BurnQuality Quality;
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;
[Header("Runtime values")]
public float TorchFuel;
private int BurningSourceCount = 0;
private float IgnitionStartTime = -1;
private Color NormalColor;
private void Start() {
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();
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);
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;
}
}
}
}