Util/LegacyParticles.cs
namespace CatHarvest.Util.Particles;
public class LegacyParticle
{
public GameObject GameObject;
public LegacyParticleSystem LegacyParticleSystem;
public string particleName;
public Vector3 Position
{
get => GameObject.WorldPosition;
set => GameObject.WorldPosition = value;
}
public Rotation Rotation
{
get => GameObject.WorldRotation;
set => GameObject.WorldRotation = value;
}
public static LegacyParticle Create(string name, Vector3 position = default, Rotation rotation = default,
List<ParticleControlPoint> controlPoints = null)
{
var lp = new LegacyParticle();
lp.GameObject = Game.ActiveScene.Scene.CreateObject();
lp.GameObject.WorldPosition = position;
lp.GameObject.WorldRotation = rotation;
lp.LegacyParticleSystem = lp.GameObject.Components.GetOrCreate<LegacyParticleSystem>();
lp.LegacyParticleSystem.ControlPoints = controlPoints ?? new List<ParticleControlPoint>()
{
new ParticleControlPoint()
{ Value = ParticleControlPoint.ControlPointValueInput.Vector3, VectorValue = position }
};
lp.particleName = name;
lp.LegacyParticleSystem.Particles = ParticleSystem.Load(lp.particleName);
return lp;
}
public void SetGameObject(int index, GameObject obj)
{
var cpv = new ParticleControlPoint()
{ Value = ParticleControlPoint.ControlPointValueInput.GameObject, GameObjectValue = obj };
if (LegacyParticleSystem.ControlPoints.Count < index + 1)
LegacyParticleSystem.ControlPoints.Add(cpv);
else
LegacyParticleSystem.ControlPoints[index] = cpv;
LegacyParticleSystem.SceneObject.SetControlPoint(index, obj.Transform.World);
}
public void SetVector(int index, Vector3 vec)
{
var cpv = new ParticleControlPoint()
{ Value = ParticleControlPoint.ControlPointValueInput.Vector3, VectorValue = vec };
if (LegacyParticleSystem.ControlPoints.Count < index + 1)
LegacyParticleSystem.ControlPoints.Add(cpv);
else
LegacyParticleSystem.ControlPoints[index] = cpv;
LegacyParticleSystem.SceneObject.SetControlPoint(index, vec);
}
public void SetFloat(int index, float flt)
{
var cpv = new ParticleControlPoint()
{ Value = ParticleControlPoint.ControlPointValueInput.Float, FloatValue = flt };
if (LegacyParticleSystem.ControlPoints.Count < index + 1)
LegacyParticleSystem.ControlPoints.Add(cpv);
else
LegacyParticleSystem.ControlPoints[index] = cpv;
LegacyParticleSystem.SceneObject.SetControlPoint(index, flt);
}
public void SetNamedValue(string name, float value)
{
LegacyParticleSystem.SceneObject.SetNamedValue(name, value);
}
public void SetNamedValue(string name, Vector3 value)
{
LegacyParticleSystem.SceneObject.SetNamedValue(name, value);
}
}