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\TileMapSerializer.cs" />
|
||||
<Compile Include="IO\FileOpenMode.cs" />
|
||||
<Compile Include="Entities\EntityPreset.cs" />
|
||||
<Compile Include="Entities\EntityPresetArgs.cs" />
|
||||
<Compile Include="Entities\SystemComponents\EntityPresetComponent.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Blarg.GameFramework.Entities
|
|||
using ComponentStore = Dictionary<Type, Dictionary<Entity, Component>>;
|
||||
using GlobalComponentStore = Dictionary<Type, Component>;
|
||||
using ComponentSystemList = List<ComponentSystem>;
|
||||
using EntityPresetMap = Dictionary<Type, EntityPreset>;
|
||||
|
||||
public class EntityManager : IDisposable
|
||||
{
|
||||
|
@ -22,6 +23,7 @@ namespace Blarg.GameFramework.Entities
|
|||
ComponentStore _components;
|
||||
GlobalComponentStore _globalComponents;
|
||||
ComponentSystemList _componentSystems;
|
||||
EntityPresetMap _presets;
|
||||
|
||||
EntityPool _entityPool;
|
||||
|
||||
|
@ -37,6 +39,7 @@ namespace Blarg.GameFramework.Entities
|
|||
_components = new ComponentStore();
|
||||
_globalComponents = new GlobalComponentStore();
|
||||
_componentSystems = new ComponentSystemList();
|
||||
_presets = new EntityPresetMap();
|
||||
|
||||
_entityPool = new EntityPool(this);
|
||||
|
||||
|
@ -82,6 +85,29 @@ namespace Blarg.GameFramework.Entities
|
|||
|
||||
#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
|
||||
|
||||
public Entity Add()
|
||||
|
@ -91,6 +117,23 @@ namespace Blarg.GameFramework.Entities
|
|||
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
|
||||
{
|
||||
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