示例#1
0
        public void Should_behave_correctly_when_key_changed()
        {
            using World world = new World();

            using EntitiesMap <int> map = world.GetEntities().AsMultiMap <int>();

            Entity entity = world.CreateEntity();

            entity.Set(42);

            Check.That(map.TryGetEntities(42, out ReadOnlySpan <Entity> result)).IsTrue();
            Check.That(result.ToArray()).ContainsExactly(entity);

            entity.Set(1337);

            Check.That(map.TryGetEntities(42, out result)).IsFalse();
            Check.That(map.TryGetEntities(1337, out result)).IsTrue();
            Check.That(result.ToArray()).ContainsExactly(entity);

            entity.Get <int>() = 42;
            entity.NotifyChanged <int>();

            Check.That(map.TryGetEntities(1337, out result)).IsFalse();
            Check.That(map.TryGetEntities(42, out result)).IsTrue();
            Check.That(result.ToArray()).ContainsExactly(entity);
        }
示例#2
0
        public void TryGetEntities_Should_return_weither_a_key_is_in_or_not()
        {
            using World world = new World();

            using EntitiesMap <int> map = world.GetEntities().AsMultiMap <int>();

            Entity entity = world.CreateEntity();

            Check.That(map.TryGetEntities(42, out ReadOnlySpan <Entity> result)).IsFalse();

            entity.Set(42);

            Check.That(map.TryGetEntities(42, out result)).IsTrue();
            Check.That(result.ToArray()).ContainsExactly(entity);

            entity.Disable <int>();

            Check.That(map.TryGetEntities(42, out result)).IsFalse();

            entity.Enable <int>();

            Check.That(map.TryGetEntities(42, out result)).IsTrue();
            Check.That(result.ToArray()).ContainsExactly(entity);

            entity.Remove <int>();

            Check.That(map.TryGetEntities(42, out result)).IsFalse();
        }
示例#3
0
        protected override void Update(float state, ReadOnlySpan <Entity> entities)
        {
            Components <CellId>   gridIds    = _world.GetComponents <CellId>();
            Components <Behavior> behaviors  = _world.GetComponents <Behavior>();
            Components <Velocity> velocities = _world.GetComponents <Velocity>();
            Components <DrawInfo> drawInfos  = _world.GetComponents <DrawInfo>();

            foreach (ref readonly Entity entity in entities)
            {
                Vector2 center    = Vector2.Zero;
                Vector2 direction = Vector2.Zero;
                int     count     = 0;

                if (_grid.TryGetEntities(gridIds[entity], out ReadOnlySpan <Entity> neighbors))
                {
                    foreach (ref readonly Entity neighbor in neighbors)
                    {
                        center    += drawInfos[neighbor].Position;
                        direction += velocities[neighbor].Value;
                    }

                    count = neighbors.Length;
                }

                behaviors[entity] = new Behavior(center, direction, count);
            }
        }
示例#4
0
        public void Complete_Should_empty_When_reative()
        {
            using World world = new World();

            using EntitiesMap <int> map = world.GetEntities().WhenAddedEither <int>().AsMultiMap <int>();

            Entity entity = world.CreateEntity();

            entity.Set(42);

            Check.That(map.TryGetEntities(42, out ReadOnlySpan <Entity> result)).IsTrue();
            Check.That(result.ToArray()).ContainsExactly(entity);

            map.Complete();

            Check.That(map.TryGetEntities(42, out result)).IsFalse();

            entity.Set(1337);

            Check.That(map.TryGetEntities(42, out result)).IsFalse();
        }