Add out-of-camp mechanic

This commit is contained in:
Jens Pitkänen 2020-04-21 00:07:25 +03:00
parent 5fdafd420d
commit fbee51b6d4
2 changed files with 29 additions and 1 deletions

View File

@ -110,7 +110,11 @@ MonoBehaviour:
RandomVarianceDuration: 0.2 RandomVarianceDuration: 0.2
RandomVarianceMagnitude: 0.25 RandomVarianceMagnitude: 0.25
LogBurningCooldown: 1 LogBurningCooldown: 1
CampRadius: 10
OutOfCampFuelThreshold: 15
OutOfCampFuelRateMultiplier: 0.2
Fuel: 60 Fuel: 60
OutOfCamp: 0
--- !u!54 &7293540718676611162 --- !u!54 &7293540718676611162
Rigidbody: Rigidbody:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -15,8 +15,15 @@ public class Campfire : MonoBehaviour {
[Tooltip("How long of a break the campfire takes between eating logs, if multiple are placed on it.")] [Tooltip("How long of a break the campfire takes between eating logs, if multiple are placed on it.")]
public float LogBurningCooldown; public float LogBurningCooldown;
[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")] [Header("Runtime values")]
public float Fuel; public float Fuel;
public bool OutOfCamp = false;
public float TimeToEnd { public float TimeToEnd {
get { get {
return Fuel - 0.7f; return Fuel - 0.7f;
@ -33,12 +40,17 @@ public class Campfire : MonoBehaviour {
private List<Item> LogQueue = new List<Item>(); private List<Item> LogQueue = new List<Item>();
private float NextLogTime = 0; private float NextLogTime = 0;
private Transform Player;
private void Awake() { private void Awake() {
EnoughFuelColor = DynamicLight.color; EnoughFuelColor = DynamicLight.color;
FullRange = DynamicLight.range; FullRange = DynamicLight.range;
Player = GameObject.FindGameObjectWithTag("Player").transform;
} }
private void Update() { private void Update() {
TickFuel();
if (LogQueue.Count > 0 && Time.time > NextLogTime) { if (LogQueue.Count > 0 && Time.time > NextLogTime) {
if (BurnLog(LogQueue[0])) { if (BurnLog(LogQueue[0])) {
LogQueue.RemoveAt(0); LogQueue.RemoveAt(0);
@ -54,7 +66,6 @@ public class Campfire : MonoBehaviour {
} }
RandomVariance = Mathf.Lerp(RandomVariance, NextRandomVariance, (Time.time - LastRandomVarianceChange) / RandomVarianceDuration); RandomVariance = Mathf.Lerp(RandomVariance, NextRandomVariance, (Time.time - LastRandomVarianceChange) / RandomVarianceDuration);
Fuel = Mathf.Max(0, Fuel - Time.deltaTime);
DynamicLight.range = Fuel / GoodFuelAmount / 2f * FullRange + RandomVariance; DynamicLight.range = Fuel / GoodFuelAmount / 2f * FullRange + RandomVariance;
DynamicLight.color = Color.Lerp(DynamicLight.color, Fuel < GoodFuelAmount ? TooLowFuelColor : EnoughFuelColor, 10f * Time.deltaTime); DynamicLight.color = Color.Lerp(DynamicLight.color, Fuel < GoodFuelAmount ? TooLowFuelColor : EnoughFuelColor, 10f * Time.deltaTime);
Flame.Aliveness = Mathf.Log(Mathf.Max(0.01f, TimeToEnd), 10) / 2f; Flame.Aliveness = Mathf.Log(Mathf.Max(0.01f, TimeToEnd), 10) / 2f;
@ -64,6 +75,19 @@ public class Campfire : MonoBehaviour {
} }
} }
private void TickFuel() {
OutOfCamp = true;
if ((Player.position - transform.position).magnitude < CampRadius) {
// Player is inside of the camp, tick fuel at normal rate
Fuel -= Time.deltaTime;
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
Fuel -= Time.deltaTime * OutOfCampFuelRateMultiplier;
}
Fuel = Mathf.Max(0, Fuel);
}
private bool BurnLog(Item burnable) { private bool BurnLog(Item burnable) {
if (Fuel >= GoodFuelAmount * 2) { if (Fuel >= GoodFuelAmount * 2) {
return false; return false;