using System; using System.Collections.Generic; using Svelto.DataStructures; using Svelto.ECS.Internal; namespace Svelto.ECS { public class EngineEntityViewDB : IEngineEntityViewDB { internal EngineEntityViewDB( Dictionary entityViewsDB, Dictionary entityViewsDBdic, Dictionary metaEntityViewsDB, Dictionary> groupEntityViewsDB) { _entityViewsDB = entityViewsDB; _entityViewsDBdic = entityViewsDBdic; _metaEntityViewsDB = metaEntityViewsDB; _groupEntityViewsDB = groupEntityViewsDB; } public FasterReadOnlyList QueryEntityViews() where T:EntityView, new() { var type = typeof(T); ITypeSafeList entityViews; if (_entityViewsDB.TryGetValue(type, out entityViews) == false) return RetrieveEmptyEntityViewList(); return new FasterReadOnlyList((FasterList)entityViews); } public FasterReadOnlyList QueryGroupedEntityViews(int @group) where T:EntityView, new() { Dictionary entityViews; if (_groupEntityViewsDB.TryGetValue(group, out entityViews) == false) return RetrieveEmptyEntityViewList(); return new FasterReadOnlyList(entityViews as FasterList); } public T[] QueryEntityViewsAsArray(out int count) where T : IEntityView { var type = typeof(T); count = 0; ITypeSafeList entityViews; if (_entityViewsDB.TryGetValue(type, out entityViews) == false) return RetrieveEmptyEntityViewArray(); var castedEntityViews = (FasterList)entityViews; count = castedEntityViews.Count; return castedEntityViews.ToArrayFast(); } public T[] QueryGroupedEntityViewsAsArray(int @group, out int count) where T : IEntityView { var type = typeof(T); count = 0; Dictionary entityViews; if (_groupEntityViewsDB.TryGetValue(group, out entityViews) == false) return RetrieveEmptyEntityViewArray(); var castedEntityViews = (FasterList)entityViews[type]; count = castedEntityViews.Count; return castedEntityViews.ToArrayFast(); } public ReadOnlyDictionary QueryIndexableEntityViews() where T:IEntityView { var type = typeof(T); ITypeSafeDictionary entityViews; if (_entityViewsDBdic.TryGetValue(type, out entityViews) == false) return TypeSafeDictionary.Default; return new ReadOnlyDictionary(entityViews as Dictionary); } public bool TryQueryEntityView(int ID, out T entityView) where T : IEntityView { var type = typeof(T); T internalEntityView; ITypeSafeDictionary entityViews; TypeSafeDictionary casted; _entityViewsDBdic.TryGetValue(type, out entityViews); casted = entityViews as TypeSafeDictionary; if (casted != null && casted.TryGetValue(ID, out internalEntityView)) { entityView = (T)internalEntityView; return true; } entityView = default(T); return false; } public T QueryEntityView(int ID) where T : IEntityView { var type = typeof(T); T internalEntityView; ITypeSafeDictionary entityViews; TypeSafeDictionary casted; _entityViewsDBdic.TryGetValue(type, out entityViews); casted = entityViews as TypeSafeDictionary; if (casted != null && casted.TryGetValue(ID, out internalEntityView)) return (T)internalEntityView; throw new Exception("EntityView Not Found"); } public T QueryMetaEntityView(int metaEntityID) where T:EntityView, new() { return QueryEntityView(metaEntityID); } public bool TryQueryMetaEntityView(int metaEntityID, out T entityView) where T:EntityView, new() { return TryQueryEntityView(metaEntityID, out entityView); } public FasterReadOnlyList QueryMetaEntityViews() where T:EntityView, new() { var type = typeof(T); ITypeSafeList entityViews; if (_metaEntityViewsDB.TryGetValue(type, out entityViews) == false) return RetrieveEmptyEntityViewList(); return new FasterReadOnlyList((FasterList)entityViews); } static FasterReadOnlyList RetrieveEmptyEntityViewList() { return FasterReadOnlyList.DefaultList; } static T[] RetrieveEmptyEntityViewArray() { return FasterList.DefaultList.ToArrayFast(); } readonly Dictionary _entityViewsDB; readonly Dictionary _entityViewsDBdic; readonly Dictionary _metaEntityViewsDB; readonly Dictionary> _groupEntityViewsDB; } }