Exemplo n.º 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))
Exemplo n.º 2
0
        void UpdateEntity(UID entity)
        {
            var entityComponents = GetSystemComponentsForEntity(entity);

            GetEntityComponents(entityComponents, entity);

            updatedComponents.Add(entityComponents);
        }
Exemplo n.º 3
0
        public void RemoveComponent(UID entity, IComponent component)
        {
            if (applicationIsQuitting)
            {
                return;
            }

            RemoveComponent(entity, component.GetType());
        }
Exemplo n.º 4
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;
            }

            RemoveComponent(entity, GetComponent <T>(entity));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 7
0
        /// <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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 9
0
 /// <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);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 11
0
        /// <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);
        }
Exemplo n.º 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);
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
 }
Exemplo n.º 16
0
        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);
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 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));
 }
Exemplo n.º 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);
Exemplo n.º 21
0
        /// <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
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 24
0
 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);
 }
Exemplo n.º 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;
                    }
                }
            }
        }
Exemplo n.º 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
            }
        }
Exemplo n.º 27
0
        /// <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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
Exemplo n.º 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);