예제 #1
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);
        }