Files
ScienceLab.AtmosphericPressure/Assets/Scripts/VenusThermosphereEffects.cs
2026-05-29 18:21:53 +03:00

205 lines
5.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VenusThermosphereEffects : MonoBehaviour
{
[Header("Ðàä³àö³éí³ õâèë³")]
public Material radiationMaterial;
public float planetRadius = 12f;
[Header("Venus Glow")]
public GameObject venusGlowObject;
[Range(0f, 1f)]
public float thermosphereLevel = 1f;
public float previousLevel = 1f;
private List<GameObject> activeRings = new List<GameObject>();
private bool glowEnabled = false;
private bool wavesActive = false;
void Awake()
{
enabled = false;
if (venusGlowObject != null)
venusGlowObject.SetActive(false);
}
public void InitState()
{
CleanUpRings();
thermosphereLevel = 1f;
previousLevel = 1f;
glowEnabled = false;
wavesActive = false;
if (venusGlowObject != null)
venusGlowObject.SetActive(false);
}
void CleanUpRings()
{
foreach (var obj in activeRings)
if (obj != null) Destroy(obj);
activeRings.Clear();
}
void StartWaves()
{
if (wavesActive) return;
wavesActive = true;
StartCoroutine(SpawnRadiationRing(0f));
StartCoroutine(SpawnRadiationRing(0.4f));
StartCoroutine(SpawnRadiationRing(0.8f));
StartCoroutine(SpawnRadiationRing(1.2f));
StartCoroutine(SpawnRadiationRing(1.3f));
StartCoroutine(SpawnRadiationRing(1.4f));
StartCoroutine(SpawnRadiationRing(1.6f));
StartCoroutine(SpawnRadiationRing(2.0f));
}
void StopWaves()
{
wavesActive = false;
CleanUpRings();
}
IEnumerator SpawnRadiationRing(float delay)
{
yield return new WaitForSeconds(delay);
while (wavesActive && enabled)
{
float level = thermosphereLevel;
float intensity;
float waitTime;
if (level >= 10.5f)
{
intensity = 1f;
waitTime = 0.2f;
}
else if (level >= 7f)
{
intensity = 0.85f;
waitTime = 0.6f;
}
else if (level >= 4f)
{
intensity = 0.5f;
waitTime = 1.5f;
}
else if (level >= 1f)
{
intensity = 0.25f;
waitTime = 3f;
}
else
{
yield return new WaitForSeconds(0.5f);
continue;
}
GameObject ringObj = new GameObject("RadiationRing");
ringObj.transform.position = transform.position;
activeRings.Add(ringObj);
LineRenderer lr = ringObj.AddComponent<LineRenderer>();
lr.useWorldSpace = true;
lr.loop = true;
lr.positionCount = 128;
lr.material = radiationMaterial;
Vector3 randomAxis = Random.onUnitSphere;
Vector3 perp1 = Vector3.Cross(randomAxis, Vector3.up).normalized;
if (perp1.magnitude < 0.1f)
perp1 = Vector3.Cross(randomAxis, Vector3.right).normalized;
Vector3 perp2 = Vector3.Cross(randomAxis, perp1).normalized;
float duration = Mathf.Lerp(2f, 0.8f, intensity);
float elapsed = 0f;
float startRadius = planetRadius * 0.95f;
float endRadius = planetRadius * Mathf.Lerp(2f, 3.5f, intensity);
float maxWidth = Mathf.Lerp(0.2f, 0.8f, intensity);
while (elapsed < duration && ringObj != null)
{
float t = elapsed / duration;
float radius = Mathf.Lerp(startRadius, endRadius, t);
float alpha = Mathf.Sin(t * Mathf.PI) * intensity;
float width = maxWidth * Mathf.Sin(t * Mathf.PI);
for (int j = 0; j < 128; j++)
{
float angle = j * Mathf.PI * 2f / 128f;
Vector3 pos = transform.position +
(perp1 * Mathf.Cos(angle) + perp2 * Mathf.Sin(angle)) * radius;
lr.SetPosition(j, pos);
}
lr.startColor = new Color(0.2f, 1f, 0.6f, alpha);
lr.endColor = new Color(0.1f, 0.8f, 1f, alpha);
lr.startWidth = width;
lr.endWidth = width;
elapsed += Time.deltaTime;
yield return null;
}
if (ringObj != null)
{
activeRings.Remove(ringObj);
Destroy(ringObj);
}
yield return new WaitForSeconds(waitTime);
}
}
void UpdateGlow()
{
if (venusGlowObject == null) return;
if (thermosphereLevel >= 7f && !glowEnabled)
{
glowEnabled = true;
venusGlowObject.SetActive(true);
}
else if (thermosphereLevel < 7f && glowEnabled)
{
glowEnabled = false;
venusGlowObject.SetActive(false);
}
}
void Update()
{
if (!Application.isPlaying) return;
previousLevel = thermosphereLevel;
float intensity = Mathf.Clamp01((thermosphereLevel - 1f) / 14f);
if (intensity > 0f && !wavesActive)
StartWaves();
else if (intensity <= 0f && wavesActive)
StopWaves();
UpdateGlow();
}
public void CleanUp()
{
StopWaves();
if (venusGlowObject != null)
venusGlowObject.SetActive(false);
glowEnabled = false;
enabled = false;
}
}