def test_change_room(world): world.add_system(ChangeRoom(), 0) world.add_system(PerceiveRoom(), 1) room = world.create_entity() other_room = world.create_entity() room.add_component(Room(adjacent=[other_room._uid]), ) other_room.add_component(Room(adjacent=[room._uid]), ) actor = world.create_entity(RoomPresence(room=room._uid, ), ) world.update() actor.add_component(ChangeRoomAction(room=other_room._uid, ), ) world.update() room_cmpt = room.get_component(Room) assert len(room_cmpt.presences) == 0 assert len(room_cmpt.arrived) == 0 assert len(room_cmpt.continued) == 0 assert len(room_cmpt.gone) == 1 assert room_cmpt.gone[0] == actor._uid other_room_cmpt = other_room.get_component(Room) assert len(other_room_cmpt.presences) == 1 assert other_room_cmpt.presences[0] == actor._uid assert len(other_room_cmpt.arrived) == 1 assert other_room_cmpt.arrived[0] == actor._uid assert len(other_room_cmpt.continued) == 0 assert len(other_room_cmpt.gone) == 0 presence_cmpt = actor.get_component(RoomPresence) assert presence_cmpt.room == other_room._uid assert len(presence_cmpt.presences) == 1 assert presence_cmpt.presences[0] == actor._uid
def test_proxying_system__field_lookup(world): token = '123' global token_out token_out = None @Component() class TestComponent: foo: str = None class BareTypeProxy(System): entity_filters = { 'test': Proxy('proxy'), } proxies = { 'proxy': ProxyType(TestComponent, 'foo'), } def update(self, entity_by_filters): for entity in entity_by_filters['test']: proxy = self.proxies['proxy'] # test = entity[proxy.component_type] token = proxy.field(entity) global token_out token_out = token system = BareTypeProxy() world.add_system(system, 0) entity = world.create_entity(TestComponent(foo=token)) world.update() assert token == token_out
def test_creation(world): world.add_system(PerceiveRoom(), 0) room = world.create_entity(Room(), ) actor = world.create_entity(RoomPresence(room=room._uid, ), ) world.update() room_cmpt = room.get_component(Room) assert len(room_cmpt.presences) == 1 assert room_cmpt.presences[0] == actor._uid assert len(room_cmpt.arrived) == 1 assert room_cmpt.arrived[0] == actor._uid assert len(room_cmpt.continued) == 0 assert len(room_cmpt.gone) == 0 presence_cmpt = actor.get_component(RoomPresence) assert len(presence_cmpt.presences) == 1 assert presence_cmpt.presences[0] == actor._uid world.update() room_cmpt = room.get_component(Room) assert len(room_cmpt.presences) == 1 assert room_cmpt.presences[0] == actor._uid assert len(room_cmpt.arrived) == 0 assert len(room_cmpt.continued) == 1 assert room_cmpt.continued[0] == actor._uid assert len(room_cmpt.gone) == 0 presence_cmpt = actor.get_component(RoomPresence) assert len(presence_cmpt.presences) == 1 assert presence_cmpt.presences[0] == actor._uid
def test_is_in_room(world): world.add_system(PerceiveRoom(), 0) # A room, and an actor and an item in the roomless void. room = world.create_entity(Room(), ) actor = world.create_entity() item = world.create_entity() assert not is_in_room(item, actor) with pytest.raises(EntityNotInARoom): is_in_room(item, actor, throw_exc=True) # Now the actor is in the room. actor.add_component(RoomPresence(room=room._uid, ), ) world.update() assert not is_in_room(item, actor) with pytest.raises(ItemNotInARoom): is_in_room(item, actor, throw_exc=True) # And now the item is there, too. item.add_component(RoomPresence(room=room._uid, ), ) world.update() assert is_in_room(item, actor)
def test_addition_to_system__entity_first(world, null_system): entity = world.create_entity(NullComponent()) world._flush_component_updates() world.add_system(null_system, 0) assert entity in null_system.entities['null'] assert len(null_system.entries) == 1 assert null_system.entries[0] == (['null'], entity)
def test_mutual_perception(world): world.add_system(ChangeRoom(), 0) world.add_system(PerceiveRoom(), 1) # Two connected rooms, an actor in each room = world.create_entity() other_room = world.create_entity() room.add_component(Room(adjacent=[other_room._uid], ), ) other_room.add_component(Room(adjacent=[room._uid], ), ) actor = world.create_entity(RoomPresence(room=room._uid, ), ) other_actor = world.create_entity(RoomPresence(room=other_room._uid, ), ) world.update() # They only see themselves presence_cmpt = actor.get_component(RoomPresence) assert len(presence_cmpt.presences) == 1 assert presence_cmpt.presences[0] == actor._uid other_presence_cmpt = actor.get_component(RoomPresence) assert len(other_presence_cmpt.presences) == 1 assert other_presence_cmpt.presences[0] == actor._uid # Now let one actor change rooms actor.add_component(ChangeRoomAction(room=other_room._uid, ), ) world.update() # Now they see each other. presence_cmpt = actor.get_component(RoomPresence) assert len(presence_cmpt.presences) == 2 assert actor._uid in presence_cmpt.presences assert other_actor._uid in presence_cmpt.presences other_presence_cmpt = actor.get_component(RoomPresence) assert len(other_presence_cmpt.presences) == 2 assert actor._uid in other_presence_cmpt.presences assert other_actor._uid in other_presence_cmpt.presences
def test_clock_max_timestep(world, entity, clock): world.add_system(DetermineTimestep(), sort=0) dt = 0.1 assert dt > entity[Clock].max_timestep clock.set(dt) world.update() assert entity[Clock].timestep == entity[Clock].max_timestep
def test_update_world_when_system_was_added_first(world, entity, component, system): world.add_system(system, 0) entity.add_component(component) assert component.count == 0 world.update() assert component.count == 1
def test_basic_clock(world): world.add_system(DetermineTimestep(), sort=0) dt = 0.01 clock = SettableClock(dt) entity = world.create_entity(Clock(clock=clock)) assert dt < entity[Clock].max_timestep world.update() assert entity[Clock].timestep == dt
def test_clock_scaling(world, entity, clock): world.add_system(DetermineTimestep(), sort=0) dt = 0.01 factor = 0.5 clock.set(dt) entity[Clock].scaling_factor = factor world.update() assert entity[Clock].game_time == dt * factor
def test_can_not_drop_item_that_actor_does_not_have_exception(world, room, item): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), DropAction(item=item._uid), ) with pytest.raises(ItemNotInInventory): world.update()
def test_can_not_take_nonexistant_item_exception(world, room): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), TakeAction(item=UID()), ) with pytest.raises(NoSuchUID): world.update()
def test_remove_system(world, null_system): entity = world.create_entity(NullComponent()) world._flush_component_updates() world.add_system(null_system, 0) world.remove_system(NullSystem) assert null_system.entities['null'] == set() assert len(null_system.exits) == 1 assert null_system.entries[0] == (['null'], entity) assert world.systems == {} assert not world.has_system(NullSystem)
def can_not_drop_untakeable_item_exception(world, room): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) item = world.create_entity() actor = world.create_entity( RoomPresence(room=room._uid), Inventory(contents=[item._uid]), DropAction(item=item._uid), ) with pytest.raises(NotTakeable): world.update()
def test_system_handling(world, system): assert len(world.get_systems()) == 0 assert not world.has_system(IncreaseCount) world.add_system(system, 0) assert world.has_system(IncreaseCount) assert world.get_system(IncreaseCount) is system assert len(world.get_systems()) == 1 world.remove_system(IncreaseCount) assert not world.has_system(IncreaseCount) assert len(world.get_systems()) == 0
def test_can_not_take_from_the_void_exception(world): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) item = world.create_entity( Takeable(), ) actor = world.create_entity( Inventory(contents=[item._uid]), DropAction(item=item._uid), ) with pytest.raises(ActorNotInRoom): world.update()
def test_proxying_system__proxy_type(world): class NonBareTypeProxy(ProxyingNullSystem): proxies = { 'null_proxy': ProxyType(NullComponent), } system = NonBareTypeProxy() world.add_system(system, 0) entity = world.create_entity(NullComponent()) world._flush_component_updates() assert entity in system.entities["null"] assert len(system.entries) == 1 assert system.entries[0] == (['null'], entity)
def test_cant_change_to_current_room(world): # This test and the next are more of an informative nature. Since # a room is usually not adjacent to itself, you can't change from # it to it. world.add_system(ChangeRoom(throw_exc=True), 0) world.add_system(PerceiveRoom(), 1) room = world.create_entity(Room(), ) actor = world.create_entity( RoomPresence(room=room._uid, ), ChangeRoomAction(room=room._uid, ), ) with pytest.raises(RoomsNotAdjacent): world.update()
def test_cant_change_to_non_adjacent_room(world): world.add_system(ChangeRoom(throw_exc=True), 0) world.add_system(PerceiveRoom(), 1) # Two connected rooms, an actor in each room = world.create_entity(Room(), ) other_room = world.create_entity(Room(), ) actor = world.create_entity( RoomPresence(room=room._uid, ), ChangeRoomAction(room=other_room._uid, ), ) with pytest.raises(RoomsNotAdjacent): world.update()
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_clock_cascade(world, entity, clock): world.add_system(DetermineTimestep(), sort=0) dt = 0.01 clock.set(dt) # Child clock factor = 0.5 child = world.create_entity( Clock( parent=entity._uid, scaling_factor=factor, ), ) world.update() assert child[Clock].frame_time == dt assert child[Clock].game_time == dt * factor
def test_init_component(world, entity, component, system, enabler): world.add_system(system, 0) assert system.init_called == 0 assert system.init_done == 0 assert not component.inited entity.add_component(component) world.flush_component_updates() assert system.init_called == 1 assert system.init_done == 0 assert not component.inited entity.add_component(enabler) world.flush_component_updates() assert system.init_called == 2 assert system.init_done == 1 assert component.inited
def test_can_change_to_current_room(world): # But we *can* make rooms circular in nature. # I don't know what the point of this is supposed to be. Maybe # someone in the future will have a use case for this. world.add_system(ChangeRoom(throw_exc=True), 0) world.add_system(PerceiveRoom(), 1) room = world.create_entity() room.add_component(Room(adjacent=[room._uid], ), ) actor = world.create_entity(RoomPresence(room=room._uid, ), ) # Let's update to have a clean room state. world.update() actor.add_component(ChangeRoomAction(room=room._uid, ), ) world.update() room_cmpt = room.get_component(Room) assert len(room_cmpt.presences) == 1 assert actor._uid in room_cmpt.presences assert len(room_cmpt.arrived) == 0 assert len(room_cmpt.continued) == 1 assert actor._uid in room_cmpt.continued assert len(room_cmpt.gone) == 0
def test_can_not_take_without_an_inventory_exception(world, room, item): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) actor = world.create_entity( RoomPresence(room=room._uid), TakeAction(item=item._uid), ) with pytest.raises(ActorHasNoInventory): world.update()
def test_can_not_take_without_an_inventory(world, room, item): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) actor = world.create_entity( RoomPresence(room=room._uid), TakeAction(item=item._uid), ) world.update() assert not actor.has_component(TakeAction) assert item.has_component(RoomPresence)
def test_can_not_take_nonexistant_item(world, room): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), TakeAction(item=UID()), ) world.update() assert actor.get_component(Inventory).contents == [] assert not actor.has_component(TakeAction)
def test_can_not_take_item_from_other_room_exception(world, room, item): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) other_room = world.create_entity( Room(), ) actor = world.create_entity( RoomPresence(room=other_room._uid), Inventory(), TakeAction(item=item._uid), ) with pytest.raises(ItemNotInRoom): world.update()
def test_can_not_drop_item_that_actor_does_not_have(world, room, item): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), DropAction(item=item._uid), ) world.update() assert actor.get_component(Inventory).contents == [] assert not actor.has_component(DropAction) assert item.has_component(RoomPresence)
def test_can_not_take_from_the_void(world): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) item = world.create_entity( Takeable(), ) actor = world.create_entity( Inventory(), TakeAction(item=item._uid), ) world.update() assert actor.get_component(Inventory).contents == [] assert not actor.has_component(TakeAction)