add entity preset support
This commit is contained in:
parent
1ab975d02e
commit
543f3f7289
|
@ -222,6 +222,9 @@
|
||||||
<Compile Include="TileMap\Json\JsonTileMap.cs" />
|
<Compile Include="TileMap\Json\JsonTileMap.cs" />
|
||||||
<Compile Include="TileMap\Json\TileMapSerializer.cs" />
|
<Compile Include="TileMap\Json\TileMapSerializer.cs" />
|
||||||
<Compile Include="IO\FileOpenMode.cs" />
|
<Compile Include="IO\FileOpenMode.cs" />
|
||||||
|
<Compile Include="Entities\EntityPreset.cs" />
|
||||||
|
<Compile Include="Entities\EntityPresetArgs.cs" />
|
||||||
|
<Compile Include="Entities\SystemComponents\EntityPresetComponent.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -13,6 +13,7 @@ namespace Blarg.GameFramework.Entities
|
||||||
using ComponentStore = Dictionary<Type, Dictionary<Entity, Component>>;
|
using ComponentStore = Dictionary<Type, Dictionary<Entity, Component>>;
|
||||||
using GlobalComponentStore = Dictionary<Type, Component>;
|
using GlobalComponentStore = Dictionary<Type, Component>;
|
||||||
using ComponentSystemList = List<ComponentSystem>;
|
using ComponentSystemList = List<ComponentSystem>;
|
||||||
|
using EntityPresetMap = Dictionary<Type, EntityPreset>;
|
||||||
|
|
||||||
public class EntityManager : IDisposable
|
public class EntityManager : IDisposable
|
||||||
{
|
{
|
||||||
|
@ -22,6 +23,7 @@ namespace Blarg.GameFramework.Entities
|
||||||
ComponentStore _components;
|
ComponentStore _components;
|
||||||
GlobalComponentStore _globalComponents;
|
GlobalComponentStore _globalComponents;
|
||||||
ComponentSystemList _componentSystems;
|
ComponentSystemList _componentSystems;
|
||||||
|
EntityPresetMap _presets;
|
||||||
|
|
||||||
EntityPool _entityPool;
|
EntityPool _entityPool;
|
||||||
|
|
||||||
|
@ -37,6 +39,7 @@ namespace Blarg.GameFramework.Entities
|
||||||
_components = new ComponentStore();
|
_components = new ComponentStore();
|
||||||
_globalComponents = new GlobalComponentStore();
|
_globalComponents = new GlobalComponentStore();
|
||||||
_componentSystems = new ComponentSystemList();
|
_componentSystems = new ComponentSystemList();
|
||||||
|
_presets = new EntityPresetMap();
|
||||||
|
|
||||||
_entityPool = new EntityPool(this);
|
_entityPool = new EntityPool(this);
|
||||||
|
|
||||||
|
@ -82,6 +85,29 @@ namespace Blarg.GameFramework.Entities
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Preset management
|
||||||
|
|
||||||
|
public void AddPreset<T>() where T : EntityPreset
|
||||||
|
{
|
||||||
|
if (_presets.ContainsKey(typeof(T)))
|
||||||
|
throw new InvalidOperationException("EntityPreset of that type is already registered.");
|
||||||
|
|
||||||
|
T preset = (T)Activator.CreateInstance(typeof(T), this);
|
||||||
|
_presets.Add(typeof(T), preset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemovePreset<T>() where T : EntityPreset
|
||||||
|
{
|
||||||
|
_presets.Remove(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveAllPresets()
|
||||||
|
{
|
||||||
|
_presets.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Entity management
|
#region Entity management
|
||||||
|
|
||||||
public Entity Add()
|
public Entity Add()
|
||||||
|
@ -91,6 +117,23 @@ namespace Blarg.GameFramework.Entities
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Entity AddUsingPreset<T>() where T : EntityPreset
|
||||||
|
{
|
||||||
|
return AddUsingPreset<T>(new EntityPreset.EmptyEntityPresetArgs());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity AddUsingPreset<T>(EntityPresetArgs args) where T : EntityPreset
|
||||||
|
{
|
||||||
|
var preset = _presets.Get(typeof(T));
|
||||||
|
if (preset == null)
|
||||||
|
throw new InvalidOperationException("Cannot add entity using a non-existant preset.");
|
||||||
|
|
||||||
|
var entity = preset.Create(args);
|
||||||
|
entity.Add<EntityPresetComponent>().PresetType = typeof(T);
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
public Entity GetFirstWith<T>() where T : Component
|
public Entity GetFirstWith<T>() where T : Component
|
||||||
{
|
{
|
||||||
EntityToComponentMap componentEntities = _components.Get(typeof(T));
|
EntityToComponentMap componentEntities = _components.Get(typeof(T));
|
||||||
|
|
24
Blarg.GameFramework/Entities/EntityPreset.cs
Normal file
24
Blarg.GameFramework/Entities/EntityPreset.cs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Blarg.GameFramework.Entities
|
||||||
|
{
|
||||||
|
public abstract class EntityPreset
|
||||||
|
{
|
||||||
|
public struct EmptyEntityPresetArgs : EntityPresetArgs
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly EntityManager EntityManager;
|
||||||
|
|
||||||
|
public EntityPreset(EntityManager entityManager)
|
||||||
|
{
|
||||||
|
if (entityManager == null)
|
||||||
|
throw new ArgumentNullException("entityManager");
|
||||||
|
|
||||||
|
EntityManager = entityManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Entity Create(EntityPresetArgs args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
9
Blarg.GameFramework/Entities/EntityPresetArgs.cs
Normal file
9
Blarg.GameFramework/Entities/EntityPresetArgs.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Blarg.GameFramework.Entities
|
||||||
|
{
|
||||||
|
public interface EntityPresetArgs
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Blarg.GameFramework.Entities.SystemComponents
|
||||||
|
{
|
||||||
|
public class EntityPresetComponent : Component
|
||||||
|
{
|
||||||
|
public Type PresetType;
|
||||||
|
|
||||||
|
public override void Reset()
|
||||||
|
{
|
||||||
|
PresetType = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue