campfire/Assets/Scripts/Campfire.cs

95 lines
3.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Campfire : MonoBehaviour {
2020-04-19 01:12:29 +02:00
public GameState GameState;
public Light DynamicLight;
2020-04-19 02:00:22 +02:00
public Flame Flame;
2020-04-19 18:42:40 +02:00
public CampfireSfx Sfx;
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;
2020-04-19 01:39:52 +02:00
[Tooltip("How long of a break the campfire takes between eating logs, if multiple are placed on it.")]
public float LogBurningCooldown;
[Header("Runtime values")]
public float Fuel;
2020-04-19 18:42:40 +02:00
public float TimeToEnd {
get {
return Fuel - 0.7f;
}
}
private Color EnoughFuelColor;
private float FullRange;
private float RandomVariance = 0;
private float NextRandomVariance = 0;
private float LastRandomVarianceChange = 0;
private List<Item> LogQueue = new List<Item>();
2020-04-19 01:39:52 +02:00
private float LastLogBurned = 0;
private void Awake() {
EnoughFuelColor = DynamicLight.color;
FullRange = DynamicLight.range;
}
private void Update() {
2020-04-19 01:39:52 +02:00
if (LogQueue.Count > 0 && Time.time - LastLogBurned > LogBurningCooldown) {
LastLogBurned = Time.time;
2020-04-19 02:00:22 +02:00
if (BurnLog(LogQueue[0])) {
LogQueue.RemoveAt(0);
}
2020-04-19 01:39:52 +02:00
}
if (Time.time - LastRandomVarianceChange > RandomVarianceDuration) {
NextRandomVariance = (Random.value - 0.5f) * 2f * RandomVarianceMagnitude;
LastRandomVarianceChange = Time.time;
}
RandomVariance = Mathf.Lerp(RandomVariance, NextRandomVariance, (Time.time - LastRandomVarianceChange) / RandomVarianceDuration);
2020-04-19 02:32:20 +02:00
Fuel = Mathf.Max(0, Fuel - Time.deltaTime);
2020-04-19 03:42:58 +02:00
DynamicLight.range = Fuel / GoodFuelAmount / 2f * FullRange + RandomVariance;
DynamicLight.color = Color.Lerp(DynamicLight.color, Fuel < GoodFuelAmount ? TooLowFuelColor : EnoughFuelColor, 10f * Time.deltaTime);
2020-04-19 18:42:40 +02:00
Flame.Aliveness = Mathf.Log(Mathf.Max(0.01f, TimeToEnd), 10) / 2f;
2020-04-19 01:12:29 +02:00
if (Fuel <= 0) {
GameState.Current = State.GameOver;
}
}
private bool BurnLog(Item burnable) {
2020-04-19 02:00:22 +02:00
if (Fuel >= GoodFuelAmount * 2) {
return false;
} else {
2020-04-19 20:04:45 +02:00
Fuel += burnable.Quality.FuelValue * Mathf.Max(0.5f, (2 - Fuel / GoodFuelAmount));
2020-04-19 02:00:22 +02:00
Destroy(burnable.gameObject);
return true;
2020-04-19 01:39:52 +02:00
}
}
private void OnCollisionEnter(Collision c) {
if (c.collider.attachedRigidbody != null && c.collider.attachedRigidbody) {
Item Burnable = c.collider.attachedRigidbody.GetComponent<Item>();
2020-04-19 16:42:42 +02:00
if (Burnable != null && !LogQueue.Contains(Burnable)) {
Flame.AddFuelEffect += Burnable.Quality.FlameEffect;
Sfx.ActiveBurn += Burnable.Quality.SoundEffect;
2020-04-19 01:39:52 +02:00
LogQueue.Add(Burnable);
}
}
}
private void OnCollisionExit(Collision c) {
if (c.collider.attachedRigidbody != null && c.collider.attachedRigidbody) {
Item Burnable = c.collider.attachedRigidbody.GetComponent<Item>();
2020-04-19 01:39:52 +02:00
if (Burnable != null) {
LogQueue.Remove(Burnable);
}
}
}
}