示例#1
0
        public void ReturnTest()
        {
            var idc = 0;

            var list = new FreeList <int>(() => idc++);

            Assert.Equal(0, list.Take());
            Assert.Equal(1, list.Take());
            Assert.Equal(2, list.Take());
            Assert.Equal(3, list.Take());
            Assert.Equal(4, list.Take());
            Assert.Equal(5, list.Take());

            list.Return(2);
            list.Return(3);

            Assert.Equal(3, list.Take());
            Assert.Equal(2, list.Take());
            Assert.Equal(6, list.Take());
        }
示例#2
0
        public virtual bool Delete(int id)
        {
            if (!componentToEntityMap.Remove(id))
            {
                return(false);
            }

            aliveComponents.Remove(id);
            componentToEntityMap.Remove(id);

            freeComponents.Return(id);

            return(true);
        }
示例#3
0
        public void Delete(int id)
        {
            if (!aliveEntities.Remove(id))
            {
                throw new InvalidOperationException($"entity {id} not alive");
            }

            // Notify events.
            deletedEvents.Publish(id, e => e(id));

            // Delete and unpair children.
            if (entityChildren.TryGetValue(id, out var children))
            {
                foreach (var childId in children)
                {
                    Unpair(id, childId);

                    Delete(childId);
                }

                entityChildren.Remove(id);
            }

            // Unpair from parents.
            if (entityParents.TryGetValue(id, out var parents))
            {
                foreach (var parentId in parents)
                {
                    Unpair(parentId, id);
                }

                parents.Remove(id);
            }

            // Clear rest of the state and return id to pool.
            deletedEvents.Delete(id);

            unpairedFromChildEvents.Delete(id);
            unpairedFromParentEvents.Delete(id);

            madeParentOfEvents.Delete(id);
            madeChildOfEvents.Delete(id);

            entityTags.Remove(id);
            entityAnnotations.Remove(id);

            freeEntities.Return(id);
        }