campfire/Assets/Scripts/ItemVR.cs

120 lines
4.3 KiB
C#
Raw Permalink Normal View History

2020-05-03 23:09:44 +02:00
using UnityEngine;
2020-05-02 23:47:34 +02:00
2020-04-30 02:26:18 +02:00
/*
* <summary>A replacement for the Item class in VR contexts.</summary>
*/
[RequireComponent(typeof(Burnable))]
2020-04-30 02:26:18 +02:00
public class ItemVR : MonoBehaviour {
public BurnQuality IgnitedQuality;
public float IgniteDuration;
public bool Ignited = false;
2020-05-02 18:28:42 +02:00
public bool Extinguished = false;
2020-04-30 02:26:18 +02:00
public float TorchDuration;
public float TorchWarnDuration;
2020-05-03 23:09:44 +02:00
public float MinDistanceFromTerrain;
public float FuelTickingMultiplierOnGround;
2020-04-30 02:26:18 +02:00
[Header("Burning indicators")]
public GameObject StickObject;
2020-05-02 18:28:42 +02:00
public GameObject DestroyedOnExtinguish;
2020-04-30 02:26:18 +02:00
public GameObject TorchObject;
public ParticleSystem FireParticles;
public Light TorchLight;
public Color WarnColor;
public GameObject FireVolume;
2020-05-01 04:37:20 +02:00
public AudioSource TorchSound;
public AudioClip[] FireClips;
2020-04-30 02:26:18 +02:00
[Header("Runtime values")]
public float TorchFuel;
private Burnable Burnable;
2020-04-30 02:26:18 +02:00
private int BurningSourceCount = 0;
private float IgnitionStartTime = -1;
private Color NormalColor;
2020-05-03 23:09:44 +02:00
private Terrain Terrain;
private Vector3 TorchLightBasePosition;
2020-04-30 02:26:18 +02:00
private void Start() {
Burnable = GetComponent<Burnable>();
2020-04-30 02:26:18 +02:00
NormalColor = TorchLight.color;
TorchObject.SetActive(Ignited);
StickObject.SetActive(!Ignited);
2020-05-03 23:09:44 +02:00
Terrain = GameObject.FindGameObjectWithTag("Main Terrain").GetComponent<Terrain>();
TorchLightBasePosition = TorchLight.transform.localPosition;
2020-04-30 02:26:18 +02:00
}
private void Update() {
2020-05-02 18:28:42 +02:00
if (Extinguished) {
return;
}
2020-05-01 04:37:20 +02:00
2020-05-03 23:09:44 +02:00
if (Ignited) {
float FuelTickAmount = Time.deltaTime;
TorchLight.transform.localPosition = TorchLightBasePosition;
Vector3 LightPosition = TorchLight.transform.position;
float MinHeight = (Terrain.SampleHeight(LightPosition) + Terrain.transform.position.y) + MinDistanceFromTerrain;
if (LightPosition.y < MinHeight) {
// The light is on ground, clamp it above round (so the light doesn't look weird)
// and start ticking fuel at a higher rate
LightPosition.y = MinHeight;
FuelTickAmount *= FuelTickingMultiplierOnGround;
}
TorchLight.transform.position = LightPosition;
TorchFuel -= FuelTickAmount;
2020-04-30 02:26:18 +02:00
if (TorchFuel < TorchWarnDuration) {
TorchLight.color = Color.Lerp(TorchLight.color, WarnColor, 5f * Time.deltaTime);
} else {
TorchLight.color = Color.Lerp(TorchLight.color, NormalColor, 5f * Time.deltaTime);
}
2020-05-01 04:37:20 +02:00
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);
2020-05-03 23:09:44 +02:00
TorchSound.volume = 0.5f + 0.5f * FuelFactor - (TorchFuel < TorchWarnDuration ? 0.1f : 0);
2020-05-01 04:37:20 +02:00
2020-04-30 02:26:18 +02:00
if (TorchFuel <= 0) {
FireParticles.Stop();
FireVolume.layer = gameObject.layer;
2020-04-30 02:26:18 +02:00
TorchLight.range = Mathf.Lerp(TorchLight.range, 0, 10f * Time.deltaTime);
if (TorchLight.range < 0.01f) {
2020-05-02 18:28:42 +02:00
Destroy(DestroyedOnExtinguish);
Extinguished = true;
2020-04-30 02:26:18 +02:00
}
}
2020-05-01 04:37:20 +02:00
} else if (!Ignited && !Burnable.BurningInCampfire && IgnitionStartTime != -1 && Time.time - IgnitionStartTime >= IgniteDuration) {
2020-04-30 02:26:18 +02:00
Ignited = true;
TorchFuel = TorchDuration;
TorchObject.SetActive(true);
FireParticles.Play();
StickObject.SetActive(false);
Burnable.Quality = IgnitedQuality;
2020-04-30 02:26:18 +02:00
}
}
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;
}
}
}
}