Skip to content

Instantly share code, notes, and snippets.

@PumpkinPaul
Created November 29, 2022 08:02
Show Gist options
  • Save PumpkinPaul/9ebb5e389c088c204353d049fd52241c to your computer and use it in GitHub Desktop.
Save PumpkinPaul/9ebb5e389c088c204353d049fd52241c to your computer and use it in GitHub Desktop.
using MoonTools.ECS;
using MoonToolsECSTest.Components;
using MoonToolsECSTest.Messages;
namespace MoonToolsECSTest.Systems
{
public sealed class ParticleSystem : MoonTools.ECS.System
{
readonly Dictionary<string, ParticleSystemTemplate> _particleSystemResources;
readonly Dictionary<int, List<string>> _entityClassificationsToParticleSystems;
public ParticleSystem(
World world,
Dictionary<string, ParticleSystemTemplate> particleSystemResources,
Dictionary<int, List<string>> entityClassificationsToParticleSystems
) : base(world)
{
_particleSystemResources = particleSystemResources;
_entityClassificationsToParticleSystems = entityClassificationsToParticleSystems;
}
public override void Update(TimeSpan delta)
{
//NOTE - random memory access here looking up items in mapping table and resource table.
//Is this a 'problem'? Do we care? Our games are 'simple' and update / draw in a couple of ms
//I do think I like the architecture - maybe
//You could instead build a simple array as the lookup with offset index and count with the downside of some data duplication
//A bunch of things may have died this frame
foreach (var message in ReadMessages<DeathMessage>())
{
//If any of the dead entities have the ExplosionComponent we can create particle explosions for them
if (Has<ExplosionComponent>(message.Entity))
{
var explosionComponent = Get<ExplosionComponent>(message.Entity);
//We look up the name(s) of the particle system(s) to create from the mapping table
//This will tell us the name(s) of the systems to create.
//e.g.
//Entities of 'type' PLAYER should create "GenericBoom", "PlayerBigBoom", "PlayerSparkles"
var particleSystemNames = _entityClassificationsToParticleSystems[explosionComponent.ClassificationId];
//Now we know the names we can lookup the templates and create particle systems based on them
foreach (var name in particleSystemNames)
{
var template = _particleSystemResources[name];
Console.WriteLine($"Template: {name}, {template.Color}, { template.Count}");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment