rewriting ecs from scratch

This commit is contained in:
Dylan Wilson
2018-06-09 00:02:07 +10:00
parent c4bca0d7ad
commit d86f767cce
58 changed files with 1068 additions and 37 deletions
@@ -0,0 +1,23 @@
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
{
public class ComponentManager
{
public ComponentManager()
{
_mappers = new Bag<ComponentMapper>();
}
private readonly Bag<ComponentMapper> _mappers;
public void RegisterComponentType<T>()
where T : class
{
var index = _mappers.Count;
var componentType = new ComponentType(typeof(T), index);
var mapper = new ComponentMapper<T>();
_mappers[index] = mapper;
}
}
}
@@ -0,0 +1,47 @@
using System;
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
{
public abstract class ComponentMapper
{
protected ComponentMapper(Type componentType)
{
ComponentType = componentType;
}
public Type ComponentType { get; }
}
public class ComponentMapper<T> : ComponentMapper
where T : class
{
public ComponentMapper()
: base(typeof(T))
{
Components = new Bag<T>();
}
public Bag<T> Components { get; }
public void Put(int entityId, T component)
{
Components[entityId] = component;
}
public T Get(int entityId)
{
return Components[entityId];
}
public bool Has(int entityId)
{
return Components[entityId] != null;
}
public void Delete(int entityId)
{
Components[entityId] = null;
}
}
}
@@ -0,0 +1,47 @@
using System;
namespace MonoGame.Extended.Entities
{
public class ComponentType : IEquatable<ComponentType>
{
public ComponentType(Type type, int index)
{
Type = type;
Index = index;
}
public Type Type { get; }
public int Index { get; }
public bool Equals(ComponentType other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Index == other.Index;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((ComponentType) obj);
}
public override int GetHashCode()
{
return Index;
}
public static bool operator ==(ComponentType left, ComponentType right)
{
return Equals(left, right);
}
public static bool operator !=(ComponentType left, ComponentType right)
{
return !Equals(left, right);
}
}
}
@@ -34,11 +34,9 @@
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System.Linq.Expressions;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
internal class Aspect
public class Aspect
{
// match entities with components using boolean logic:
// (A and B and C and ..) AND (D or E or F or ...) AND NOT(G or H or I or ..)
@@ -87,7 +85,7 @@ namespace MonoGame.Extended.Entities
NorMask = new BitVector(norMask);
}
internal bool Matches(BitVector componentBits)
public bool Matches(BitVector componentBits)
{
AndMask.And(componentBits, ref Result);
if (!Result.Equals(AndMask))
@@ -1,6 +1,6 @@
using System;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public enum AspectType
{
@@ -49,9 +49,9 @@ SOFTWARE.
using System;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
internal sealed class BitVector
public sealed class BitVector
{
// XPerY=n means that n Xs can be stored in 1 Y.
private const int _bitsPerInt32 = 32;
@@ -1,6 +1,6 @@
using System;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public class DefaultDependencyResolver : DependencyResolver
{
@@ -1,6 +1,6 @@
using System;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public abstract class DependencyResolver
{
@@ -37,9 +37,10 @@
using System;
using System.Runtime.CompilerServices;
using MonoGame.Extended.Collections;
// ReSharper disable InconsistentNaming
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public sealed class Entity : IPoolable
{
@@ -1,4 +1,4 @@
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public delegate void EntityComponentDelegate(Entity entity, object component);
}
@@ -1,6 +1,6 @@
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
internal interface IComponentPool
{
@@ -37,7 +37,7 @@
using System;
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class EntityComponentPoolAttribute : Attribute
@@ -37,7 +37,7 @@ using Microsoft.Xna.Framework;
using MonoGame.Extended.Animations;
using MonoGame.Extended.Sprites;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public sealed class EntityComponentSystem : DrawableGameComponent
{
@@ -211,9 +211,8 @@ namespace MonoGame.Extended.Entities
return;
var processingSystem = _dependencyResolver.Resolve<ProcessingSystem>(typeInfo.AsType());
var entityProcessingSystem = processingSystem as EntityProcessingSystem;
if (entityProcessingSystem != null)
if (processingSystem is EntityProcessingSystem entityProcessingSystem)
entityProcessingSystem.Aspect = new Aspect(andMask, orMask, norMask);
_systemManager.AddSystem(processingSystem, systemAttribute.GameLoopType, systemAttribute.Layer, SystemExecutionType.Synchronous);
@@ -37,7 +37,7 @@
using System;
using System.Diagnostics;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
[DebuggerDisplay("Index:{" + nameof(Index) + "}")]
public sealed class EntityComponentType
@@ -1,4 +1,4 @@
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public delegate void EntityDelegate(Entity entity);
}
@@ -40,7 +40,7 @@ using System.Diagnostics;
using System.Reflection;
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public class EntityManager
{
@@ -37,7 +37,7 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public abstract class EntityProcessingSystem : ProcessingSystem
{
@@ -36,7 +36,7 @@
using System;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class EntitySystemAttribute : Attribute
@@ -34,7 +34,7 @@
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public abstract class EntityTemplate
{
@@ -36,7 +36,7 @@
using System;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class EntityTemplateAttribute : Attribute
@@ -36,7 +36,7 @@
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public abstract class PoolableComponent : IPoolable
{
@@ -38,7 +38,7 @@ using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public abstract class ProcessingSystem
{
@@ -40,7 +40,7 @@ using System.Diagnostics;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities
namespace MonoGame.Extended.Entities.Legacy
{
public enum GameLoopType
{
@@ -149,10 +149,8 @@ namespace MonoGame.Extended.Entities
system.EntityComponentSystem = _manager;
Systems.Add(system);
var processingSystem = system as EntityProcessingSystem;
if (processingSystem != null)
if (system is EntityProcessingSystem processingSystem)
{
processingSystem.Index = ProcessingSystems.Count;
ProcessingSystems.Add(processingSystem);