33 lines
950 B
C#
33 lines
950 B
C#
using System.Reflection;
|
|
|
|
namespace Encompass
|
|
{
|
|
/// <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>
|
|
public abstract class Spawner<TMessage> : Engine where TMessage : struct, IMessage
|
|
{
|
|
protected Spawner() : base()
|
|
{
|
|
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
|
if (readsAttribute != null)
|
|
{
|
|
readsAttribute.readTypes.Add(typeof(TMessage));
|
|
}
|
|
|
|
receiveTypes.Add(typeof(TMessage));
|
|
}
|
|
|
|
public override void Update(double dt)
|
|
{
|
|
foreach (var message in ReadMessages<TMessage>())
|
|
{
|
|
Spawn(message);
|
|
}
|
|
}
|
|
|
|
protected abstract void Spawn(TMessage message);
|
|
}
|
|
}
|