Пример #1
0
def query_setup():
    world = World()

    class Cmp1(RegisteredComponent):
        pass

    class Cmp2(RegisteredComponent):
        pass

    class Cmp3(RegisteredComponent):
        pass

    for components in [
        (),
        (Cmp1, ),
        (Cmp2, ),
        (Cmp1, Cmp2),
        (Cmp3, ),
        (Cmp3, Cmp1),
        (Cmp3, Cmp2),
        (Cmp3, Cmp2, Cmp1),
    ]:
        new_entity([c() for c in components], world=world)

    return world, (Cmp1, Cmp2, Cmp3)
Пример #2
0
def test_entity_creation_no_components():
    world = World()
    for i in range(3):
        ent_id = new_entity(world=world)
        assert ent_id == EntityID(i + 1)  # entity IDs start at 1
    world = World()
    for i in range(3):
        ent_id = new_entity(world=world)
        assert ent_id == EntityID(i + 1)
Пример #3
0
def test_entity_creation_disallows_duplicate_components(world, component_a):
    pre_counter = copy(world._entity_counter)
    pre_entcache = deepcopy(world._entity_cache)
    pre_entities = deepcopy(world._entities)
    pre_bitmasks = deepcopy(world._entity_bitmasks)
    with pytest.raises(
            ValueError,
            match=r".*Adding multiple components of the same type to one entity "
            r"is not allowed.*",
    ):
        new_entity((component_a(), component_a()), world=world)
    # Make sure the world state hasn't changed
    assert world._entity_counter == pre_counter
    assert world._entities == pre_entities
    assert world._entity_cache == pre_entcache
    assert world._entity_bitmasks == pre_bitmasks
Пример #4
0
def test_component_lookups_dynamic_snecs(snecs_world, benchmark, backend,
                                         name):
    c1 = SnecsComponentA()
    c2 = SnecsComponentB()
    c3 = SnecsComponentC()

    ents = [
        new_entity(components, snecs_world) for components in [
            (),
            (c1, ),
            (c2, ),
            (c1, c2),
            (c3, ),
            (c3, c1),
            (c3, c2),
            (c3, c2, c1),
        ] * 128
    ]

    def snecs_has_components():
        return [
            result for result in Query((SnecsComponentA,
                                        SnecsComponentC), snecs_world)
        ]

    benchmark(snecs_has_components)
Пример #5
0
def test_has_components_snecs(snecs_world, benchmark, backend, name):
    c1 = SnecsComponentA()
    c2 = SnecsComponentB()
    c3 = SnecsComponentC()

    ents = [
        new_entity(components, snecs_world) for components in [
            (),
            (c1, ),
            (c2, ),
            (c1, c2),
            (c3, ),
            (c3, c1),
            (c3, c2),
            (c3, c2, c1),
        ] * 128
    ]

    def snecs_has_components():
        return [
            has_components(ent, (SnecsComponentA, SnecsComponentC),
                           snecs_world) for ent in ents
        ]

    benchmark(snecs_has_components)
Пример #6
0
def test_entity_creation_adds_to_entity_cache(world, component_a):
    c = component_a()
    ent_id = new_entity((c, ), world=world)
    assert ent_id in world._entities
    assert world._entities[ent_id] == {component_a: c}

    assert ent_id in world._entity_bitmasks
    assert world._entity_bitmasks[ent_id] == component_a._bitmask

    assert component_a in world._entity_cache
    assert world._entity_cache[component_a] == {ent_id}
Пример #7
0
def test_serialize(world, serializable_component_a, serializable_component_b):
    ent1 = new_entity(
        (serializable_component_a(1), serializable_component_b(2, 3)), world)

    ent2 = new_entity(
        (
            serializable_component_a("abc"),
            serializable_component_b([1, 2], [3, 4]),
        ),
        world,
    )

    serialized = serialize_world(world)
    assert serialized == {
        SERIALIZED_COMPONENTS_KEY: [
            serializable_component_a.__name__,
            serializable_component_b.__name__,
        ],
        SERIALIZED_ENTITIES_KEY: {
            ent1: {
                0: 1,
                1: (2, 3)
            },
            ent2: {
                0: "abc",
                1: ([1, 2], [3, 4])
            },
        },
    }

    new_world = deserialize_world(json.loads(json.dumps(serialized)))
    new_serialized = serialize_world(new_world)
    assert serialized == new_serialized

    empty_world = move_world(new_world, World())
    assert serialize_world(empty_world) == new_serialized
Пример #8
0
def test_has_component_snecs(snecs_world, benchmark, backend, name):
    c1 = SnecsComponentA()
    c2 = SnecsComponentB()
    c3 = SnecsComponentC()

    ents = [
        new_entity(components, snecs_world) for components in [
            (),
            (c1, ),
            (c2, ),
            (c1, c2),
            (c3, ),
            (c3, c1),
            (c3, c2),
            (c3, c2, c1),
        ] * 128
    ]

    def snecs_has_component():
        return [
            has_component(ent, SnecsComponentA, snecs_world) for ent in ents
        ]

    benchmark.pedantic(snecs_has_component, rounds=2000)
Пример #9
0
def test_exists(world):
    ent = new_entity((), world)
    assert exists(ent, world)
    assert not exists(ent + 1, world)
Пример #10
0
def entity_with_cmp_a(world, component_a):
    return new_entity((component_a(), ), world=world)
Пример #11
0
def empty_entity(world):
    return new_entity(world=world)
Пример #12
0
 def create_entities():
     new_entity(world=snecs_world)
Пример #13
0
 def setup():
     ents = [
         new_entity((c1, c2, c3), world=snecs_world) for _ in range(1_000)
     ]
     return (ents, ), {}
Пример #14
0
 def create_entities():
     new_entity((c1, c2, c3), snecs_world)