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 looking_body(body: NPC, scenario: Scenario) -> None: ''' It is called specifically when the Hero looks to an enemy dead body. Handles the weapons, shield and armor used by the deceased. ''' message = [] if body.armor: message.append( f'it is wearing {body.armor.article[0]}{body.armor.name}') body.armor.container = body.name if body.weapon: message.append( f'{body.weapon.article[0]}{body.weapon.name}') scenario.add_to_floor(body.weapon) body.weapon = None if body.shield: message.append(f'{body.shield.article[0]}{body.shield.name}.') scenario.add_to_floor(body.shield) body.shield = None if len(message) == 0: return elif len(message) == 1: print_cinematics(f'You see {message[0]}.') else: print_cinematics( f'You see {", ".join(message[:-1])} and {message[-1]}.')
def searching_body(in_inventory: str, body: NPC, scenario: Scenario) -> None: ''' It is called specifically when the Hero searches an enemy dead body. Handles the weapons, armor and shield used by the deceased. ''' message = [] if body.armor: message.append( f'it is wearing {body.armor.article[0]}{body.armor.name}') body.armor.container = body.name if body.weapon: message.append( f'{body.weapon.article[0]}{body.weapon.name}') scenario.add_to_floor(body.weapon) body.weapon = None if body.shield: message.append(f'{body.shield.article[0]}{body.shield.name}.') scenario.add_to_floor(body.shield) body.shield = None if len(message) == 0: print_cinematics(in_inventory) elif len(message) == 1: print_cinematics(f'{in_inventory} You also see {message[0]}.') else: print_cinematics( f'{in_inventory}\n\t\tYou also see {", ".join(message[:-1])} and {message[-1]}.')
def moving_body(body: NPC, scenario: Scenario) -> None: droped_items = body.drop_item() Hero.appearance = 'all bloodstained' if len(droped_items) > 0: print( f'When you move the body, its \ {" and ".join(item.name for item in droped_items)} \ fall{"s" if len(droped_items) == 1 else ""} on the ground.') for item in droped_items: scenario.add_to_floor(item)
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.')
import cinematics from core_elements.scenario import Scenario from core_elements.elements import Item, Container import gameplay from db.enemies import ugly_monster from core_elements import * from db.food import * from db.valuables import * from core_elements.characters.Hero import Hero # A TESTING SCENARIO # This scenario is just for testing purposes # CREATES A TESTING SCENARIO INSTANCE AND ADDS ITS ATTRIBUTES testing_forest = Scenario('testing_forest', 'scene01', 'forest') gameplay.CURRENT_SCENARIO = testing_forest testing_forest.description = 'a vast green field full of trees. \ Near you there are some bushes and far away you can see a river flowing from the left \ to the right. The trail you\'ve been following seems to go on until a bridge on the river' # ELEMENTS THAT CAN BE LIMITEDLY INTERACTED WITH. testing_forest.ambient = ['trail', 'field'] testing_forest.far_away = ['river', 'old bridge'] # ELEMENTS THAT CAN BE USED AS SAFE PLACES FOT THE HERO TO HIDE AND REST. testing_forest.safe_places = ['bushes'] # EXIT POINTS THAT CONNECT TO OTHER SCENARIOS. # GLOBAL CONTAINERS FROM THE SCENARIO THAT CAN BE IMMEDIATELLY INTERACTED WITH. trees = Container('trees', 'big and very green apple trees')
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.')