Files
MonoGame.Extended/Source/MonoGame.Extended.Particles/ParticleEffect.cs
T
Dylan Wilson b3a631aff5 Gui dialogs (#410)
* started refactoring for gui windows

* a bit more work on gui windows

* fixed some bugs and broken build stuff

* fixed some bugs in gui controls

* a little code cleanup

* got gui dialogs positioning and experimenting with named styles by type

* apply control styles directly in the constructor of controls

* got everything compiling again

* all gui controls have skinnable constructors

* rework uniform grid desired size calculations

* turns out gui layouts are complicated

* include pdb files in nuget packages

* fixed a bug in find nearest glyph

* custom control styling

* added a range constructor overload that takes one value

* rethinking control desired size behaviour

* made particle emitters more editable at runtime

* moved the auto trigger code to the particle effect instead of the emitters

* tweaks

* changed the particle modifer interface to an abstract base with an optional name

* changed the particle modifer interface to an abstract base with an optional name

* an attempt at creating a gui combo box

* rolled back the nested control experiment

* pretty good (but not perfect) combo boxes

* tweaks

* name property bindings on lists

* removed and cleaned up the hacks from the gui combo box

* minor combo box styling tweaks

* minor bug fix in gui combo box

* experiments with data binding

* fixed a mistake on the texture region service interface

* working on the ability to serialize particles

* finished particle serialization

* more robust range deserialization

* added editor browable attributes

* fixed some broken demos

* made the particle modifiers editable at runtime

* fixed some issues with particle serialization

* fixed some issues with interpolators
2017-07-22 21:22:13 +10:00

98 lines
2.9 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Particles.Serialization;
using MonoGame.Extended.Serialization;
using Newtonsoft.Json;
namespace MonoGame.Extended.Particles
{
public class ParticleEffect : Transform2D<ParticleEffect>
{
public ParticleEffect(string name = null, bool autoTrigger = true, float autoTriggerDelay = 0f)
{
Name = name;
AutoTrigger = autoTrigger;
AutoTriggerDelay = autoTriggerDelay;
Emitters = new List<ParticleEmitter>();
}
private float _nextAutoTrigger;
public string Name { get; set; }
public bool AutoTrigger { get; set; }
public float AutoTriggerDelay { get; set; }
public List<ParticleEmitter> Emitters { get; set; }
public int ActiveParticles => Emitters.Sum(t => t.ActiveParticles);
public void FastForward(Vector2 position, float seconds, float triggerPeriod)
{
var time = 0f;
while (time < seconds)
{
Update(triggerPeriod);
Trigger(position);
time += triggerPeriod;
}
}
public static ParticleEffect FromFile(ITextureRegionService textureRegionService, string path)
{
using (var stream = TitleContainer.OpenStream(path))
{
return FromStream(textureRegionService, stream);
}
}
public static ParticleEffect FromStream(ITextureRegionService textureRegionService, Stream stream)
{
var serializer = new ParticleJsonSerializer(textureRegionService);
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
return serializer.Deserialize<ParticleEffect>(jsonReader);
}
}
public void Update(float elapsedSeconds)
{
if (AutoTrigger)
{
_nextAutoTrigger -= elapsedSeconds;
if (_nextAutoTrigger <= 0)
{
Trigger();
_nextAutoTrigger = AutoTriggerDelay;
}
}
for (var i = 0; i < Emitters.Count; i++)
Emitters[i].Update(elapsedSeconds);
}
public void Trigger()
{
Trigger(Position);
}
public void Trigger(Vector2 position, float layerDepth = 0)
{
for (var i = 0; i < Emitters.Count; i++)
Emitters[i].Trigger(position, layerDepth);
}
public void Trigger(LineSegment line)
{
for (var i = 0; i < Emitters.Count; i++)
Emitters[i].Trigger(line);
}
public override string ToString()
{
return Name;
}
}
}