Example #1
0
        /// <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))
Example #2
0
        void UpdateEntity(UID entity)
        {
            var entityComponents = GetSystemComponentsForEntity(entity);

            GetEntityComponents(entityComponents, entity);

            updatedComponents.Add(entityComponents);
        }
Example #3
0
        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));
        }
Example #5
0
        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));
        }
Example #6
0
 /// <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));
        }
Example #8
0
        /// <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);
     }
 }
Example #10
0
        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);
        }
Example #12
0
 public IComponent AddComponent(UID entity, Type componentType)
 {
     if (EntityExists(entity))
     {
         if (!HasComponent(entity, componentType))
         {
             IComponent component = CreateComponentFromType(componentType);
             return(AddComponent(entity, component));
         }
     }
     return(null);
 }
Example #13
0
        /// <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);
        }
Example #14
0
        void UpdateEntity(UID entity)
        {
            TComponents entityComponents = GetSystemComponentsForEntity(entity);

            GetEntityComponents(entityComponents, entity);

            if (entityComponents != null && !updatedComponentsLUT.Contains(entityComponents))
            {
                updatedComponents.Add(entityComponents);
                updatedComponentsLUT.Add(entityComponents);
            }
        }
Example #15
0
 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);
            }
        }
Example #17
0
        /// <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);
        }
Example #19
0
 /// <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));
 }
Example #20
0
        /// <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
            }
        }
Example #22
0
        /// <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);
        }
Example #23
0
        /// <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);
 }
Example #25
0
        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;
                    }
                }
            }
        }
Example #26
0
        /// <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);
        }
Example #28
0
        /// <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);
            }
        }
Example #29
0
 /// <summary>
 /// Caches all needed components from the entity
 /// </summary>
 /// <param name="components"></param>
 /// <returns></returns>
 protected abstract TComponents GetEntityComponents(TComponents components, UID entity);
Example #30
0
 /// <summary>
 /// Checks if the entity can be used by the system
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 protected abstract bool IsEntityValid(UID entity);