quantum-space-buddies/QSB/PlayerBodySetup/Remote/DreamWorldSpawnAnimator.cs

68 lines
1.3 KiB
C#
Raw Normal View History

2022-03-26 17:14:55 +00:00
using UnityEngine;
2022-03-26 14:17:40 +00:00
namespace QSB.PlayerBodySetup.Remote;
public class DreamWorldSpawnAnimator : MonoBehaviour
{
[SerializeField]
private Transform _bodyRoot;
private float _progression;
private Material _spawnEffectMaterial;
public const float DREAMWORLD_SPAWN_TIME = 2f;
private void Awake()
{
enabled = false;
_spawnEffectMaterial = GetSpawnMaterial();
_spawnEffectMaterial.SetFloat("_Enabled", 0f);
_spawnEffectMaterial.SetFloat("_Progression", 0f);
}
private Material GetSpawnMaterial()
{
2022-03-26 17:36:04 +00:00
foreach (var renderer in GetComponentsInChildren<Renderer>(true))
2022-03-26 14:17:40 +00:00
{
foreach (var material in renderer.sharedMaterials)
{
2022-03-26 17:14:55 +00:00
if (material == null)
2022-03-26 14:17:40 +00:00
{
continue;
}
if (material.shader.name == "DreamWorldSpawnEffect")
{
return material;
2022-03-26 14:17:40 +00:00
}
}
}
return null;
2022-03-26 14:17:40 +00:00
}
public void StartSpawnEffect()
{
_progression = 0;
_spawnEffectMaterial.SetFloat("_Enabled", 1f);
enabled = true;
}
private void Update()
{
_spawnEffectMaterial.SetVector("_BodyPosition", _bodyRoot.position);
2022-03-26 17:14:55 +00:00
_progression = Mathf.MoveTowards(_progression, 4, 4 * Time.deltaTime / DREAMWORLD_SPAWN_TIME);
2022-03-26 14:17:40 +00:00
if (OWMath.ApproxEquals(_progression, 4))
{
_progression = 4;
_spawnEffectMaterial.SetFloat("_Enabled", 0f);
enabled = false;
}
_spawnEffectMaterial.SetFloat("_Progression", _progression);
}
}