예제 #1
0
        protected virtual int InitializeComponent(int entityId)
        {
            var id = freeComponents.Take();

            // Make sure this component is not duplicated.
            if (componentToEntityMap.ContainsKey(id))
            {
                throw new InvalidOperationException($"duplicated internal component id {id}");
            }

            aliveComponents.Add(id);
            componentToEntityMap.Add(id, entityId);

            // Create events.
            deletedEvents.Create(id);

            // Delete the component when entity is deleted.
            entities.Deleted.Subscribe(entityId, delegate
            {
                if (!Alive(id))
                {
                    return;
                }

                Delete(id);
            });

            return(id);
        }
예제 #2
0
        public bool Create(int id, uint annotation = 0u, string tag = "")
        {
            if (Alive(id))
            {
                return(false);
            }

            // Add to lookups.
            entityAnnotations[id] = annotation;
            entityTags[id]        = tag;

            aliveEntities.Add(id);

            // Create events.
            deletedEvents.Create(id);

            unpairedFromChildEvents.Create(id);
            unpairedFromParentEvents.Create(id);

            madeParentOfEvents.Create(id);
            madeChildOfEvents.Create(id);

            return(true);
        }