void MoveEntities(EGID fromEntityGID, EGID?toEntityGID, IEntityBuilder[] entitiesToMove, FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup, PlatformProfiler sampler) { FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup = null; if (toEntityGID != null) { var toGroupID = toEntityGID.Value.groupID; if (_groupEntityViewsDB.TryGetValue(toGroupID, out toGroup) == false) { toGroup = _groupEntityViewsDB[toGroupID] = new FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary>(); } //Add all the entities to the dictionary for (var i = 0; i < entitiesToMove.Length; i++) { CopyEntityToDictionary(fromEntityGID, toEntityGID.Value, fromGroup, toGroup, entitiesToMove[i].GetEntityType()); } } //call all the callbacks for (var i = 0; i < entitiesToMove.Length; i++) { MoveEntityViewFromAndToEngines(fromEntityGID, toEntityGID, fromGroup, toGroup, entitiesToMove[i].GetEntityType(), sampler); } //then remove all the entities from the dictionary for (var i = 0; i < entitiesToMove.Length; i++) { RemoveEntityFromDictionary(fromEntityGID, fromGroup, entitiesToMove[i].GetEntityType(), sampler); } }
public void MoveEntityFromDictionaryAndEngines(EGID fromEntityGid, EGID?toEntityID, ITypeSafeDictionary toGroup, Dictionary <Type, FasterList <IEngine> > engines, ref PlatformProfiler profiler) { var valueIndex = GetValueIndex(fromEntityGid.entityID); if (toGroup != null) { RemoveEntityViewFromEngines(engines, ref _values[valueIndex], fromEntityGid.groupID, ref profiler); var toGroupCasted = toGroup as TypeSafeDictionary <TValue>; ref var entity = ref _values[valueIndex]; var previousGroup = fromEntityGid.groupID; /// /// NOTE I WOULD EVENTUALLY NEED TO REUSE THE REAL ID OF THE REMOVING ELEMENT /// SO THAT I CAN DECREASE THE GLOBAL GROUP COUNT /// // entity.ID = EGID.UPDATE_REAL_ID_AND_GROUP(entity.ID, toEntityID.groupID, entityCount); if (HasEgid) { var needEgid = (INeedEGID)entity; needEgid.ID = toEntityID.Value; entity = (TValue)needEgid; } var index = toGroupCasted.Add(fromEntityGid.entityID, ref entity); AddEntityViewToEngines(engines, ref toGroupCasted._values[index], previousGroup, ref profiler); }
void MoveEntityComponents(EGID fromEntityGID, EGID?toEntityGID, IComponentBuilder[] entitiesToMove, FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup, PlatformProfiler sampler) { FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup = null; if (toEntityGID != null) { var toGroupID = toEntityGID.Value.groupID; toGroup = GetOrCreateGroup(toGroupID); //Add all the entities to the dictionary for (var i = 0; i < entitiesToMove.Length; i++) { CopyEntityToDictionary(fromEntityGID, toEntityGID.Value, fromGroup, toGroup, entitiesToMove[i].GetEntityComponentType()); } } //call all the callbacks for (var i = 0; i < entitiesToMove.Length; i++) { MoveEntityComponentFromAndToEngines(fromEntityGID, toEntityGID, fromGroup, toGroup, entitiesToMove[i].GetEntityComponentType(), sampler); } //then remove all the entities from the dictionary for (var i = 0; i < entitiesToMove.Length; i++) { RemoveEntityFromDictionary(fromEntityGID, fromGroup, entitiesToMove[i].GetEntityComponentType(), sampler); } }
void MoveEntityView(EGID entityGID, EGID?toEntityGID, Dictionary <Type, ITypeSafeDictionary> toGroup, ref Dictionary <Type, ITypeSafeDictionary> fromGroup, Type entityViewType, PlatformProfiler profiler) { if (fromGroup.TryGetValue(entityViewType, out var fromTypeSafeDictionary) == false) { throw new ECSException("no entities in from group eid: ".FastConcat(entityGID.entityID) .FastConcat(" group: ").FastConcat(entityGID.groupID)); } ITypeSafeDictionary toEntitiesDictionary = null; //in case we want to move to a new group, otherwise is just a remove if (toEntityGID != null) { if (toGroup.TryGetValue(entityViewType, out toEntitiesDictionary) == false) { toEntitiesDictionary = fromTypeSafeDictionary.Create(); toGroup.Add(entityViewType, toEntitiesDictionary); } if (_groupsPerEntity.TryGetValue(entityViewType, out var groupedGroup) == false) { groupedGroup = _groupsPerEntity[entityViewType] = new FasterDictionary <uint, ITypeSafeDictionary>(); } groupedGroup[toEntityGID.Value.groupID] = toEntitiesDictionary; } if (fromTypeSafeDictionary.Has(entityGID.entityID) == false) { throw new EntityNotFoundException(entityGID, entityViewType); } fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(entityGID, toEntityGID, toEntitiesDictionary, toEntityGID == null ? _reactiveEnginesAddRemove : _reactiveEnginesSwap, ref profiler); if (fromTypeSafeDictionary.Count == 0) //clean up { _groupsPerEntity[entityViewType].Remove(entityGID.groupID); //I don't remove the group if empty on purpose, in case it needs to be reused however I trim it to save //memory fromTypeSafeDictionary.Trim(); } }
///-------------------------------------------- /// void MoveEntityFromAndToEngines(IComponentBuilder[] componentBuilders, EGID fromEntityGID, EGID?toEntityGID) { using (var sampler = new PlatformProfiler("Move Entity From Engines")) { var fromGroup = GetDBGroup(fromEntityGID.groupID); //Check if there is an EntityInfo linked to this entity, if so it's a DynamicEntityDescriptor! if (fromGroup.TryGetValue(new RefWrapperType(ComponentBuilderUtilities.ENTITY_INFO_COMPONENT) , out var entityInfoDic) && (entityInfoDic as ITypeSafeDictionary <EntityInfoComponent>).TryGetValue( fromEntityGID.entityID, out var entityInfo)) { SwapOrRemoveEntityComponents(fromEntityGID, toEntityGID, entityInfo.componentsToBuild, fromGroup , sampler); } //otherwise it's a normal static entity descriptor else { SwapOrRemoveEntityComponents(fromEntityGID, toEntityGID, componentBuilders, fromGroup, sampler); } } }
void SwapOrRemoveEntityComponents (EGID fromEntityGID, EGID?toEntityGID, IComponentBuilder[] entitiesToMove , FasterDictionary <RefWrapperType, ITypeSafeDictionary> fromGroup, in PlatformProfiler sampler)
///-------------------------------------------- /// void MoveEntityFromAndToEngines(IComponentBuilder[] componentBuilders, EGID fromEntityGID, EGID?toEntityGID) { using (var sampler = new PlatformProfiler("Move Entity From Engines")) { var fromGroup = GetGroup(fromEntityGID.groupID); // Update the egid to unique id maps. if (toEntityGID.HasValue) { UpdateLocator(fromEntityGID, toEntityGID.Value); } else { RemoveLocator(fromEntityGID); } //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor! if (fromGroup.TryGetValue(new RefWrapper <Type>(ComponentBuilderUtilities.ENTITY_STRUCT_INFO_VIEW) , out var entityInfoViewDic) && (entityInfoViewDic as ITypeSafeDictionary <EntityInfoViewComponent>).TryGetValue( fromEntityGID.entityID, out var entityInfoView)) { MoveEntityComponents(fromEntityGID, toEntityGID, entityInfoView.componentsToBuild, fromGroup , sampler); } //otherwise it's a normal static entity descriptor else { MoveEntityComponents(fromEntityGID, toEntityGID, componentBuilders, fromGroup, sampler); } } }
///-------------------------------------------- /// void MoveEntity(IEntityBuilder[] entityBuilders, EGID fromEntityGID, Type originalDescriptorType, EGID?toEntityGID) { var profiler = new PlatformProfiler(); using (profiler.StartNewSession("Move Entity")) { //for each entity view generated by the entity descriptor if (_groupEntityDB.TryGetValue(fromEntityGID.groupID, out var fromGroup) == false) { throw new ECSException("from group not found eid: ".FastConcat(fromEntityGID.entityID) .FastConcat(" group: ").FastConcat(fromEntityGID.groupID)); } //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor! bool correctEntityDescriptorFound = true; EntityInfoView entityInfoView = default; if (fromGroup.TryGetValue(ENTITY_INFO_VIEW_TYPE, out var entityInfoViewDic) && (entityInfoViewDic as TypeSafeDictionary <EntityInfoView>).TryGetValue( fromEntityGID.entityID, out entityInfoView) && (correctEntityDescriptorFound = entityInfoView.type == originalDescriptorType)) { var entitiesToMove = entityInfoView.entitiesToBuild; Dictionary <Type, ITypeSafeDictionary> toGroup = null; if (toEntityGID != null) { var toGroupID = toEntityGID.Value.groupID; if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false) { toGroup = _groupEntityDB[toGroupID] = new Dictionary <Type, ITypeSafeDictionary>(); } } for (int i = 0; i < entitiesToMove.Length; i++) { MoveEntityView(fromEntityGID, toEntityGID, toGroup, ref fromGroup, entitiesToMove[i].GetEntityType(), profiler); } } //otherwise it's a normal static entity descriptor else { #if DEBUG && !PROFILER if (correctEntityDescriptorFound == false) { throw new ECSException(INVALID_DYNAMIC_DESCRIPTOR_ERROR.FastConcat(" ID ").FastConcat(fromEntityGID.entityID) .FastConcat(" group ID ").FastConcat(fromEntityGID.groupID).FastConcat( " descriptor found: ", entityInfoView.type.Name, " descriptor Excepted ", originalDescriptorType.Name)); } #endif Dictionary <Type, ITypeSafeDictionary> toGroup = null; if (toEntityGID != null) { var toGroupID = toEntityGID.Value.groupID; if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false) { toGroup = _groupEntityDB[toGroupID] = new Dictionary <Type, ITypeSafeDictionary>(); } } for (var i = 0; i < entityBuilders.Length; i++) { MoveEntityView(fromEntityGID, toEntityGID, toGroup, ref fromGroup, entityBuilders[i].GetEntityType(), profiler); } } } }
void MoveEntityView(EGID entityGID, EGID?toEntityGID, Dictionary <Type, ITypeSafeDictionary> toGroup, ref Dictionary <Type, ITypeSafeDictionary> fromGroup, Type entityViewType, in PlatformProfiler profiler)
void MoveEntityViewFromAndToEngines(EGID entityGID, EGID?toEntityGID, FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup, FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup, Type entityViewType, in PlatformProfiler profiler)
///-------------------------------------------- /// void MoveEntityFromAndToEngines(IEntityBuilder[] entityBuilders, EGID fromEntityGID, EGID?toEntityGID) { using (var sampler = new PlatformProfiler("Move Entity From Engines")) { //for each entity view generated by the entity descriptor if (_groupEntityViewsDB.TryGetValue(fromEntityGID.groupID, out var fromGroup) == false) { throw new ECSException("from group not found eid: ".FastConcat(fromEntityGID.entityID) .FastConcat(" group: ").FastConcat(fromEntityGID.groupID)); } //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor! if (fromGroup.TryGetValue(new RefWrapper <Type>(EntityBuilderUtilities.ENTITY_STRUCT_INFO_VIEW), out var entityInfoViewDic) && (entityInfoViewDic as TypeSafeDictionary <EntityStructInfoView>).TryGetValue( fromEntityGID.entityID, out var entityInfoView)) { MoveEntities(fromEntityGID, toEntityGID, entityInfoView.entitiesToBuild, fromGroup, sampler); } //otherwise it's a normal static entity descriptor else { MoveEntities(fromEntityGID, toEntityGID, entityBuilders, fromGroup, sampler); } } }
///-------------------------------------------- /// void MoveEntityFromAndToEngines(IEntityBuilder[] entityBuilders, EGID fromEntityGID, EGID?toEntityGID) { using (var sampler = new PlatformProfiler("Move Entity From Engines")) { var fromGroup = GetGroup(fromEntityGID.groupID); //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor! if (fromGroup.TryGetValue(new RefWrapper <Type>(EntityBuilderUtilities.ENTITY_STRUCT_INFO_VIEW), out var entityInfoViewDic) && (entityInfoViewDic as ITypeSafeDictionary <EntityStructInfoView>).TryGetValue(fromEntityGID.entityID, out var entityInfoView)) { MoveEntityViews(fromEntityGID, toEntityGID, entityInfoView.entitiesToBuild, fromGroup, sampler); } //otherwise it's a normal static entity descriptor else { MoveEntityViews(fromEntityGID, toEntityGID, entityBuilders, fromGroup, sampler); } } }