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

212 lines
5.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MarsExosphereEffects : MonoBehaviour
{
public Material ruptureMaterial;
public Material escapeMaterial;
[Range(0f, 1f)]
public float exosphereLevel = 1f;
private float previousLevel = 1f;
private ParticleSystem ruptureSystem;
private ParticleSystem escapeSystem;
private bool deathTriggered = false;
private MarsDeath marsDeath;
void Awake()
{
enabled = false;
}
public void InitState()
{
if (ruptureSystem != null) Destroy(ruptureSystem.gameObject);
if (escapeSystem != null) Destroy(escapeSystem.gameObject);
ruptureSystem = null;
escapeSystem = null;
marsDeath = GetComponentInParent<MarsDeath>();
exosphereLevel = 1f;
previousLevel = 1f;
deathTriggered = false;
CreateSystems();
}
void Update()
{
if (!Application.isPlaying) return;
if (exosphereLevel >= 1f && previousLevel < 1f)
ResetState();
previousLevel = exosphereLevel;
if (exosphereLevel <= 0f && !deathTriggered)
{
deathTriggered = true;
if (marsDeath != null)
marsDeath.mesosphereLevel = 0f;
StartCoroutine(DeathBurst());
return;
}
if (deathTriggered) return;
UpdateRupture();
UpdateEscape();
}
void ResetState()
{
deathTriggered = false;
if (marsDeath != null)
marsDeath.ResetTexture();
if (ruptureSystem != null)
{
var e = ruptureSystem.emission;
e.rateOverTime = 0;
}
if (escapeSystem != null)
{
var e = escapeSystem.emission;
e.rateOverTime = 0;
}
}
void CreateSystems()
{
GameObject r = new GameObject("Rupture");
r.transform.position = transform.position;
ruptureSystem = r.AddComponent<ParticleSystem>();
var mainR = ruptureSystem.main;
mainR.loop = true;
mainR.startLifetime = new ParticleSystem.MinMaxCurve(0.4f, 1.2f);
mainR.startSpeed = new ParticleSystem.MinMaxCurve(6f, 14f);
mainR.startSize = new ParticleSystem.MinMaxCurve(0.05f, 0.2f);
mainR.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.4f, 0.05f, 0.05f),
new Color(0.7f, 0.1f, 0.1f)
);
mainR.simulationSpace = ParticleSystemSimulationSpace.World;
var emissionR = ruptureSystem.emission;
emissionR.rateOverTime = 0;
var shapeR = ruptureSystem.shape;
shapeR.enabled = true;
shapeR.shapeType = ParticleSystemShapeType.Sphere;
shapeR.radius = 3.5f;
shapeR.radiusThickness = 0.05f;
var noiseR = ruptureSystem.noise;
noiseR.enabled = true;
noiseR.strength = 2f;
var velR = ruptureSystem.velocityOverLifetime;
velR.enabled = true;
velR.space = ParticleSystemSimulationSpace.World;
velR.z = new ParticleSystem.MinMaxCurve(10f);
if (ruptureMaterial != null)
ruptureSystem.GetComponent<ParticleSystemRenderer>().material = ruptureMaterial;
ruptureSystem.Play();
GameObject e = new GameObject("Escape");
e.transform.position = transform.position;
escapeSystem = e.AddComponent<ParticleSystem>();
var mainE = escapeSystem.main;
mainE.loop = true;
mainE.startLifetime = new ParticleSystem.MinMaxCurve(2f, 5f);
mainE.startSpeed = new ParticleSystem.MinMaxCurve(15f, 40f);
mainE.startSize = new ParticleSystem.MinMaxCurve(0.03f, 0.15f);
mainE.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.3f, 0.05f, 0.05f),
new Color(0.8f, 0.1f, 0.1f)
);
mainE.simulationSpace = ParticleSystemSimulationSpace.World;
var emissionE = escapeSystem.emission;
emissionE.rateOverTime = 0;
var shapeE = escapeSystem.shape;
shapeE.enabled = true;
shapeE.shapeType = ParticleSystemShapeType.Sphere;
shapeE.radius = 3.6f;
shapeE.radiusThickness = 0.1f;
var velE = escapeSystem.velocityOverLifetime;
velE.enabled = true;
velE.space = ParticleSystemSimulationSpace.World;
velE.z = new ParticleSystem.MinMaxCurve(25f);
if (escapeMaterial != null)
escapeSystem.GetComponent<ParticleSystemRenderer>().material = escapeMaterial;
escapeSystem.Play();
}
void UpdateRupture()
{
float t = Mathf.InverseLerp(1f, 0f, exosphereLevel);
var e = ruptureSystem.emission;
e.rateOverTime = Mathf.Lerp(0, 2000, t);
if (Random.value < t * 0.5f)
ruptureSystem.Emit(20);
}
void UpdateEscape()
{
float t = Mathf.InverseLerp(1f, 0f, exosphereLevel);
var e = escapeSystem.emission;
e.rateOverTime = Mathf.Lerp(0, 5000, t);
}
IEnumerator DeathBurst()
{
var e1 = ruptureSystem.emission;
e1.rateOverTime = 7000;
var e2 = escapeSystem.emission;
e2.rateOverTime = 12000;
yield return new WaitForSeconds(0.5f);
var e3 = ruptureSystem.emission;
e3.rateOverTime = 0;
var e4 = escapeSystem.emission;
e4.rateOverTime = 0;
}
public void CleanUp()
{
if (ruptureSystem != null) Destroy(ruptureSystem.gameObject);
if (escapeSystem != null) Destroy(escapeSystem.gameObject);
if (marsDeath != null)
marsDeath.ResetTexture();
enabled = false;
}
}