Ejemplo n.º 1
0
        public override void Execute(GameState state)
        {
            var term = state.Backend;

            term.BackgroundColor = new ColorValue(Color16.Black);
            term.Clear();
            // enumerate through entity view
            foreach (var id in _drawableIds)
            {
                var position = _physObjects.Get(id).Position;
                position.X = MathF.Round(position.X);
                position.Y = MathF.Round(position.Y);
                var drawable = _drawables.Get(id);

                // draw it
                term.SetCursorPosition((int)position.X, (int)position.Y);
                term.ForegroundColor = drawable.Color;
                term.Write(drawable.Symbol.ToString());
            }
        }
Ejemplo n.º 2
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);
            }
        }