Last active
September 20, 2018 16:20
-
-
Save mzaks/37b115853e6b191446d2 to your computer and use it in GitHub Desktop.
Example of an index in Entitas-CSharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NameIndex { | |
| Group observedCollection; | |
| Dictionary<string, Entity> lookup = new Dictionary<string, Entity>(); | |
| public NameIndex(Pool pool){ | |
| observedCollection = pool.GetGroup(Matcher.Name); | |
| observedCollection.OnEntityAdded += AddEntity; | |
| observedCollection.OnEntityRemoved += RemoveEntity; | |
| } | |
| ~NameIndex() | |
| { | |
| Cleanup(); | |
| } | |
| public void Cleanup() | |
| { | |
| observedCollection.OnEntityAdded -= AddEntity; | |
| observedCollection.OnEntityRemoved -= RemoveEntity; | |
| lookup.Clear(); | |
| } | |
| public Entity FindEntityWithName(string name) | |
| { | |
| return lookup[name]; | |
| } | |
| protected virtual void AddEntity(Group collection, Entity entity, int index, IComponent component) | |
| { | |
| var nameComponent = component as NameComponent; | |
| if(nameComponent != null){ | |
| if(lookup.ContainsKey(nameComponent.value) && lookup[nameComponent.value] == entity){ | |
| return; | |
| } | |
| if(lookup.ContainsKey(nameComponent.value) && lookup[nameComponent.value] != entity){ | |
| throw new Exception("the key " + nameComponent.value + " is not unique. Present on entity: " + entity.creationIndex + " and entity: " + lookup[nameComponent.value].creationIndex); | |
| } | |
| entity.Retain(this); | |
| lookup[nameComponent.value] = entity; | |
| } | |
| } | |
| protected virtual void RemoveEntity(Group collection, Entity entity, int index, IComponent component) | |
| { | |
| var nameComponent = component as NameComponent; | |
| if(nameComponent != null && lookup.ContainsKey(nameComponent.value)){ | |
| lookup[nameComponent.value].Release(this); | |
| lookup.Remove(nameComponent.value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment