campfire/Assets/Scripts/Campfire.cs

136 lines
5.0 KiB
C#
Raw Normal View History

2020-05-03 23:09:44 +02:00
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;
2020-04-29 23:31:45 +02:00
public bool DebugDisableFuelMechanic;
2020-05-03 23:09:44 +02:00
[Tooltip("The speed at which fuel goes down. For example, 0.5 will cause the campfire to last twice as long.")]
public float FuelTickingMultiplier = 1;
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;
2020-04-20 23:07:25 +02:00
[Header("Out-of-camp mechanic")]
public float CampRadius;
[Tooltip("Fuel won't go below this threshold if the player is outside of the camp.")]
public float OutOfCampFuelThreshold;
public float OutOfCampFuelRateMultiplier;
[Header("Runtime values")]
public float Fuel;
2020-04-20 23:07:25 +02:00
public bool OutOfCamp = false;
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<Burnable> LogQueue = new List<Burnable>();
2020-04-20 22:50:17 +02:00
private float NextLogTime = 0;
2020-04-19 01:39:52 +02:00
2020-04-20 23:07:25 +02:00
private Transform Player;
private void Awake() {
EnoughFuelColor = DynamicLight.color;
FullRange = DynamicLight.range;
2020-04-20 23:07:25 +02:00
Player = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update() {
2020-04-20 23:07:25 +02:00
TickFuel();
2020-04-20 22:50:17 +02:00
if (LogQueue.Count > 0 && Time.time > NextLogTime) {
2020-04-19 02:00:22 +02:00
if (BurnLog(LogQueue[0])) {
LogQueue.RemoveAt(0);
2020-04-20 22:50:17 +02:00
if (LogQueue.Count > 0) {
NextLogTime = Time.time + LogQueue[0].Quality.BurnTime;
}
2020-04-19 02:00:22 +02:00
}
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 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;
}
}
2020-04-20 23:07:25 +02:00
private void TickFuel() {
2020-05-03 23:09:44 +02:00
float TickAmount = Time.deltaTime * FuelTickingMultiplier;
if (DebugDisableFuelMechanic) {
TickAmount = 0;
}
2020-04-20 23:07:25 +02:00
OutOfCamp = true;
if ((Player.position - transform.position).magnitude < CampRadius) {
2020-05-03 23:09:44 +02:00
// In camp, tick at normal rate
2020-04-20 23:07:25 +02:00
OutOfCamp = false;
} else if (Fuel > OutOfCampFuelThreshold) {
// Player is outside of the camp, and the campfire is within safe levels, tick fuel at a slow rate
2020-05-03 23:09:44 +02:00
TickAmount *= OutOfCampFuelRateMultiplier;
} else {
// Player is outside of the camp, and the campfire is below the threshold, stop ticking fuel
// ( So they'll come back "just in time" ;) )
TickAmount = 0;
2020-04-20 23:07:25 +02:00
}
2020-05-03 23:09:44 +02:00
Fuel = Mathf.Max(0, Fuel - TickAmount);
2020-04-20 23:07:25 +02:00
}
private bool BurnLog(Burnable 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) {
Burnable Burnable = c.collider.attachedRigidbody.GetComponent<Burnable>();
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-20 22:50:17 +02:00
if (LogQueue.Count == 0) {
NextLogTime = Time.time + Burnable.Quality.BurnTime;
}
2020-04-19 01:39:52 +02:00
LogQueue.Add(Burnable);
2020-05-01 04:37:20 +02:00
Burnable.BurningInCampfire = true;
2020-04-19 01:39:52 +02:00
}
}
}
private void OnCollisionExit(Collision c) {
if (c.collider.attachedRigidbody != null && c.collider.attachedRigidbody) {
Burnable Burnable = c.collider.attachedRigidbody.GetComponent<Burnable>();
2020-05-01 04:37:20 +02:00
if (Burnable != null && LogQueue.Contains(Burnable)) {
2020-04-19 01:39:52 +02:00
LogQueue.Remove(Burnable);
2020-05-01 04:37:20 +02:00
Burnable.BurningInCampfire = false;
}
}
}
}