add filter callback system
							parent
							
								
									e4131d58f5
								
							
						
					
					
						commit
						1438188dca
					
				|  | @ -23,5 +23,15 @@ namespace MoonTools.ECS | |||
| 
 | ||||
| 		// WARNING: this WILL crash if the index is out of range! | ||||
| 		public Entity NthEntity(int index) => FilterStorage.FilterNthEntity(Signature, index); | ||||
| 
 | ||||
| 		public void RegisterAddCallback(Action<Entity> callback) | ||||
| 		{ | ||||
| 			FilterStorage.RegisterAddCallback(Signature, callback); | ||||
| 		} | ||||
| 
 | ||||
| 		public void RegisterRemoveCallback(Action<Entity> callback) | ||||
| 		{ | ||||
| 			FilterStorage.RegisterRemoveCallback(Signature, callback); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -10,6 +10,9 @@ namespace MoonTools.ECS | |||
| 		private Dictionary<FilterSignature, IndexableSet<Entity>> filterSignatureToEntityIDs = new Dictionary<FilterSignature, IndexableSet<Entity>>(); | ||||
| 		private Dictionary<int, HashSet<FilterSignature>> typeToFilterSignatures = new Dictionary<int, HashSet<FilterSignature>>(); | ||||
| 
 | ||||
| 		private Dictionary<FilterSignature, Action<Entity>> addCallbacks = new Dictionary<FilterSignature, Action<Entity>>(); | ||||
| 		private Dictionary<FilterSignature, Action<Entity>> removeCallbacks = new Dictionary<FilterSignature, Action<Entity>>(); | ||||
| 
 | ||||
| 		public FilterStorage(EntityStorage entityStorage, TypeIndices componentTypeIndices) | ||||
| 		{ | ||||
| 			EntityStorage = entityStorage; | ||||
|  | @ -116,6 +119,10 @@ namespace MoonTools.ECS | |||
| 				if (!EntityStorage.HasComponent(entityID, type)) | ||||
| 				{ | ||||
| 					filterSignatureToEntityIDs[filterSignature].Remove(entityID); | ||||
| 					if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback)) | ||||
| 					{ | ||||
| 						removeCallback(entityID); | ||||
| 					} | ||||
| 					return; | ||||
| 				} | ||||
| 			} | ||||
|  | @ -125,11 +132,19 @@ namespace MoonTools.ECS | |||
| 				if (EntityStorage.HasComponent(entityID, type)) | ||||
| 				{ | ||||
| 					filterSignatureToEntityIDs[filterSignature].Remove(entityID); | ||||
| 					if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback)) | ||||
| 					{ | ||||
| 						removeCallback(entityID); | ||||
| 					} | ||||
| 					return; | ||||
| 				} | ||||
| 			} | ||||
| 
 | ||||
| 			filterSignatureToEntityIDs[filterSignature].Add(entityID); | ||||
| 			if (addCallbacks.TryGetValue(filterSignature, out var addCallback)) | ||||
| 			{ | ||||
| 				addCallback(entityID); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		public void RemoveEntity(int entityID, int componentTypeIndex) | ||||
|  | @ -139,8 +154,22 @@ namespace MoonTools.ECS | |||
| 				foreach (var filterSignature in filterSignatures) | ||||
| 				{ | ||||
| 					filterSignatureToEntityIDs[filterSignature].Remove(entityID); | ||||
| 					if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback)) | ||||
| 					{ | ||||
| 						removeCallback(entityID); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		public void RegisterAddCallback(FilterSignature filterSignature, Action<Entity> callback) | ||||
| 		{ | ||||
| 			addCallbacks.Add(filterSignature, callback); | ||||
| 		} | ||||
| 
 | ||||
| 		public void RegisterRemoveCallback(FilterSignature filterSignature, Action<Entity> callback) | ||||
| 		{ | ||||
| 			removeCallbacks.Add(filterSignature, callback); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -38,12 +38,12 @@ namespace MoonTools.ECS | |||
| 				throw new InvalidOperationException("This entity is not valid!"); | ||||
| 			} | ||||
| #endif | ||||
| 			ComponentDepot.Set<TComponent>(entity.ID, component); | ||||
| 
 | ||||
| 			if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex<TComponent>())) | ||||
| 			{ | ||||
| 				FilterStorage.Check<TComponent>(entity.ID); | ||||
| 			} | ||||
| 
 | ||||
| 			ComponentDepot.Set<TComponent>(entity.ID, component); | ||||
| 		} | ||||
| 
 | ||||
| 		public Template CreateTemplate() | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue