From 8fee9d641189670b03f7160a3beea9bdb6450fd3 Mon Sep 17 00:00:00 2001 From: DoctorReid Date: Tue, 23 Jul 2024 10:33:46 +0800 Subject: [PATCH] =?UTF-8?q?#23=20=E5=8A=A0=E5=85=A5=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E4=BA=BA=E5=9F=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/zzz_od/context/battle_context.py | 20 +++++- src/zzz_od/game_data/__init__.py | 0 src/zzz_od/game_data/agent.py | 99 ++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/zzz_od/game_data/__init__.py create mode 100644 src/zzz_od/game_data/agent.py diff --git a/src/zzz_od/context/battle_context.py b/src/zzz_od/context/battle_context.py index 83e80834..20b2c5aa 100644 --- a/src/zzz_od/context/battle_context.py +++ b/src/zzz_od/context/battle_context.py @@ -1,10 +1,13 @@ import time +from cv2.typing import MatLike from enum import Enum +from typing import Optional from one_dragon.base.operation.context_event_bus import ContextEventBus from one_dragon.utils.log_utils import log from zzz_od.controller.zzz_pc_controller import ZPcController +from zzz_od.game_data.agent import AgentLoader class BattleEventEnum(Enum): @@ -18,9 +21,14 @@ class BattleEventEnum(Enum): class BattleContext: - def __init__(self, event_bus: ContextEventBus): + def __init__(self, event_bus: ContextEventBus, + controller: Optional[ZPcController] = None, + agent_loader: Optional[AgentLoader] = None): self.__event_bus: ContextEventBus = event_bus - self.controller: ZPcController = None + + # 以下都在 ZContext中赋值 在这里只是起一个声明作用 + self.controller: ZPcController = controller + self.agent_loader: AgentLoader = agent_loader def dodge(self): e = BattleEventEnum.BTN_DODGE.value @@ -51,3 +59,11 @@ def special_attack(self): log.info(e) self.controller.special_attack() self.__event_bus.dispatch_event(e, time.time()) + + def check_character(self, screen: MatLike): + """ + 识别当前画面的角色 + :param screen: + :return: + """ + pass diff --git a/src/zzz_od/game_data/__init__.py b/src/zzz_od/game_data/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/zzz_od/game_data/agent.py b/src/zzz_od/game_data/agent.py new file mode 100644 index 00000000..6cc98bc8 --- /dev/null +++ b/src/zzz_od/game_data/agent.py @@ -0,0 +1,99 @@ +import os +from enum import Enum +from functools import lru_cache +from typing import Optional, List + +from one_dragon.base.yaml_operator import YamlOperator +from one_dragon.utils import os_utils +from one_dragon.utils.i18_utils import gt + + +class AgentType: + + def __init__(self, cn: str): + """ + 代理人类型 + :param cn: + """ + self.cn: str = cn + + +class AgentTypeEnum(Enum): + + SUPPORT = AgentType('支援') + UNKNOWN = AgentType('未知') + + @classmethod + def from_name(cls, name): + if name in AgentTypeEnum.__members__: + return cls[name] + else: + return cls.UNKNOWN + + +class Agent(YamlOperator): + + def __init__(self, agent_id: str): + """ + 代理人 + """ + YamlOperator.__init__(self, get_agent_yml_path(agent_id)) + self.agent_id: str = agent_id # 代理人的英文名称 + self.agent_name: str = self.get('agent_name', '') # 代理人的中文名称 + agent_type_str = self.get('agent_type', '') + self.agent_type: AgentTypeEnum = AgentTypeEnum.UNKNOWN if agent_type_str not in AgentTypeEnum else AgentTypeEnum[agent_type_str] + + @property + def agent_type_str(self) -> str: + return gt(self.agent_type.value.cn) + + +class AgentLoader: + + def __init__(self): + self.agent_name_map: dict[str, Agent] = {} + self.load_all_agents() + + def get_agent(self, agent_name: str) -> Optional[Agent]: + """ + 根据中文名称获取 + :param agent_name: + :return: + """ + return self.agent_name_map.get(agent_name, None) + + def load_all_agents(self) -> List[Agent]: + """ + 加载所有代理人的信息 + :return: + """ + agent_list: List[Agent] = [] + root_dir_path = get_agent_root_dir_path() + agent_files = os.listdir(root_dir_path) + for agent_file_name in agent_files: + if not agent_file_name.endswith('yml'): + continue + agent = Agent(agent_file_name[:-4]) + agent_list.append(agent) + self.agent_name_map[agent.agent_name] = agent + + return agent_list + + +@lru_cache +def get_agent_root_dir_path() -> str: + """ + 代理人配置 + :return: + """ + return os_utils.get_path_under_work_dir('assets', 'game_data', 'agent') + + +@lru_cache +def get_agent_yml_path(agent_id: str) -> str: + """ + 获取代理人配置文件路径 + :param agent_id: + :return: + """ + return os.path.join(get_agent_root_dir_path(), f'{agent_id}.yml')