def drop(element: Union[str, NPC], scenario: Scenario) -> None: ''' Function fired when the Player wants the Hero to drop something. ''' if not element: print('Which item would you like to drop?') Hero.declare_inventory() which_item = input('> ') __element: str = system_name(which_item) else: __element: str = system_name(element) this_element: Item = Hero.get_item_from_inventory(__element) if this_element: if this_element not in scenario.floor: scenario.add_to_floor(this_element) Hero.drop_item(this_element) else: equiped_item: Item = Hero.get_equiped_item(__element) if equiped_item: scenario.add_to_floor(equiped_item) Hero.drop_item(equiped_item) else: print('You can\'t drop items that you don\'t have.')
def eat(element: Food, scenario: Scenario): __element = system_name(element) if Hero.has_item(__element): this_item = Hero.get_item_from_inventory(__element) if type(this_item) == Food: print(f'You eat {system_name_with_article(this_item.name)}.') this_item.remove(1) if this_item.quantity == 0: print(f'You have no more {this_item.name}s left.') Hero.inventory.remove(this_item) elif any(system_name(__item.name).split()[-1] == __element for __item in scenario.elements) or \ any(system_name(__something.name).split()[-1] == __element for __something in scenario.floor): print_cinematics('You can only eat things that you have with you.') elif any( system_name(__something.name).startswith('body') for __something in scenario.floor) and 'body' in __element: for __something in scenario.floor: if element.startswith('body') and system_name( __something.name).startswith('body'): print( f'You would need to be really starving to death to even think about eating the {__something.name}.' ) # print( # f'The {__something.name} laying on the floor {__something.verb} {__something.description}.') # if type(__something) == NPC: # looking_body(__something, scenario) else: print_cinematics(f'You can\'t eat that.')
def search(element: str, scenario: Scenario) -> None: ''' It is called when the Hero searches somewhere. ''' __element = system_name(element) if any(system_name(__item).split()[-1] == __element for __item in scenario.ambient): print_cinematics(f'You search the {element} but you find nothing.') elif any(system_name(__item).split()[-1] == __element for __item in scenario.far_away): print_cinematics(f'It is too far away.') elif any(system_name(__item.name).split()[-1] == __element for __item in scenario.elements): searching_element = scenario.get_element(element) searching_element.on_searching() elif any(system_name(__item.name).startswith('body') for __item in Hero.inventory) and \ element.startswith('body'): print_cinematics( f'You can\'t search the body while you are carrying it.') elif any(system_name(__item.name).split()[-1] == __element for __item in Hero.inventory): __item_from_inv: Item = Hero.get_item_from_inventory(element) __item_from_inv.on_searching() elif any(system_name(__something.name).split()[-1] == __element for __something in scenario.floor) or \ any(system_name(__something.name).startswith('body') for __something in scenario.floor) and 'body' in __element: for __something in scenario.floor: if element.startswith('body') and system_name(__something.name).startswith('body'): in_inventory = __something.declare_inventory() for __item in __something.inventory: scenario.add_to_floor(__item) __something.inventory.remove(__item) searching_body(in_inventory, __something, scenario) elif system_name(__something.name).split()[-1] == __element: __something.on_searching() else: print_cinematics( f'There is nothing to be found.')
def get_element(self, element: str) -> Element: for __element in self.elements: if system_name(__element.name).endswith(system_name(element)) or \ __element.name.endswith('body') and element.startswith('body'): return __element
def take(element: str, scenario: Scenario) -> None: ''' Function fired when the Player wants the Hero to take something from the Scenario. ''' Hero.update_weigth() element = system_name(element) if any(__item.endswith(element) for __item in scenario.ambient): print('There\'s no point in doing it.') elif any(__item.endswith(element) for __item in scenario.far_away): print('Even if you could take it, it is too far away.') elif any(system_name(__item.name).endswith(element) for __item in scenario.floor) or \ element.startswith('body'): for __item in scenario.floor: __item: Union[Item, NPC] if system_name(__item.name).startswith('body') and system_name(element).startswith('body') or \ system_name(__item.name).endswith(element) and system_name(__item.name).startswith('body'): __item.update_weigth() if Hero.carrying_weigth + __item.weight > Hero.weigth_capacity: print('It is too much for you to carry.') else: moving_body(__item, scenario) Hero.take_item(__item) scenario.floor.remove(__item) elif system_name(__item.name).endswith(element): if Hero.carrying_weigth + __item.weight > Hero.weigth_capacity: print('It is too much for you to carry.') else: if Hero.has_item(__item): owned_item = Hero.get_item_from_inventory(__item) owned_item.change_quantity(__item.quantity) Item_name_in_singular = owned_item.name[:-1] print( f'You get another {Item_name_in_singular}.') else: if hasattr(__item, 'on_taking') and __item.on_taking() == 'keep': __item = copy.copy(__item) else: scenario.floor.remove(__item) Hero.take_item(__item) elif any(system_name(__item.name).endswith(element) for __item in scenario.elements): this_element: Element = scenario.get_element(element) if type(this_element) == Container: print('You cannot take it.') elif Hero.carrying_weigth + this_element.weight > Hero.weigth_capacity: print('It is too much for you to carry.') elif Hero.has_item(this_element): owned_item = Hero.get_item_from_inventory(this_element) owned_item.add(this_element.quantity) Item_name_in_singular = owned_item.name[:-1] print( f'You get another {Item_name_in_singular}.') else: Hero.take_item(this_element) if not hasattr(this_element, 'on_taking') or this_element.on_taking() != 'keep': scenario.elements.remove(this_element) elif element.endswith('armor') and \ any(type(__thing) == NPC and __thing.armor.container != None for __thing in scenario.floor): for __thing in scenario.floor: if type(__thing) == NPC: if __thing.armor != None: __thing.armor.on_taking() __thing.armor.container = None Hero.take_item(__thing.armor) __thing.armor = None else: print(f'You don\'t see any {element} nearby to take it.')
def look(element: str, scenario: Scenario) -> None: ''' It is called when the Hero looks at something. ''' __element = system_name(element) if __element == 'me': print_cinematics(f'You are {Hero.appearance}.') elif __element == 'floor': if len(scenario.floor) == 0: print('There\'s nothing special on the floor.') else: message = 'Looking at the floor you see' if len(scenario.floor) == 1: print_cinematics( f'{message} {scenario.floor[0].article[1]}{scenario.floor[0].name}.') else: print_cinematics( f'{message} {", ".join(f"{item.article[1]}{item.name}" for item in scenario.floor[: -1])} and {f"{scenario.floor[-1].article[0]}{scenario.floor[-1].name}"}.') elif __element == '' or __element == 'around': print_cinematics( f'You look around and you see {scenario.description}.') # elements derivated from the Class Item can be accessed by the final word of their names (ej. "sword" matches for "short sword") elif any(system_name(__item).split()[-1] == __element for __item in scenario.ambient): print_cinematics( f'You see nothing special about the {element}.') elif any(system_name(__item).split()[-1] == __element for __item in scenario.far_away): print_cinematics( f'You look at the {element}, but is too far away to see any details.') elif any(system_name(__item.name).split()[-1] == __element for __item in scenario.elements): looking_element = scenario.get_element(system_name(element)) print_cinematics( f'The {looking_element.name} {looking_element.verb} {looking_element.description}.') looking_element.on_looking() elif Hero.has_item(__element): this_item = Hero.get_item_from_inventory(__element) enough_for = '' if type(this_item) == Food: # I consider that the weight of food is enough for a full day, so multiplying quantity by the food weight returns for how many days one portion of this food is capable of sustaining the Hero. enough_for = f', enough for {int(this_item.quantity * this_item.unity_weight)} days' print( f'The {this_item.name} on your inventory {this_item.verb} {this_item.description}{enough_for}.') elif any(system_name(__something.name).split()[-1] == __element for __something in scenario.floor) or \ any(system_name(__something.name).startswith('body') for __something in scenario.floor) and 'body' in __element: for __something in scenario.floor: if element.startswith('body') and system_name(__something.name).startswith('body') or \ system_name(__something.name).endswith(__element): print( f'The {__something.name} laying on the floor {__something.verb} {__something.description}.') if type(__something) == NPC: looking_body(__something, scenario) else: print_cinematics( f'There is nothing to be seen.')