diff --git a/Assets/Scripts/OpenTS2/Scenes/EffectsManager.cs b/Assets/Scripts/OpenTS2/Scenes/EffectsManager.cs index b9deb17..3c29994 100644 --- a/Assets/Scripts/OpenTS2/Scenes/EffectsManager.cs +++ b/Assets/Scripts/OpenTS2/Scenes/EffectsManager.cs @@ -1,9 +1,12 @@ using System; +using System.Collections.Generic; using System.Linq; using OpenTS2.Common; using OpenTS2.Content; using OpenTS2.Content.DBPF; using OpenTS2.Content.DBPF.Effects; +using OpenTS2.Content.DBPF.Scenegraph; +using OpenTS2.Engine; using OpenTS2.Files; using OpenTS2.Files.Formats.DBPF; using UnityEngine; @@ -14,6 +17,7 @@ namespace OpenTS2.Scenes public class EffectsManager : MonoBehaviour { private EffectsAsset _effects; + private List _particleMaterials = new List(); private void Start() { @@ -28,6 +32,15 @@ namespace OpenTS2.Scenes emissionModule.enabled = false; } + // Clean up the materials we created. + private void OnDestroy() + { + foreach(var mat in _particleMaterials) + { + mat.Free(); + } + } + public void StartEffect(string effectName) { var unityParticleSystem = GetComponent(); @@ -50,15 +63,36 @@ namespace OpenTS2.Scenes unityParticleSystem.Play(withChildren:true); } - private static GameObject CreateForParticleEffect(EffectDescription description, ParticleEffect effect) + private GameObject CreateForParticleEffect(EffectDescription description, ParticleEffect effect) { var particleObject = new GameObject(description.EffectName, typeof(ParticleSystem)); var system = particleObject.GetComponent(); + system.Stop(withChildren:true, ParticleSystemStopBehavior.StopEmittingAndClear); var emission = system.emission; var emissionRateOverTime = emission.rateOverTime; emissionRateOverTime.curve = effect.Emission.RateCurve.ToUnityCurve(); + var main = system.main; + main.duration = effect.Life.Life[0]; + + if (effect.Drawing.MaterialName != "") + { + var textureAsset = ContentProvider.Get().GetAsset(new ResourceKey( + $"{effect.Drawing.MaterialName}_txtr", GroupIDs.Scenegraph, TypeIDs.SCENEGRAPH_TXTR)); + + // TODO: temporarily using the standard shader here. + var material = new Material(Shader.Find("OpenTS2/StandardMaterial/AlphaBlended")) + { + mainTexture = textureAsset.GetSelectedImageAsUnityTexture(ContentProvider.Get()) + }; + _particleMaterials.Add(material); + + system.GetComponent().material = material; + + Debug.Log($"material: {effect.Drawing.MaterialName}"); + } + return particleObject; } }