ParticlePrefabs are deserialized using SerializableProperty
This commit is contained in:
@@ -105,10 +105,10 @@ namespace Barotrauma.Particles
|
|||||||
velocity += ConvertUnits.ToDisplayUnits(currentHull.Submarine.Velocity);
|
velocity += ConvertUnits.ToDisplayUnits(currentHull.Submarine.Velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.rotation = rotation + Rand.Range(prefab.StartRotationMin, prefab.StartRotationMax);
|
this.rotation = rotation + Rand.Range(prefab.StartRotationMinRad, prefab.StartRotationMaxRad);
|
||||||
prevRotation = rotation;
|
prevRotation = rotation;
|
||||||
|
|
||||||
angularVelocity = prefab.AngularVelocityMin + (prefab.AngularVelocityMax - prefab.AngularVelocityMin) * Rand.Range(0.0f, 1.0f);
|
angularVelocity = Rand.Range(prefab.AngularVelocityMinRad, prefab.AngularVelocityMaxRad);
|
||||||
|
|
||||||
totalLifeTime = prefab.LifeTime;
|
totalLifeTime = prefab.LifeTime;
|
||||||
lifeTime = prefab.LifeTime;
|
lifeTime = prefab.LifeTime;
|
||||||
@@ -120,7 +120,7 @@ namespace Barotrauma.Particles
|
|||||||
color = new Color(prefab.StartColor, 1.0f);
|
color = new Color(prefab.StartColor, 1.0f);
|
||||||
alpha = prefab.StartAlpha;
|
alpha = prefab.StartAlpha;
|
||||||
|
|
||||||
velocityChange = prefab.VelocityChange;
|
velocityChange = prefab.VelocityChangeDisplay;
|
||||||
|
|
||||||
OnChangeHull = null;
|
OnChangeHull = null;
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace Barotrauma.Particles
|
|||||||
|
|
||||||
private Dictionary<string, ParticlePrefab> prefabs;
|
private Dictionary<string, ParticlePrefab> prefabs;
|
||||||
|
|
||||||
Camera cam;
|
private Camera cam;
|
||||||
|
|
||||||
public ParticleManager(string configFile, Camera cam)
|
public ParticleManager(string configFile, Camera cam)
|
||||||
{
|
{
|
||||||
@@ -30,7 +30,12 @@ namespace Barotrauma.Particles
|
|||||||
|
|
||||||
particles = new Particle[MaxParticles];
|
particles = new Particle[MaxParticles];
|
||||||
|
|
||||||
XDocument doc = XMLExtensions.TryLoadXml(configFile);
|
LoadPrefabs(configFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LoadPrefabs(string file)
|
||||||
|
{
|
||||||
|
XDocument doc = XMLExtensions.TryLoadXml(file);
|
||||||
if (doc == null || doc.Root == null) return;
|
if (doc == null || doc.Root == null) return;
|
||||||
|
|
||||||
prefabs = new Dictionary<string, ParticlePrefab>();
|
prefabs = new Dictionary<string, ParticlePrefab>();
|
||||||
@@ -39,7 +44,7 @@ namespace Barotrauma.Particles
|
|||||||
{
|
{
|
||||||
if (prefabs.ContainsKey(element.Name.ToString()))
|
if (prefabs.ContainsKey(element.Name.ToString()))
|
||||||
{
|
{
|
||||||
DebugConsole.ThrowError("Error in " + configFile + "! Each particle prefab must have a unique name.");
|
DebugConsole.ThrowError("Error in " + file + "! Each particle prefab must have a unique name.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
prefabs.Add(element.Name.ToString(), new ParticlePrefab(element));
|
prefabs.Add(element.Name.ToString(), new ParticlePrefab(element));
|
||||||
|
|||||||
@@ -13,41 +13,150 @@ namespace Barotrauma.Particles
|
|||||||
|
|
||||||
public readonly List<Sprite> Sprites;
|
public readonly List<Sprite> Sprites;
|
||||||
|
|
||||||
public readonly float AnimDuration;
|
//movement -----------------------------------------
|
||||||
public readonly bool LoopAnim;
|
|
||||||
|
|
||||||
public readonly float AngularVelocityMin, AngularVelocityMax;
|
private float angularVelocityMin;
|
||||||
|
public float AngularVelocityMinRad { get; private set; }
|
||||||
|
|
||||||
public readonly float StartRotationMin, StartRotationMax;
|
[Serialize(0.0f, false)]
|
||||||
|
public float AngularVelocityMin
|
||||||
|
{
|
||||||
|
get { return angularVelocityMin; }
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
angularVelocityMin = value;
|
||||||
|
AngularVelocityMinRad = MathHelper.ToRadians(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public readonly Vector2 StartSizeMin, StartSizeMax;
|
private float angularVelocityMax;
|
||||||
public readonly Vector2 SizeChangeMin, SizeChangeMax;
|
public float AngularVelocityMaxRad { get; private set; }
|
||||||
|
|
||||||
public readonly float Drag, WaterDrag;
|
[Serialize(0.0f, false)]
|
||||||
|
public float AngularVelocityMax
|
||||||
|
{
|
||||||
|
get { return angularVelocityMax; }
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
angularVelocityMax = value;
|
||||||
|
AngularVelocityMaxRad = MathHelper.ToRadians(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public readonly Color StartColor;
|
private float startRotationMin;
|
||||||
public readonly float StartAlpha;
|
public float StartRotationMinRad { get; private set; }
|
||||||
|
|
||||||
public readonly Vector4 ColorChange;
|
[Serialize(0.0f, false)]
|
||||||
|
public float StartRotationMin
|
||||||
public readonly float LifeTime;
|
{
|
||||||
|
get { return startRotationMin; }
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
startRotationMin = value;
|
||||||
|
StartRotationMinRad = MathHelper.ToRadians(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public readonly float GrowTime;
|
private float startRotationMax;
|
||||||
|
public float StartRotationMaxRad { get; private set; }
|
||||||
|
|
||||||
public readonly float CollisionRadius;
|
[Serialize(0.0f, false)]
|
||||||
public readonly bool DeleteOnCollision;
|
public float StartRotationMax
|
||||||
public readonly bool CollidesWithWalls;
|
{
|
||||||
|
get { return startRotationMax; }
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
startRotationMax = value;
|
||||||
|
StartRotationMaxRad = MathHelper.ToRadians(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public readonly float Friction;
|
[Serialize(false, false)]
|
||||||
public readonly float Restitution;
|
public bool RotateToDirection { get; private set; }
|
||||||
|
|
||||||
public readonly Vector2 VelocityChange;
|
[Serialize(0.0f, false)]
|
||||||
|
public float Drag { get; private set; }
|
||||||
|
|
||||||
public readonly DrawTargetType DrawTarget;
|
[Serialize(0.0f, false)]
|
||||||
|
public float WaterDrag { get; private set; }
|
||||||
|
|
||||||
public readonly ParticleBlendState BlendState;
|
private Vector2 velocityChange;
|
||||||
|
public Vector2 VelocityChangeDisplay { get; private set; }
|
||||||
|
|
||||||
public readonly bool RotateToDirection;
|
[Serialize("0.0,0.0", false)]
|
||||||
|
public Vector2 VelocityChange
|
||||||
|
{
|
||||||
|
get { return velocityChange; }
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
velocityChange = value;
|
||||||
|
VelocityChangeDisplay = ConvertUnits.ToDisplayUnits(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serialize(0.0f, false)]
|
||||||
|
public float CollisionRadius { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(false, false)]
|
||||||
|
public bool DeleteOnCollision { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(false, false)]
|
||||||
|
public bool CollidesWithWalls { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(0.5f, false)]
|
||||||
|
public float Friction { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(0.5f, false)]
|
||||||
|
public float Restitution { get; private set; }
|
||||||
|
|
||||||
|
//size -----------------------------------------
|
||||||
|
|
||||||
|
[Serialize("1.0,1.0", false)]
|
||||||
|
public Vector2 StartSizeMin { get; private set; }
|
||||||
|
|
||||||
|
[Serialize("1.0,1.0", false)]
|
||||||
|
public Vector2 StartSizeMax { get; private set; }
|
||||||
|
|
||||||
|
[Serialize("0.0,0.0", false)]
|
||||||
|
public Vector2 SizeChangeMin { get; private set; }
|
||||||
|
|
||||||
|
[Serialize("0.0,0.0", false)]
|
||||||
|
public Vector2 SizeChangeMax { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(0.0f, false)]
|
||||||
|
public float GrowTime { get; private set; }
|
||||||
|
|
||||||
|
//rendering -----------------------------------------
|
||||||
|
|
||||||
|
[Serialize("1.0,1.0,1.0,1.0", false)]
|
||||||
|
public Color StartColor { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(1.0f, false)]
|
||||||
|
public float StartAlpha { get; private set; }
|
||||||
|
|
||||||
|
[Serialize("0.0,0.0,0.0,0.0", false)]
|
||||||
|
public Vector4 ColorChange { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(DrawTargetType.Air, false)]
|
||||||
|
public DrawTargetType DrawTarget { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(ParticleBlendState.AlphaBlend, false)]
|
||||||
|
public ParticleBlendState BlendState { get; private set; }
|
||||||
|
|
||||||
|
//animation -----------------------------------------
|
||||||
|
|
||||||
|
[Serialize(1.0f, false)]
|
||||||
|
public float AnimDuration { get; private set; }
|
||||||
|
|
||||||
|
[Serialize(true, false)]
|
||||||
|
public bool LoopAnim { get; private set; }
|
||||||
|
|
||||||
|
//misc -----------------------------------------
|
||||||
|
|
||||||
|
[Serialize(5.0f, false)]
|
||||||
|
public float LifeTime { get; private set; }
|
||||||
|
|
||||||
|
//----------------------------------------------------
|
||||||
|
|
||||||
public ParticlePrefab(XElement element)
|
public ParticlePrefab(XElement element)
|
||||||
{
|
{
|
||||||
@@ -55,6 +164,8 @@ namespace Barotrauma.Particles
|
|||||||
|
|
||||||
Sprites = new List<Sprite>();
|
Sprites = new List<Sprite>();
|
||||||
|
|
||||||
|
SerializableProperty.DeserializeProperties(this, element);
|
||||||
|
|
||||||
foreach (XElement subElement in element.Elements())
|
foreach (XElement subElement in element.Elements())
|
||||||
{
|
{
|
||||||
switch (subElement.Name.ToString().ToLowerInvariant())
|
switch (subElement.Name.ToString().ToLowerInvariant())
|
||||||
@@ -69,121 +180,37 @@ namespace Barotrauma.Particles
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnimDuration = element.GetAttributeFloat("animduration", 1.0f);
|
if (element.Attribute("angularvelocity") != null)
|
||||||
LoopAnim = element.GetAttributeBool("loopanim", true);
|
|
||||||
|
|
||||||
if (element.Attribute("angularvelocity") == null)
|
|
||||||
{
|
|
||||||
AngularVelocityMin = element.GetAttributeFloat("angularvelocitymin", 0.0f);
|
|
||||||
AngularVelocityMax = element.GetAttributeFloat("angularvelocitymax", 0.0f);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
AngularVelocityMin = element.GetAttributeFloat("angularvelocity", 0.0f);
|
AngularVelocityMin = element.GetAttributeFloat("angularvelocity", 0.0f);
|
||||||
AngularVelocityMax = AngularVelocityMin;
|
AngularVelocityMax = AngularVelocityMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
AngularVelocityMin = MathHelper.ToRadians(AngularVelocityMin);
|
if (element.Attribute("startsize") != null)
|
||||||
AngularVelocityMax = MathHelper.ToRadians(AngularVelocityMax);
|
|
||||||
|
|
||||||
if (element.Attribute("startsize") == null)
|
|
||||||
{
|
|
||||||
StartSizeMin = element.GetAttributeVector2("startsizemin", Vector2.One);
|
|
||||||
StartSizeMax = element.GetAttributeVector2("startsizemax", Vector2.One);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
StartSizeMin = element.GetAttributeVector2("startsize", Vector2.One);
|
StartSizeMin = element.GetAttributeVector2("startsize", Vector2.One);
|
||||||
StartSizeMax = StartSizeMin;
|
StartSizeMax = StartSizeMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (element.Attribute("sizechange") == null)
|
if (element.Attribute("sizechange") != null)
|
||||||
{
|
|
||||||
SizeChangeMin = element.GetAttributeVector2("sizechangemin", Vector2.Zero);
|
|
||||||
SizeChangeMax = element.GetAttributeVector2("sizechangemax", Vector2.Zero);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
SizeChangeMin = element.GetAttributeVector2("sizechange", Vector2.Zero);
|
SizeChangeMin = element.GetAttributeVector2("sizechange", Vector2.Zero);
|
||||||
SizeChangeMax = SizeChangeMin;
|
SizeChangeMax = SizeChangeMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
Drag = element.GetAttributeFloat("drag", 0.0f);
|
if (element.Attribute("startrotation") != null)
|
||||||
WaterDrag = element.GetAttributeFloat("waterdrag", 0.0f);
|
|
||||||
|
|
||||||
Friction = element.GetAttributeFloat("friction", 0.5f);
|
|
||||||
Restitution = element.GetAttributeFloat("restitution", 0.5f);
|
|
||||||
|
|
||||||
switch (element.GetAttributeString("blendstate", "alphablend"))
|
|
||||||
{
|
|
||||||
case "alpha":
|
|
||||||
case "alphablend":
|
|
||||||
BlendState = ParticleBlendState.AlphaBlend;
|
|
||||||
break;
|
|
||||||
case "add":
|
|
||||||
case "additive":
|
|
||||||
BlendState = ParticleBlendState.Additive;
|
|
||||||
break;
|
|
||||||
case "distort":
|
|
||||||
case "distortion":
|
|
||||||
BlendState = ParticleBlendState.Distortion;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
GrowTime = element.GetAttributeFloat("growtime", 0.0f);
|
|
||||||
|
|
||||||
if (element.Attribute("startrotation") == null)
|
|
||||||
{
|
|
||||||
StartRotationMin = element.GetAttributeFloat("startrotationmin", 0.0f);
|
|
||||||
StartRotationMax = element.GetAttributeFloat("startrotationmax", 0.0f);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
StartRotationMin = element.GetAttributeFloat("startrotation", 0.0f);
|
StartRotationMin = element.GetAttributeFloat("startrotation", 0.0f);
|
||||||
StartRotationMax = StartRotationMin;
|
StartRotationMax = StartRotationMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
StartRotationMin = MathHelper.ToRadians(StartRotationMin);
|
if (CollisionRadius <= 0.0f) CollisionRadius = Sprites.Count > 0 ? 1 : Sprites[0].SourceRect.Width / 2.0f;
|
||||||
StartRotationMax = MathHelper.ToRadians(StartRotationMax);
|
|
||||||
|
|
||||||
StartColor = new Color(element.GetAttributeVector4("startcolor", Vector4.One));
|
|
||||||
StartAlpha = element.GetAttributeFloat("startalpha", 1.0f);
|
|
||||||
|
|
||||||
DeleteOnCollision = element.GetAttributeBool("deleteoncollision", false);
|
|
||||||
CollidesWithWalls = element.GetAttributeBool("collideswithwalls", false);
|
|
||||||
|
|
||||||
CollisionRadius = element.GetAttributeFloat("collisionradius",
|
|
||||||
Sprites.Count > 0 ? 1 : Sprites[0].SourceRect.Width / 2.0f);
|
|
||||||
|
|
||||||
ColorChange = element.GetAttributeVector4("colorchange", Vector4.Zero);
|
|
||||||
|
|
||||||
LifeTime = element.GetAttributeFloat("lifetime", 5.0f);
|
|
||||||
|
|
||||||
VelocityChange = element.GetAttributeVector2("velocitychange", Vector2.Zero);
|
|
||||||
VelocityChange = ConvertUnits.ToDisplayUnits(VelocityChange);
|
|
||||||
|
|
||||||
RotateToDirection = element.GetAttributeBool("rotatetodirection", false);
|
|
||||||
|
|
||||||
switch (element.GetAttributeString("drawtarget", "air").ToLowerInvariant())
|
|
||||||
{
|
|
||||||
case "air":
|
|
||||||
default:
|
|
||||||
DrawTarget = DrawTargetType.Air;
|
|
||||||
break;
|
|
||||||
case "water":
|
|
||||||
DrawTarget = DrawTargetType.Water;
|
|
||||||
break;
|
|
||||||
case "both":
|
|
||||||
DrawTarget = DrawTargetType.Both;
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 CalculateEndPosition(Vector2 startPosition, Vector2 velocity)
|
public Vector2 CalculateEndPosition(Vector2 startPosition, Vector2 velocity)
|
||||||
{
|
{
|
||||||
//endPos = x + vt + 1/2 * at^2
|
//endPos = x + vt + 1/2 * at^2
|
||||||
return startPosition + velocity * LifeTime + 0.5f * VelocityChange * LifeTime * LifeTime;
|
return startPosition + velocity * LifeTime + 0.5f * VelocityChangeDisplay * LifeTime * LifeTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user