Exemple #1
0
 public override void Execute(object state)
 {
     sampleComponent.Process((id, val) =>
     {
         var newValue = new SampleComponent()
         {
             Foo = val.Bar ? id + 1 : id,
             Bar = !val.Bar
         };
         return(newValue);
     });
 }
Exemple #2
0
        public void ShouldSetComponentValueIfSupplied()
        {
            container
            .AddComponent <SampleComponent>()
            .Build();
            var em      = container.EntityManager;
            var id      = em.Create();
            var storage = container.GetStorageFor <SampleComponent>();

            em.HasComponent <SampleComponent>(id).Should().BeFalse();

            var expected = new SampleComponent()
            {
                Foo = -1f,
                Bar = !default(bool)
            };

            container.AddComponent(id, expected);
            em.HasComponent <SampleComponent>(id).Should().BeTrue();
            storage.Get(id).Should().Be(expected);
        }
Exemple #3
0
        public void ShouldIterateAndModify(IComponentCollection <SampleComponent> storage)
        {
            var em                = new EntityManager(t => storage as IComponentCollectionStorage);
            var withComponents    = new List <int>();
            var withoutComponents = new List <int>();

            for (var i = 0; i < 5; i++)
            {
                withoutComponents.Add(em.Create());
                var entityWithComponent = em.Create();
                em.AddComponent <SampleComponent>(entityWithComponent);
                withComponents.Add(entityWithComponent);
            }

            // Check if components were added where necessary
            withComponents.Should().OnlyContain(id => em.HasComponent <SampleComponent>(id));
            withoutComponents.Should().OnlyContain(id => !em.HasComponent <SampleComponent>(id));

            // Check IEntityView implementation
            var view = storage.GetView();

            view.Should().BeEquivalentTo(withComponents);
            view.EntityCount.Should().Be(withComponents.Count);
            view.Filter.Should().BeEquivalentTo(new[] { typeof(SampleComponent) });
            Span <int> viewData       = stackalloc int[withComponents.Count];
            var        actualViewData = view.CopyTo(viewData).ToArray();

            actualViewData.Should().BeEquivalentTo(withComponents);

            // Try to iterate using foreach
            var count = 0;

            foreach (var component in storage.GetValues())
            {
                count++;
            }
            count.Should().Be(withComponents.Count);

            // Try to iterate and modify
            count = 0;
            var ids = storage.GetView().ToList();

            foreach (var id in ids)
            {
                var value = new SampleComponent()
                {
                    Foo = 1337,
                    Bar = true
                };
                storage.Update(id, value);
                count++;
            }

            count.Should().Be(withComponents.Count);

            // Check if they were modified
            storage.GetValues().Should().OnlyContain(c => c.Foo == 1337);
            storage.GetValues().Should().OnlyContain(c => c.Bar == true);

            // Modify again and delete component from every second entity
            var iterations = 0;

            count = 0;
            storage.ForEach((id, componentValue, _) =>
            {
                componentValue.Foo = id;
                storage.UpdateCurrent(componentValue);
                if (iterations % 2 == 1)
                {
                    em.RemoveComponent <SampleComponent>(id);
                }
                else
                {
                    count++;
                }
                iterations++;
            }, default(object));
            iterations.Should().Be(withComponents.Count);
            var oddsAndEvens = withComponents
                               .Select((item, i) => (item, isEven: i % 2 == 0))
                               .ToLookup(x => x.isEven, x => x.item);

            // Check if the components were removed
            oddsAndEvens[true].Should().OnlyContain(id => em.HasComponent <SampleComponent>(id));
            oddsAndEvens[false].Should().OnlyContain(id => !em.HasComponent <SampleComponent>(id));

            // Check if the alive ones was updated
            storage.ForEach((id, val, _) =>
            {
                val.Foo.Should().Be(id);
                val.Bar.Should().BeTrue();
            }, default(object));

            // Modify and get in different order
            // Note: this is slower than the ForEach due to additional lookups
            var alive = oddsAndEvens[true].Reverse();

            foreach (var id in alive)
            {
                var newValue = new SampleComponent()
                {
                    Foo = id + 1
                };
                storage.Update(id, newValue);
            }

            foreach (var id in alive.Reverse())
            {
                storage.Get(id).Foo.Should().Be(id + 1);
            }
        }
Exemple #4
0
 private bool Equals(SampleComponent other)
 {
     return(Foo == other.Foo && Bar == other.Bar);
 }