/// <summary> /// Destroys the entity /// </summary> /// <param name="entityID"></param> public void DestroyEntity(ref UID entity) { if (applicationIsQuitting) { return; } if (_entities.TryGetValue(entity, out IComponent[] entityComponents))
void UpdateEntity(UID entity) { var entityComponents = GetSystemComponentsForEntity(entity); GetEntityComponents(entityComponents, entity); updatedComponents.Add(entityComponents); }
public void RemoveComponent(UID entity, IComponent component) { if (applicationIsQuitting) { return; } RemoveComponent(entity, component.GetType()); }
/// <summary> /// Removes the component from the entity /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> public void RemoveComponent <T>(UID entity) where T : IComponent { if (applicationIsQuitting) { return; } RemoveComponent(entity, GetComponent <T>(entity)); }
private TComponents _CreateSystemComponentsForEntity(UID entity) { TComponents tc = new TComponents(); tc.Entity = entity; // TODO: Get rid of this again //tc.EntityManager = entityManager; return(GetEntityComponents(tc, entity)); }
/// <summary> /// Creates a new entity /// </summary> /// <returns></returns> protected UID ThreadSafeCreateEntity(int id, int revision) { //UnityEngine.Debug.Log(uid.ID); lock (_entities) { UID uid = new UID(id, revision); _entities.Add(uid, new List <IComponent>()); //_entityIDs.Add(uid); return(uid); } }
/// <summary> /// Checks if the entity is alive/exists /// </summary> /// <param name="entityID"></param> /// <returns></returns> public bool EntityExists(UID entity) { //return _entities.ContainsKey(entity); if (entity.ID == 0) { return(false); } return(_entityIDs.Contains(entity.ID)); }
/// <summary> /// Removes the component from the entity /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> public void RemoveComponent <T>(UID entity) where T : IComponent { if (applicationIsQuitting) { return; } Type type = typeof(T); RemoveComponent(entity, type); }
/// <summary> /// Gets all components for an entity. Returns null if entity does not exist /// </summary> /// <param name="entity"></param> /// <returns></returns> public List <IComponent> GetAllComponents(UID entity) { if (EntityExists(entity)) { return(_entities[entity].ToList()); } else { return(null); } }
void RegisterEntity(UID entity) { //UnityEngine.Debug.Log(entity.ID + "valid! Adding to system!"); validEntities.Add(entity); //Add components to process TComponents components = _CreateSystemComponentsForEntity(entity); componentsToProcess.Add(components); newComponents.Add(components); }
/// <summary> /// Creates a new entity /// </summary> /// <returns></returns> protected UID CreateEntity(int id) { UID uid = new UID(id); //UnityEngine.Debug.Log(uid.ID); _entities.Add(uid, new HashSet <IComponent>()); _entityIDs.Add(uid.ID); return(uid); }
public IComponent AddComponent(UID entity, Type componentType) { if (EntityExists(entity)) { if (!HasComponent(entity, componentType)) { IComponent component = CreateComponentFromType(componentType); return(AddComponent(entity, component)); } } return(null); }
/// <summary> /// Creates a new entity /// </summary> /// <returns></returns> protected UID CreateEntity(int id, int revision) { UID uid = new UID(id, revision); //UnityEngine.Debug.Log(uid.ID); _entities.Add(uid, new IComponent[initialArrayCapacity]); _entityComponentTypes[uid] = new Dictionary <Type, int>(); //_entityIDs.Add(uid); return(uid); }
void UpdateEntity(UID entity) { TComponents entityComponents = GetSystemComponentsForEntity(entity); GetEntityComponents(entityComponents, entity); if (entityComponents != null && !updatedComponentsLUT.Contains(entityComponents)) { updatedComponents.Add(entityComponents); updatedComponentsLUT.Add(entityComponents); } }
public IComponent AddComponent(UID entity, IComponent component) { if (EntityExists(entity) && !HasComponent(entity, component.GetType())) { //component.Entity.SetID(entity.ID); component.Entity = entity; SetupComponentID(component); _entities[entity].Add(component); _EntityModified(entity); //UnityEngine.Debug.Log("Added component " + component.GetType() + " to entity:" + entity.ID); return(component); } return(null); }
public void RemoveComponent(UID entity, IComponent component) { if (applicationIsQuitting) { return; } if (component != null && EntityExists(entity) && HasComponent(entity, component)) { _entities[entity].Remove(component); component.Entity.SetID(-1); _EntityModified(entity); } }
/// <summary> /// Get components for entity /// </summary> /// <param name="entity"></param> /// <returns></returns> protected TComponents GetSystemComponentsForEntity(UID entity) { //TComponents components = componentsToProcess.Find(o => o.Entity == entity); TComponents components; if (componentsToProcessLUT.TryGetValue(entity.ID, out components)) { return(components); } else { return(default(TComponents)); } }
public bool HasComponent(UID entity, IComponent component) { if (EntityExists(entity)) { //TODO: This is "slow" and produces garbage. Rethink how entities/components are stored. //IComponent c = _entities[entity].Find(o => o == component); //if (c != null) { // return true; //} //Hashset version. More gc friendly and less laggy, especially when creating LOTS of entities in one frame return(_entities[entity].Contains(component)); } return(false); }
/// <summary> /// Adds the component to the entity. If component already exists, no new component will be added and existing component will be returned. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> public T AddComponent <T>(UID entity) where T : IComponent, new() { //UnityEngine.Debug.Log("Adding component "+typeof(T)+" to entity:" + entity.ID); if (EntityExists(entity)) { if (!HasComponent <T>(entity)) { IComponent component = new T(); return((T)AddComponent(entity, component)); } else { return(GetComponent <T>(entity)); } } return(default(T)); }
/// <summary> /// Gets the component from the entity /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public T GetComponent <T>(UID entity) where T : IComponent { if (_entities.TryGetValue(entity, out List <IComponent> components)) { int _componentsCount = components.Count; Type t = typeof(T); for (int i = 0; i < _componentsCount; ++i) { IComponent comp = components[i]; if (comp.GetType() == t) { return((T)comp); } } } return(default);
/// <summary> /// Destroys the entity /// </summary> /// <param name="entityID"></param> public void DestroyEntity(ref UID entity) { if (applicationIsQuitting) { return; } if (EntityExists(entity)) { _entities[entity].Clear(); _entityIDs.Remove(entity.ID); _EntityModified(entity); _entities[entity] = null; _entities.Remove(entity); _recycledEntityIds.Enqueue(entity.ID); entity.ID = 0; // make it NULL } }
/// <summary> /// Set the entities component for this type. A component of this type already added to this entity /// will be removed beforehand /// </summary> /// <param name="entity"></param> /// <param name="component"></param> /// <returns></returns> public IComponent SetComponent(UID entity, IComponent component) { if (_entities.TryGetValue(entity, out List <IComponent> comps)) { RemoveComponent(entity, component); if (component != null) { component.Entity = entity; SetupComponentID(component); comps.Add(component); } } //component.Entity.SetID(entity.ID); _EntityModified(entity); //UnityEngine.Debug.Log("Added component " + component.GetType() + " to entity:" + entity.ID); return(component); }
/// <summary> /// /// </summary> /// <param name="entity"></param> void UnregisterEntity(UID entity) { //UnityEngine.Debug.Log(entity.ID + " invalid! Removing from system!"); //Remove components to process int _entityID = entity.ID; //TODO: Find a faster way to remove the components. TComponents components = GetSystemComponentsForEntity(entity); if (components != null) { componentsToProcess.Remove(components); } //componentsToProcess.RemoveWhere(v => v.Entity.ID == _entityID); validEntities.Remove(entity); removedComponents.Add(components); }
public bool HasComponent(UID entity, Type componentType) { if (EntityExists(entity)) { IComponent c = null;// foreach (IComponent comp in _entities[entity]) { if (comp.GetType() == componentType) { c = comp; break; } } if (c != null) { return(true); } } return(false); }
public void RemoveComponent(UID entity, Type componentType) { if (applicationIsQuitting) { return; } if (_entities.TryGetValue(entity, out List <IComponent> comps)) { for (int i = comps.Count - 1; i >= 0; i--) { if (comps[i].GetType() == componentType) { comps.RemoveAt(i); _EntityModified(entity); return; } } } }
/// <summary> /// Destroys the entity /// </summary> /// <param name="entityID"></param> public void DestroyEntity(ref UID entity) { if (applicationIsQuitting) { return; } if (EntityExists(entity)) { //Dispose entity components while (_entities[entity].Count > 0) { DisposeComponent(_entities[entity].First()); } _entities[entity].Clear(); //_entityIDs.Remove(entity); _EntityModified(entity); _entities[entity] = null; _entities.Remove(entity); _recycledEntityIds.Enqueue(entity); entity.SetNull(); // make it NULL } }
/// <summary> /// Checks if an entity has the component /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public bool HasComponent <T>(UID entity) where T : IComponent { if (EntityExists(entity)) { //TODO: This is "slow" and produces garbage. Rethink how entities/components are stored. //IComponent c = _entities[entity].Find(o => o is T); //Hashset version. More gc friendly and less laggy, especially when creating LOTS of entities in one frame IComponent c = null;// foreach (IComponent comp in _entities[entity]) { if (comp is T) { c = comp; break; } } if (c != null) { return(true); } } return(false); }
/// <summary> /// Call whenever an entity is modified. Entity might no longer be valid for this system. /// </summary> /// <param name="entity"></param> public virtual void EntityModified(UID entity) { bool valid = IsEntityValid(entity); bool wasValid = validEntities.Contains(entity); //if(validEntities.Find(v => v.ID == entity.ID).ID > 0) { // wasValid = true; //} //UnityEngine.Debug.Log(entity.ID + "valid: "+valid); if (valid && wasValid) { UpdateEntity(entity); } else if (valid && !wasValid) { RegisterEntity(entity); } else if (!valid && wasValid) { UnregisterEntity(entity); } }
/// <summary> /// Caches all needed components from the entity /// </summary> /// <param name="components"></param> /// <returns></returns> protected abstract TComponents GetEntityComponents(TComponents components, UID entity);
/// <summary> /// Checks if the entity can be used by the system /// </summary> /// <param name="entity"></param> /// <returns></returns> protected abstract bool IsEntityValid(UID entity);