bring back ReadsPending attribute

pull/5/head
Evan Hemsley 2019-07-23 10:17:53 -07:00
parent c6192d5b4c
commit c7cae0d9d1
4 changed files with 44 additions and 13 deletions

View File

@ -22,7 +22,6 @@ namespace Encompass
}
this.readTypes.Add(typeof(ComponentMessage<>).MakeGenericType(readType));
this.readTypes.Add(typeof(PendingComponentMessage<>).MakeGenericType(readType));
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
[AttributeUsage(AttributeTargets.Class)]
public class ReadsPending : Attribute
{
public readonly HashSet<Type> readPendingTypes = new HashSet<Type>();
public ReadsPending(params Type[] readPendingTypes)
{
foreach (var readPendingType in readPendingTypes)
{
var isComponent = readPendingType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", readPendingType.Name);
}
this.readPendingTypes.Add(typeof(PendingComponentMessage<>).MakeGenericType(readPendingType));
}
}
}
}

View File

@ -46,6 +46,12 @@ namespace Encompass
{
receiveTypes.UnionWith(readsAttribute.readTypes);
}
var readsPendingAttribute = GetType().GetCustomAttribute<ReadsPending>(false);
if (readsPendingAttribute != null)
{
receiveTypes.UnionWith(readsPendingAttribute.readPendingTypes);
}
}
internal void AssignEntityManager(EntityManager entityManager)
@ -181,6 +187,14 @@ namespace Encompass
{
return ExistingComponents<TComponent>().Union(PendingComponents<TComponent>());
}
else if (existingRead)
{
return ExistingComponents<TComponent>();
}
else if (pendingRead)
{
return PendingComponents<TComponent>();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
@ -217,11 +231,6 @@ namespace Encompass
return ExistingComponentsOnEntity<TComponent>(entity);
}
protected ValueTuple<Guid, TComponent> GetPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return PendingComponentsOnEntity<TComponent>(entity).First();
}
protected ValueTuple<Guid, TComponent> GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return GetComponents<TComponent>(entity).First();
@ -232,11 +241,6 @@ namespace Encompass
return GetComponents<TComponent>(entity).Any();
}
internal void ActivateComponentInWorld(Guid componentID)
{
componentManager.Activate(componentID);
}
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
{
componentManager.AddUpdateComponentOperation(componentID, newComponent);

View File

@ -123,7 +123,7 @@ namespace Tests
}
}
[Reads(typeof(MockComponent))]
[ReadsPending(typeof(MockComponent))]
class HasMockComponentEngine : Engine
{
private Entity entity;
@ -481,7 +481,7 @@ namespace Tests
}
[Receives(typeof(CheckHasMockComponentMessage))]
[Reads(typeof(MockComponent))]
[ReadsPending(typeof(MockComponent))]
class CheckHasPendingMockComponentEngine : Engine
{
public override void Update(double dt)