Files
HauntedBloodlines/Assets/Scripts/Hallucinations/CandleBurnSystem.cs
2025-05-29 22:31:40 +03:00

151 lines
5.1 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
public class CandleBurnSystem : MonoBehaviour
{
[Header("Raycast Settings")]
public float burnDistance = 10f;
public LayerMask eyeLayer;
[Header("Candle Settings")]
public float timeToBurn = 4.5f;
public float candleLifeConsumptionRate = 1f;
public float candleBurnWhileZoomingMultiplier = 2f;
private bool burnStateReseted = true;
private float currentBurnTime = 0f;
private HallucinationsEye currentTarget;
private Coroutine intensityCoroutine;
private float baseIntensity;
private bool isIntensityBoosted = false;
private void Start()
{
StartCoroutine(AfterGameLoaded());
}
IEnumerator AfterGameLoaded()
{
yield return new WaitUntil(() => LoadManager.GetInstance() != null && !LoadManager.GetInstance().LoadingGame);
baseIntensity = CandleController.GetInstance().candleLight.intensity;
}
private void Update()
{
if (CameraMovement.GetInstance().IsZooming && CandleController.GetInstance().isCandleOn)
{
TryBurnRaycast();
}
else if (!burnStateReseted)
{
ResetBurnState();
}
if (CandleController.GetInstance().isCandleOn)
{
var light = CandleController.GetInstance().candleLight;
foreach (var eye in EyesManager.GetInstance().allEyes)
eye.EvaluateLightingFrom(light.transform, light.spotAngle, light.range);
}
}
private void TryBurnRaycast()
{
RaycastHit hit;
var cam = Camera.main;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, burnDistance, eyeLayer))
{
var candidates = hit.collider.GetComponentsInParent<HallucinationsEye>();
HallucinationsEye hitEye = null;
foreach (var eye in candidates)
{
if (eye.IsAlive && eye.IsOutOfWall && eye.OwnsCollider(hit.collider))
{
hitEye = eye;
break;
}
}
if (hitEye != null && hitEye.TryGetEyeIndex(hit.collider, out int eyeIndex))
{
if (currentTarget != hitEye)
{
if (currentTarget != null && currentTarget.IsAlive)
currentTarget.SetBurningState(false);
currentTarget = hitEye;
currentBurnTime = 0f;
}
currentBurnTime += Time.deltaTime;
hitEye.SetBurningState(true, eyeIndex);
float normalized = Mathf.Clamp01(currentBurnTime / timeToBurn);
hitEye.UpdateBurnVisual(normalized, eyeIndex);
CandleController.GetInstance().ReduceCandleLife(Time.deltaTime * candleBurnWhileZoomingMultiplier);
hitEye.SetSmokeActive(true);
PlayerManager.GetInstance()._PlayerMovement._CharacterAnimator.SetBool("CandleAttack", true);
PlayerManager.GetInstance()._cameraMovement.SetShake(2f, 2f);
if (!isIntensityBoosted)
{
if (intensityCoroutine != null) StopCoroutine(intensityCoroutine);
intensityCoroutine = StartCoroutine(ChangeCandleIntensity(baseIntensity * 2f));
isIntensityBoosted = true;
}
if (currentBurnTime >= timeToBurn)
{
hitEye.BurnEye();
ResetBurnState();
}
burnStateReseted = false;
return;
}
}
ResetBurnState();
}
private IEnumerator ChangeCandleIntensity(float targetIntensity)
{
Light candleLight = CandleController.GetInstance().candleLight;
HDAdditionalLightData lightData = candleLight.GetComponent<HDAdditionalLightData>();
float startIntensity = lightData.intensity;
float duration = 1f;
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float t = elapsed / duration;
lightData.intensity = Mathf.Lerp(startIntensity, targetIntensity, t);
yield return null;
}
lightData.intensity = targetIntensity;
}
private void ResetBurnState()
{
if (currentTarget != null && currentTarget.IsAlive)
{
currentTarget.SetBurningState(false);
currentTarget.SetSmokeActive(false);
}
if (isIntensityBoosted)
{
if (intensityCoroutine != null) StopCoroutine(intensityCoroutine);
intensityCoroutine = StartCoroutine(ChangeCandleIntensity(baseIntensity));
isIntensityBoosted = false;
}
PlayerManager.GetInstance()._cameraMovement.SetShakeToDefault();
PlayerManager.GetInstance()._PlayerMovement._CharacterAnimator.SetBool("CandleAttack", false);
currentBurnTime = 0f;
currentTarget = null;
burnStateReseted = true;
}
}