Files
MonoGame.Extended/Source/MonoGame.Extended.Particles/ParticleEmitter.cs
T
Dylan WilsonandGitHub 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

177 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Particles.Modifiers;
using MonoGame.Extended.Particles.Profiles;
using MonoGame.Extended.TextureAtlases;
using Newtonsoft.Json;
namespace MonoGame.Extended.Particles
{
public unsafe class ParticleEmitter : IDisposable
{
private readonly FastRandom _random = new FastRandom();
private float _totalSeconds;
[JsonConstructor]
public ParticleEmitter(string name, TextureRegion2D textureRegion, int capacity, TimeSpan lifeSpan, Profile profile)
{
if (profile == null)
throw new ArgumentNullException(nameof(profile));
_lifeSpanSeconds = (float)lifeSpan.TotalSeconds;
Name = name;
TextureRegion = textureRegion;
Buffer = new ParticleBuffer(capacity);
Offset = Vector2.Zero;
Profile = profile;
Modifiers = new List<Modifier>();
ModifierExecutionStrategy = ParticleModifierExecutionStrategy.Serial;
Parameters = new ParticleReleaseParameters();
}
public ParticleEmitter(TextureRegion2D textureRegion, int capacity, TimeSpan lifeSpan, Profile profile)
: this(null, textureRegion, capacity, lifeSpan, profile)
{
}
public void Dispose()
{
Buffer.Dispose();
GC.SuppressFinalize(this);
}
~ParticleEmitter()
{
Dispose();
}
public string Name { get; set; }
public int ActiveParticles => Buffer.Count;
public Vector2 Offset { get; set; }
public List<Modifier> Modifiers { get; }
public Profile Profile { get; set; }
public ParticleReleaseParameters Parameters { get; set; }
public TextureRegion2D TextureRegion { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public ParticleModifierExecutionStrategy ModifierExecutionStrategy { get; set; }
internal ParticleBuffer Buffer;
public int Capacity
{
get { return Buffer.Size; }
set
{
var oldBuffer = Buffer;
oldBuffer.Dispose();
Buffer = new ParticleBuffer(value);
}
}
private float _lifeSpanSeconds;
public TimeSpan LifeSpan
{
get { return TimeSpan.FromSeconds(_lifeSpanSeconds); }
set { _lifeSpanSeconds = (float) value.TotalSeconds; }
}
private void ReclaimExpiredParticles()
{
var iterator = Buffer.Iterator;
var expired = 0;
while (iterator.HasNext)
{
var particle = iterator.Next();
if (_totalSeconds - particle->Inception < _lifeSpanSeconds)
break;
expired++;
}
if (expired != 0)
Buffer.Reclaim(expired);
}
public bool Update(float elapsedSeconds)
{
_totalSeconds += elapsedSeconds;
if (Buffer.Count == 0)
return false;
ReclaimExpiredParticles();
var iterator = Buffer.Iterator;
while (iterator.HasNext)
{
var particle = iterator.Next();
particle->Age = (_totalSeconds - particle->Inception) / _lifeSpanSeconds;
particle->Position = particle->Position + particle->Velocity * elapsedSeconds;
}
ModifierExecutionStrategy.ExecuteModifiers(Modifiers, elapsedSeconds, iterator);
return true;
}
public void Trigger(Vector2 position, float layerDepth = 0)
{
var numToRelease = _random.Next(Parameters.Quantity);
Release(position + Offset, numToRelease, layerDepth);
}
public void Trigger(LineSegment line)
{
var numToRelease = _random.Next(Parameters.Quantity);
var lineVector = line.ToVector();
for (var i = 0; i < numToRelease; i++)
{
var offset = lineVector * _random.NextSingle();
Release(line.Origin + offset, 1);
}
}
private void Release(Vector2 position, int numToRelease, float layerDepth = 0)
{
var iterator = Buffer.Release(numToRelease);
while (iterator.HasNext)
{
var particle = iterator.Next();
Vector2 heading;
Profile.GetOffsetAndHeading(out particle->Position, out heading);
particle->Age = 0f;
particle->Inception = _totalSeconds;
particle->Position += position;
particle->TriggerPos = position;
var speed = _random.NextSingle(Parameters.Speed);
particle->Velocity = heading * speed;
_random.NextColor(out particle->Color, Parameters.Color);
particle->Opacity = _random.NextSingle(Parameters.Opacity);
var scale = _random.NextSingle(Parameters.Scale);
particle->Scale = new Vector2(scale, scale);
particle->Rotation = _random.NextSingle(Parameters.Rotation);
particle->Mass = _random.NextSingle(Parameters.Mass);
particle->LayerDepth = layerDepth;
}
}
public override string ToString()
{
return Name;
}
}
}