2019-12-05 22:59:55 +00:00
using System ;
using System.Collections.Generic ;
namespace Encompass
{
internal abstract class TypedComponentStore
{
public abstract int Count { get ; }
2019-12-24 02:45:49 +00:00
public abstract IEnumerable < ( int , Type , IComponent ) > AllInterfaceTyped ( ) ;
public abstract bool Has ( int entity ) ;
public abstract bool Remove ( int entity , int priority ) ;
public abstract void ForceRemove ( int entity ) ;
2019-12-05 22:59:55 +00:00
public abstract void Clear ( ) ;
2019-12-30 03:43:05 +00:00
public abstract void ClearPriorities ( ) ;
2019-12-05 22:59:55 +00:00
}
internal class TypedComponentStore < TComponent > : TypedComponentStore where TComponent : struct , IComponent
{
2019-12-24 02:45:49 +00:00
private readonly Dictionary < int , TComponent > store = new Dictionary < int , TComponent > ( 128 ) ;
private readonly Dictionary < int , int > priorities = new Dictionary < int , int > ( 128 ) ;
2019-12-05 22:59:55 +00:00
public override int Count { get = > store . Count ; }
2019-12-24 02:45:49 +00:00
public TComponent Get ( int entityID )
2019-12-05 22:59:55 +00:00
{
2019-12-28 23:08:07 +00:00
if ( ! store . ContainsKey ( entityID ) ) { throw new Exceptions . NoComponentOfTypeOnEntityException ( "No component of type {0} exists on Entity with ID {1}" , typeof ( TComponent ) , entityID ) ; }
2019-12-24 02:45:49 +00:00
return store [ entityID ] ;
2019-12-05 22:59:55 +00:00
}
2019-12-24 02:45:49 +00:00
public void Set ( int entityID , TComponent component )
2019-12-05 22:59:55 +00:00
{
2019-12-24 02:45:49 +00:00
store [ entityID ] = component ;
2019-12-05 22:59:55 +00:00
}
2019-12-24 02:45:49 +00:00
public bool Set ( int entityID , TComponent component , int priority )
2019-12-05 22:59:55 +00:00
{
2019-12-24 02:45:49 +00:00
if ( ! priorities . ContainsKey ( entityID ) | | priority < priorities [ entityID ] )
{
store [ entityID ] = component ;
priorities [ entityID ] = priority ;
2019-12-05 22:59:55 +00:00
return true ;
}
return false ;
}
2019-12-24 02:45:49 +00:00
public override bool Remove ( int entityID , int priority )
2019-12-24 02:01:49 +00:00
{
2019-12-24 02:45:49 +00:00
if ( ! priorities . ContainsKey ( entityID ) | | priority < priorities [ entityID ] )
2019-12-24 02:01:49 +00:00
{
2019-12-24 02:45:49 +00:00
priorities [ entityID ] = priority ;
store . Remove ( entityID ) ;
priorities . Remove ( entityID ) ;
2019-12-24 02:01:49 +00:00
return true ;
}
return false ;
}
2019-12-24 02:45:49 +00:00
public override void ForceRemove ( int entityID )
2019-12-24 02:01:49 +00:00
{
2019-12-24 02:45:49 +00:00
store . Remove ( entityID ) ;
priorities . Remove ( entityID ) ;
2019-12-24 02:01:49 +00:00
}
2019-12-24 02:45:49 +00:00
public override bool Has ( int entityID )
2019-12-05 22:59:55 +00:00
{
2019-12-24 02:45:49 +00:00
return store . ContainsKey ( entityID ) ;
2019-12-05 22:59:55 +00:00
}
public override void Clear ( )
{
store . Clear ( ) ;
2019-12-06 07:55:17 +00:00
priorities . Clear ( ) ;
2019-12-05 22:59:55 +00:00
}
2019-12-30 03:43:05 +00:00
public override void ClearPriorities ( )
{
priorities . Clear ( ) ;
}
2019-12-24 02:45:49 +00:00
public IEnumerable < ( TComponent , int ) > All ( )
2019-12-05 22:59:55 +00:00
{
2019-12-17 04:40:15 +00:00
foreach ( var kvp in store )
{
yield return ( kvp . Value , kvp . Key ) ;
}
2019-12-05 22:59:55 +00:00
}
2019-12-24 02:45:49 +00:00
public override IEnumerable < ( int , Type , IComponent ) > AllInterfaceTyped ( )
2019-12-05 22:59:55 +00:00
{
2019-12-17 04:40:15 +00:00
foreach ( var kvp in store )
{
yield return ( kvp . Key , typeof ( TComponent ) , ( IComponent ) kvp . Value ) ;
}
2019-12-05 22:59:55 +00:00
}
}
}