Пример #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 int Create(uint annotation = 0u, string tag = "")
        {
            var id = freeEntities.Take();

            if (!Create(id, annotation, tag))
            {
                throw new InvalidOperationException($"duplicated internal entity id {id}");
            }

            return(id);
        }
Пример #3
0
        public void TakeTest()
        {
            var idc = 0;

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

            for (var i = 0; i < 10; i++)
            {
                Assert.Equal(i, list.Take());
            }
        }
Пример #4
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());
        }