mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
Merge pull request #744 from GrizzlyEnglish/develop
Added a on put and on delete event on component mapper
This commit is contained in:
@@ -20,6 +20,9 @@ namespace MonoGame.Extended.Entities
|
||||
public class ComponentMapper<T> : ComponentMapper
|
||||
where T : class
|
||||
{
|
||||
public event Action<int> OnPut;
|
||||
public event Action<int> OnDelete;
|
||||
|
||||
private readonly Action<int> _onCompositionChanged;
|
||||
|
||||
public ComponentMapper(int id, Action<int> onCompositionChanged)
|
||||
@@ -35,6 +38,7 @@ namespace MonoGame.Extended.Entities
|
||||
{
|
||||
Components[entityId] = component;
|
||||
_onCompositionChanged(entityId);
|
||||
OnPut?.Invoke(entityId);
|
||||
}
|
||||
|
||||
public T Get(Entity entity)
|
||||
@@ -59,6 +63,7 @@ namespace MonoGame.Extended.Entities
|
||||
{
|
||||
Components[entityId] = null;
|
||||
_onCompositionChanged(entityId);
|
||||
OnDelete?.Invoke(entityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,23 @@ namespace MonoGame.Extended.Entities.Tests
|
||||
Assert.Empty(mapper.Components);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnPut()
|
||||
{
|
||||
const int entityId = 3;
|
||||
|
||||
var mapper = new ComponentMapper<Transform2>(1, _ => { });
|
||||
var component = new Transform2();
|
||||
|
||||
mapper.OnPut += (entId) =>
|
||||
{
|
||||
Assert.Equal(entityId, entId);
|
||||
Assert.Same(component, mapper.Get(entityId));
|
||||
};
|
||||
|
||||
mapper.Put(entityId, component);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PutAndGetComponent()
|
||||
{
|
||||
@@ -28,6 +45,24 @@ namespace MonoGame.Extended.Entities.Tests
|
||||
Assert.Same(component, mapper.Get(entityId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnDelete()
|
||||
{
|
||||
const int entityId = 1;
|
||||
|
||||
var mapper = new ComponentMapper<Transform2>(2, _ => { });
|
||||
var component = new Transform2();
|
||||
|
||||
mapper.OnDelete += (entId) =>
|
||||
{
|
||||
Assert.Equal(entityId, entId);
|
||||
Assert.False(mapper.Has(entityId));
|
||||
};
|
||||
|
||||
mapper.Put(entityId, component);
|
||||
mapper.Delete(entityId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteComponent()
|
||||
{
|
||||
@@ -57,4 +92,4 @@ namespace MonoGame.Extended.Entities.Tests
|
||||
Assert.True(mapper.Has(entityId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user