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_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 can_not_take_untakeable_item_exception(world, room): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(throw_exc=True), 1) item = world.create_entity( RoomPresence(room=room._uid), ) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), TakeAction(item=item._uid), ) with pytest.raises(NotTakeable): world.update()
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_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_is_not_in_inventory(world, room, item): actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), ) world._flush_component_updates() assert not is_in_inventory(item, actor)
def can_not_take_untakeable_item(world, room): world.add_system(PerceiveRoom(), 0) world.add_system(TakeOrDrop(), 1) world.add_system(PerceiveRoom(), 2, add_duplicates=True) item = world.create_entity( RoomPresence(room=room._uid), ) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(), TakeAction(item=item._uid), ) world.update() assert actor.get_component(Inventory).contents == [] assert not actor.has_component(TakeAction) assert item.has_component(RoomPresence)
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_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_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_is_in_inventory(world, room): item = world.create_entity( Takeable(), ) actor = world.create_entity( RoomPresence(room=room._uid), Inventory(contents=[item._uid]), ) world._flush_component_updates() assert is_in_inventory(item, actor)
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_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_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_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 unequip(slot, target, entity, world): slot_cmpt = slot.get_component(Slot) item_uid = slot_cmpt.content if target.has_component(Room): slot_cmpt.content = None item = world.get_entity(item_uid) item.add_component(RoomPresence(room=target._uid)) elif target.has_component(Inventory): inventory = target.get_component(Inventory) slot_cmpt.content = None inventory.contents.append(item_uid) else: print("Unequipping failed.")
def test_drop_item(world, room): 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( RoomPresence(room=room._uid), Inventory(contents=[item._uid]), DropAction(item=item._uid), ) world.update() assert actor.get_component(Inventory).contents == [] assert not actor.has_component(DropAction) assert item.has_component(RoomPresence) assert item.get_component(RoomPresence).room == actor.get_component(RoomPresence).room
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 drop(item, entity): room_uid = entity.get_component(RoomPresence).room inventory = entity.get_component(Inventory).contents idx = inventory.index(item._uid) del inventory[idx] item.add_component(RoomPresence(room=room_uid))
def item(world, room): return world.create_entity( RoomPresence(room=room._uid), Takeable(), )