encompass-cs/encompass-cs/Engines/Spawner.cs

33 lines
966 B
C#
Raw Normal View History

2019-06-29 05:07:48 +00:00
using System.Reflection;
namespace Encompass
2019-06-29 05:07:48 +00:00
{
/// <summary>
/// A Spawner is a special type of Engine that runs a Spawn method in response to each Message it receives.
/// Spawners are useful for organizing the building of new Entities in your game.
/// </summary>
2019-06-29 05:07:48 +00:00
public abstract class Spawner<TMessage> : Engine where TMessage : struct, IMessage
{
protected Spawner() : base()
{
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
if (readsAttribute != null)
{
2020-03-20 07:09:57 +00:00
readsAttribute.ReadTypes.Add(typeof(TMessage));
2019-06-29 05:07:48 +00:00
}
2020-03-20 07:09:57 +00:00
ReceiveTypes.Add(typeof(TMessage));
2019-06-29 05:07:48 +00:00
}
public override void Update(double dt)
{
2020-07-22 01:05:03 +00:00
foreach (ref readonly var message in ReadMessages<TMessage>())
2019-06-29 05:07:48 +00:00
{
Spawn(message);
}
}
2020-07-22 01:05:03 +00:00
protected abstract void Spawn(in TMessage message);
2019-06-29 05:07:48 +00:00
}
}