campfire/Assets/Scripts/Campfire.cs

53 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Campfire : MonoBehaviour {
public Light DynamicLight;
[Tooltip("This audiosource will play the clips to indicate that an object has been thrown in the fire.")]
public AudioSource BurnEffectSource;
public float GoodFuelAmount;
[Tooltip("The light turns this color when Fuel < GoodFuelAmount. Otherwise it'll be as it is in the editor.")]
public Color TooLowFuelColor;
public float RandomVarianceDuration;
public float RandomVarianceMagnitude;
[Header("Runtime values")]
public float Fuel;
private Color EnoughFuelColor;
private float FullRange;
private float RandomVariance = 0;
private float NextRandomVariance = 0;
private float LastRandomVarianceChange = 0;
private void Awake() {
EnoughFuelColor = DynamicLight.color;
FullRange = DynamicLight.range;
}
private void Update() {
if (Time.time - LastRandomVarianceChange > RandomVarianceDuration) {
NextRandomVariance = (Random.value - 0.5f) * 2f * RandomVarianceMagnitude;
LastRandomVarianceChange = Time.time;
}
RandomVariance = Mathf.Lerp(RandomVariance, NextRandomVariance, (Time.time - LastRandomVarianceChange) / RandomVarianceDuration);
Fuel -= Time.deltaTime;
DynamicLight.range = Mathf.Log(Fuel + 2f, 10) / 2f * FullRange + RandomVariance;
DynamicLight.color = Color.Lerp(DynamicLight.color, Fuel < GoodFuelAmount ? TooLowFuelColor : EnoughFuelColor, 10f * Time.deltaTime);
}
private void OnCollisionEnter(Collision c) {
if (c.collider.attachedRigidbody != null && c.collider.attachedRigidbody) {
Burnable Burnable = c.collider.attachedRigidbody.GetComponent<Burnable>();
if (Burnable != null) {
Fuel += Burnable.Quality.FuelValue;
BurnEffectSource.PlayOneShot(Burnable.Quality.BurningSound);
Destroy(c.collider.attachedRigidbody.gameObject);
}
}
}
}