def add_healthy( self, healthy: int, entity_id: UUID, scene: Scene ) -> bool: """Добоаляем жизни""" if component := scene.get_component(entity_id, ComponentUnitHealthy): self._add_healthy(component=component, healthy=healthy) return True
def render(self, scene: Scene, render_entity_id: UUID, next_dialog: UUID) -> List[ComponentDialogEvent]: event_v: ComponentDialogEvent = ComponentDialogEvent( command="v", display_info="Выпить", system_type=SystemPotionY, kwargs={}, value=True, value_name="", entity_id=render_entity_id, component_type=ComponentPotion, next_dialog=next_dialog, ) event_q: ComponentDialogEvent = ComponentDialogEvent( command="q", display_info="Вернутся в инвентарь", system_type=SystemPotionY, kwargs={}, value=False, value_name="", entity_id=render_entity_id, component_type=ComponentPotion, next_dialog=next_dialog, ) scene.set_resource("dialog", render_entity_id) return [event_v, event_q]
def display_input(self, scene: Scene, component_dialog: ComponentDialog) -> bool: """Получени ввода""" try: input_text: str = input("Введите вариант: ").strip() except UnicodeDecodeError: return False events: List[ComponentDialogEvent] for component in component_dialog.dialog_events: if component.command == input_text: system: Optional[System] = scene.get_system( component.system_type) component_input: Optional[Component] = scene.get_component( entity_id=component.entity_id, component=component.component_type) value: Any = component.value value_name: str = component.value_name kwargs: Dict[str, Any] = component.kwargs entity_id: UUID = component.entity_id event_result: bool = self._event_system( system=system, component=component_input, value=value, value_name=value_name, kwargs=kwargs, scene=scene, entity_id=entity_id) return event_result return False
def enable_potion(self, scene: Scene, item_entity_id: UUID, component_inventory: ComponentInventory, component_potion: ComponentPotion, player: Entity) -> None: if system := scene.get_system(SystemUnitHealthy): system: SystemUnitHealthy system.add_healthy(healthy=component_potion.hp, entity_id=player.get_uuid(), scene=scene)
def render(self, scene: Scene, render_entity_id: UUID, next_dialog: UUID) -> List[ComponentDialogEvent]: event: ComponentDialogEvent = ComponentDialogEvent( command="i", display_info="Инвентарь", system_type=SystemInventory, value=None, value_name="", entity_id=render_entity_id, component_type=ComponentInventory, next_dialog=next_dialog, ) scene.set_resource("dialog", render_entity_id) return [event]
def _event_input(self, value: Tuple[int, int], scene: Scene, entity_id: UUID, value_name: str, component: ComponentPosition) -> bool: if value_name == "coordinate_x_y": system_word: Optional[SystemWord] = scene.get_system(SystemWord) if system_word is None: return False move: bool = system_word.set_move(entity_id=entity_id, component=component, move_x=value[0], move_y=value[1]) scene.set_status(GameStatus.runner) return move
def deprive_healthy( self, healthy: int, entity_id: UUID, scene: Scene ) -> bool: """Отымаем жизни""" type_components: Tuple[Type[Component], ...] = ( ComponentUnitHealthy ) if components := scene.get_components(entity_id, type_components): self._deprive_healthy( component_healthy=components[ComponentUnitHealthy], healthy=healthy, component_game_status=Component(), # TODO: Компонент убийства юнита ) return True
def render(self, scene: Scene, render_entity_id: UUID, next_dialog: UUID) -> List[ComponentDialogEvent]: system_word: Optional[SystemWord] = scene.get_system(SystemWord) position: Optional[ComponentPosition] = scene.get_component( entity_id=render_entity_id, component=ComponentPosition) if system_word is None or position is None: return [] events: List[ComponentDialogEvent] = self._render( system_word=system_word, position=position, entity_id=render_entity_id, next_dialog=next_dialog) scene.set_resource("dialog", next_dialog) return events
def _show_display(self, scene: Scene, entity_id: UUID) -> bool: """Отрисовка диалогов""" system_display: Optional[SystemDisplay] = scene.get_system(SystemDisplay) system_input: Optional[SystemInput] = scene.get_system(SystemInput) component_dialog: Optional[ComponentDialog] = scene.get_component( entity_id=entity_id, component=ComponentDialog) if system_display is None or system_input is None or component_dialog is None: return False self.clear() system_display.display_print(scene=scene, component_dialog=component_dialog) component_dialog = scene.get_component(entity_id=entity_id, component=ComponentDialog) system_input.display_input(scene=scene, component_dialog=component_dialog) return True
def create_item_potion(scene: Scene, hp: int = 100) -> Entity: entity: Entity = Entity() renderings: List[ComponentDialogRender] = [ ComponentDialogRender( system_type=SystemPotion, entity_id=entity.get_uuid(), component=ComponentDialog, next_dialog=entity.get_uuid() ) ] entity.add_component(ComponentItem( name=f"Зелье воставноления жизней {hp}", system_type=SystemPotion )) entity.add_component(ComponentDialog(renderings=renderings)) entity.add_component(ComponentPotion(hp=hp)) scene.add_entity(entity=entity) return entity
def new_entity_items(scene: Scene, player: Entity) -> UUID: entity: Entity = Entity( tags=["Items"] ) renderings: List[ComponentDialogRender] = [ ComponentDialogRender( system_type=SystemItem, entity_id=player.get_uuid(), component=ComponentInventory, next_dialog=player.get_uuid() ), ] dialog: ComponentDialog = ComponentDialog( renderings=renderings ) entity.add_component(dialog) scene.add_entity(entity=entity) return entity.get_uuid()
def event(self, scene: Scene, system_event: System, event_type: str, entity_id: UUID, component: Component, value: Any, value_name: str, kwargs: Dict[str, Any]) -> None: player_uuid: UUID = scene.get_resource("player") player: Entity = scene.get_entity(entity_id=player_uuid) component_inventory: Optional[ ComponentInventory] = player.get_component(ComponentInventory) if component_inventory is None: raise RuntimeError( "Не найден компонент инветаря при использование зелья") if isinstance(value, bool) and value: self.enable_potion(scene=scene, item_entity_id=entity_id, component_inventory=component_inventory, component_potion=component, player=player) scene.set_resource("dialog", component_inventory.entity_items)
def _get_dialogs( self, scene: Scene, component: ComponentDialog ) -> List[ComponentDialogEvent]: """Получение диалогов""" renderings: List[ComponentDialogEvent] = list() for render_iter in component.renderings: render_iter: ComponentDialogRender system: Optional[SystemRender] = scene.get_system( render_iter.system_type ) for render in system.render( scene=scene, render_entity_id=render_iter.entity_id, next_dialog=render_iter.next_dialog ): render: ComponentDialogEvent renderings.append(render) return renderings
def event(self, scene: Scene, system_event: System, event_type: str, entity_id: UUID, component: Component, value: Any, value_name: str, kwargs: Dict[str, Any]) -> None: component: ComponentInventory scene.set_resource("dialog", component.entity_items) scene.set_status(GameStatus.dialog)
def event(self, scene: Scene, system_event: System, event_type: str, entity_id: UUID, component: Component, value: Any, value_name: str, kwargs: Dict[str, Any]) -> None: if value is None: scene.set_resource("dialog", entity_id)
# https://ru.qaz.wiki/wiki/Data-driven_programming from src.core.scene import Scene if __name__ == '__main__': scene: Scene = Scene() scene.new_world() scene.run()
def render(self, scene: Scene, render_entity_id: UUID, next_dialog: UUID) -> List[ComponentDialogEvent]: inventory: Optional[ComponentInventory] = scene.get_component( entity_id=render_entity_id, component=ComponentInventory) if inventory is None: raise RuntimeError("Не найден компонен для рендеринга инвентаря") renders: List[ComponentDialogEvent] = list() q_event: ComponentDialogEvent = ComponentDialogEvent( command=f"q", display_info="Вернутся в меню", system_type=SystemDialog, value=None, value_name="", entity_id=render_entity_id, component_type=ComponentDialog, next_dialog=render_entity_id, info=False) if len(inventory.items) == 0: info_event: ComponentDialogEvent = ComponentDialogEvent( command=f"", display_info="В инвентаре нет предметов", system_type=type(None), value=None, value_name="", entity_id=render_entity_id, component_type=Component, next_dialog=render_entity_id, info=True) return [info_event, q_event] for index, item_uuid in enumerate(inventory.items): entity: Optional[Entity] = scene.get_entity(entity_id=item_uuid) if entity is None: continue component_item: Optional[ComponentItem] = entity.get_component( ComponentItem) if component_item is None: continue event: ComponentDialogEvent = ComponentDialogEvent( command=f"{index}", display_info=component_item.name, system_type=component_item.system_type, value=None, value_name="", entity_id=entity.get_uuid(), component_type=ComponentItem, next_dialog=next_dialog, ) renders.append(event) renders.append(q_event) scene.set_resource("dialog", render_entity_id) return renders
def _dialog(self, scene: Scene, entity_id: UUID) -> bool: """Генерация диалогов""" if component := scene.get_component(entity_id, ComponentDialog): component: ComponentDialog component.dialog_events = self._get_dialogs(scene, component) return True