def test_compound_filter_3(world, entity): sub_filter_1 = and_filter([ComponentA]) sub_filter_2 = or_filter([ComponentB, ComponentC]) f = or_filter([sub_filter_1, sub_filter_2]) s = set() assert not f(s) assert not f(entity) s = set([ComponentA]) assert f(s) entity.add_component(ComponentA()) world._flush_component_updates() assert f(entity) s = set([ComponentB]) assert f(s) entity.remove_component(ComponentA) entity.add_component(ComponentB()) world._flush_component_updates() assert f(entity) s = set([ComponentC]) assert f(s) entity.remove_component(ComponentB) entity.add_component(ComponentC()) world._flush_component_updates() assert f(entity)
def update(self, entities_by_filter): for entity in entities_by_filter['foos']: # If removal of entities is not deferred until after the # system is run, this would lead to a RuntimeError, since # it'd be changing the list of entities that we are working # on right now. entity.remove_component(FooComponent)
def test_and_filter_with_entity(world, entity): f = and_filter([ComponentA, ComponentB]) assert not f(entity) entity.add_component(ComponentA()) world._flush_component_updates() assert not f(entity) entity.add_component(ComponentB()) world._flush_component_updates() assert f(entity) entity.remove_component(ComponentA) world._flush_component_updates() assert not f(entity)
def test_destroy_component_by_removing_component(world, entity, component, system, enabler): world.add_system(system, 0) entity.add_component(component) entity.add_component(enabler) world.flush_component_updates() assert system.destroy_called == 0 assert system.destroy_done == 0 assert component.inited entity.remove_component(type(component)) world.flush_component_updates() assert system.destroy_called == 2 # Both filters fail a once assert system.destroy_done == 1 # ...but only one destroyed the component assert not component.inited
def test_destroy_component_by_removing_enabler(world, entity, component, system, enabler): world.add_system(system, 0) entity.add_component(component) entity.add_component(enabler) world.flush_component_updates() assert system.destroy_called == 0 assert system.destroy_done == 0 assert component.inited entity.remove_component(type(enabler)) world.flush_component_updates() assert system.destroy_called == 1 assert system.destroy_done == 1 assert not component.inited
def test_basic_component_handling(world, entity): component = Counter(count=0, inited=False) assert entity.get_components() == set() assert not entity.has_component(Counter) entity.add_component(component) # flush should not be called directly. It's normally called by # world.update(). For testing purposes, however... world.flush_component_updates() assert entity.has_component(Counter) assert entity.get_component(Counter) is component entity.remove_component(Counter) world.flush_component_updates() assert entity.get_components() == set() assert not entity.has_component(Counter)
def test_or_filter(world, entity): f = or_filter([ComponentA, ComponentB]) assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() assert f(entity) entity.add_component(ComponentB()) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentA) world.flush_component_updates() assert f(entity) entity.remove_component(ComponentB) world.flush_component_updates() assert not f(entity)
def test_compound_filter_4(world, entity): sub_filter_1 = and_filter([ComponentA]) sub_filter_2 = or_filter([ComponentB, ComponentC]) f = and_filter([sub_filter_1, sub_filter_2]) # A and (B or C) assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() # A assert not f(entity) entity.remove_component(ComponentA) world.flush_component_updates() entity.add_component(ComponentB()) world.flush_component_updates() # B assert not f(entity) entity.add_component(ComponentA()) world.flush_component_updates() # A, B assert f(entity) entity.remove_component(ComponentB) entity.add_component(ComponentC()) world.flush_component_updates() # A, C assert f(entity) entity.remove_component(ComponentA) world.flush_component_updates() # C assert not f(entity)
def test_compound_filter_4(world, entity): sub_filter_1 = and_filter([ComponentA]) sub_filter_2 = or_filter([ComponentB, ComponentC]) f = and_filter([sub_filter_1, sub_filter_2]) # A and (B or C) # Empty s = set() assert not f(s) assert not f(entity) # A s = set([ComponentA]) assert not f(s) entity.add_component(ComponentA()) world._flush_component_updates() assert not f(entity) entity.remove_component(ComponentA) world._flush_component_updates() # B s = set([ComponentB]) assert not f(s) entity.add_component(ComponentB()) world._flush_component_updates() assert not f(entity) # A, B s = set([ComponentA, ComponentB]) assert f(s) entity.add_component(ComponentA()) world._flush_component_updates() assert f(entity) # A, C s = set([ComponentA, ComponentC]) assert f(s) entity.remove_component(ComponentB) entity.add_component(ComponentC()) world._flush_component_updates() assert f(entity) # C s = set([ComponentC]) assert not f(s) entity.remove_component(ComponentA) world._flush_component_updates() assert not f(entity)
def test_can_not_remove_nonexistent_component(entity): with pytest.raises(KeyError): entity.remove_component(Counter)