From 5406e7f5c769c41510485a280fe7bf93c8615774 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 21 Jul 2023 18:33:15 +0800 Subject: [PATCH] dr rs compat --- docs/src/change_log/dr_game.md | 7 +++ mods/dr_game/Difficult_Rocket_rs/__init__.py | 45 +++++++++++++++----- mods/dr_game/__init__.py | 2 +- mods/dr_game/sr1_ship.py | 34 +++++++++------ mods/dr_game/types.py | 17 +++++--- 5 files changed, 75 insertions(+), 30 deletions(-) diff --git a/docs/src/change_log/dr_game.md b/docs/src/change_log/dr_game.md index ad91002..abc7981 100644 --- a/docs/src/change_log/dr_game.md +++ b/docs/src/change_log/dr_game.md @@ -7,6 +7,13 @@ ## 20230721 DR rs 0.2.17.0 +### Add + +- 导出了 export + - `PySR1PartData` + - `PySaveStatus` + - `map_ptype_textures(part_type: str) -> str` + ### BreakingChanges - `SR1Ship_rs` diff --git a/mods/dr_game/Difficult_Rocket_rs/__init__.py b/mods/dr_game/Difficult_Rocket_rs/__init__.py index 9a61618..5c431a3 100644 --- a/mods/dr_game/Difficult_Rocket_rs/__init__.py +++ b/mods/dr_game/Difficult_Rocket_rs/__init__.py @@ -15,15 +15,37 @@ if TYPE_CHECKING: py_obj.draw() return True - def get_version_str() -> str: ... + def get_version_str() -> str: + """ + 获取版本号 + :return: 版本号 + """ - def part_list_read_test(file_name: Optional[str] = "./assets/builtin/PartList.xml") -> None: ... + def part_list_read_test(file_name: Optional[str] = "./assets/builtin/PartList.xml") -> None: + """ + PartList 读取测试 + :param file_name: + :return: + """ - def read_ship_test(path: Optional[str] = "./assets/builtin/dock1.xml") -> None: ... + def read_ship_test(path: Optional[str] = "./assets/builtin/dock1.xml") -> None: + """ + 飞船存档读取测试 + :param path: + :return: + """ + + def map_ptype_textures(part_type: str) -> str: + """ + 获取零件的贴图 (写死的) + :param part_type: + :return: + """ - class SR1PartType_rs: - """ 用于从 rust 中读取 SR1PartType + class SR1PartType_rs: # NOQA + """ + 用于从 rust 中读取 SR1PartType 不能从 Python 端创建 自带一些可从 Python 端读取的属性 (实际上就是一个接口) """ @@ -50,7 +72,7 @@ if TYPE_CHECKING: """ 零件是否隐藏 """ - class SR1PartList_rs: + class SR1PartList_rs: # NOQA """ 用于从 rust 中读取 SR1PartList """ def __init__(self, file_name: Optional[str] = "./assets/builtin/PartList.xml", list_name: Optional[str] = 'NewPartList'): ... @@ -59,8 +81,9 @@ if TYPE_CHECKING: def get_part_type(self, name: str) -> SR1PartType_rs: ... - class SR1PartData_rs: - """ 用于从 rust 中读取 SR1PartData (其实好像也没啥用哈) + class SR1PartData_rs: # NOQA + """ + 用于从 rust 中读取 SR1PartData (其实好像也没啥用哈) """ @property def id(self) -> int: ... @@ -85,10 +108,10 @@ if TYPE_CHECKING: @property def flip_y(self) -> bool: ... - class SaveStatus_rs: + class SaveStatus_rs: # NOQA def __init__(self, save_default: Optional[bool] = False) -> None: ... - class SR1Ship_rs: + class SR1Ship_rs: # NOQA """ 用于高效且省内存的读取 SR1Ship """ def __init__(self, file_path: Optional[str] = './assets/builtin/dock1.xml', @@ -113,7 +136,7 @@ if TYPE_CHECKING: """用于返回一个包含所有已连接零件的字典""" def save(self, file_path: str, save_status: Optional[SaveStatus_rs] = None) -> None: ... - class Console_rs: + class Console_rs: # NOQA def __init__(self) -> None: ... def start(self) -> None: ... def stop(self) -> bool: ... diff --git a/mods/dr_game/__init__.py b/mods/dr_game/__init__.py index f785825..d2eafcd 100644 --- a/mods/dr_game/__init__.py +++ b/mods/dr_game/__init__.py @@ -16,7 +16,7 @@ from Difficult_Rocket.api.mod import ModInfo from Difficult_Rocket.client import ClientWindow from Difficult_Rocket.api.types import Options, Version -DR_rust_version = Version("0.2.17.0") # DR_mod 的 Rust 编写部分的兼容版本 +DR_rust_version = Version("0.2.18.0") # DR_mod 的 Rust 编写部分的兼容版本 logger = logging.getLogger('client.dr_game') diff --git a/mods/dr_game/sr1_ship.py b/mods/dr_game/sr1_ship.py index afe5a65..f7943b5 100644 --- a/mods/dr_game/sr1_ship.py +++ b/mods/dr_game/sr1_ship.py @@ -40,7 +40,10 @@ if TYPE_CHECKING: if DR_mod_runtime.use_DR_rust: from .Difficult_Rocket_rs import (SR1PartList_rs, - SR1Ship_rs) + SR1Ship_rs, + SR1PartData_rs, + SR1PartType_rs, + map_ptype_textures) logger = logging.getLogger('client.dr_game_sr1_ship') logger.level = logging.DEBUG @@ -117,8 +120,8 @@ class SR1ShipRender(BaseScreen): self.camera = CenterCamera(main_window, min_zoom=(1 / 2) ** 10, max_zoom=10) # Optional data + self.textures: SR1Textures = SR1Textures() self.gen_draw: Optional[Generator] = None - self.textures: Union[SR1Textures, None] = None self.xml_name: Optional[str] = None # 准备移除, 更换为基于 rust 的 xml 解析 self.xml_doc: Optional[ElementTree] = None # 准备移除, 更换为基于 rust 的 xml 解析 self.xml_root: Optional[Element] = None # 准备移除, 更换为基于 rust 的 xml 解析 @@ -163,13 +166,6 @@ class SR1ShipRender(BaseScreen): print(e) return False - def load_textures(self): - """ - 初始化纹理加载 - :return: - """ - self.textures = SR1Textures() - def gen_sprite(self, part_datas: Dict[int, SR1PartData], each_count: int = 100) -> Generator: """ 生成 sprite @@ -181,8 +177,24 @@ class SR1ShipRender(BaseScreen): count = 0 self.drawing = True # rust 渲染 + if DR_mod_runtime.use_DR_rust: + for p_id, parts in self.rust_ship.as_dict().items(): + p_id: int + parts: List[Tuple[SR1PartType_rs, SR1PartData_rs]] + part_group = Group(20, parent=self.part_group) + batch = [] + for p_type, p_data in parts: + part_sprite = Sprite(img=self.textures.get_texture(map_ptype_textures(p_data.part_type_id)), + x=p_data.x * 60, y=p_data.y * 60, z=random.random(), + batch=self.main_batch, group=part_group) + + batch.append(part_sprite) + + count += 1 + if count >= each_count: + count = 0 + yield - # python 渲染 for part_id, part in part_datas.items(): # 下面就是调用 pyglet 去渲染的部分 @@ -252,8 +264,6 @@ class SR1ShipRender(BaseScreen): """ 渲染船 """ - if self.textures is None: - self.load_textures() logger.info(sr_tr().sr1.ship.ship.load().format(self.xml_name)) start_time = time.perf_counter_ns() self.part_data: Dict[int, SR1PartData] = {} diff --git a/mods/dr_game/types.py b/mods/dr_game/types.py index e40e2aa..966b62c 100644 --- a/mods/dr_game/types.py +++ b/mods/dr_game/types.py @@ -33,10 +33,8 @@ class SR1PartData: class SR1Textures(Options): """ 存储 sr1 的材质 img """ - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.flush_option() - for image_name in self.cached_options: + def load_file(self, **kwargs): + for image_name in self.flush_option(): img = load(f'assets/textures/parts/{image_name}.png') img.anchor_x = img.width // 2 img.anchor_y = img.height // 2 @@ -49,8 +47,10 @@ class SR1Textures(Options): :param name: :return: """ - assert name in self.cached_options - return self.cached_options.get(name) + if name in self.cached_options: + return self.cached_options.get(name) + else: + return None Battery: AbstractImage = None Beam: AbstractImage = None @@ -134,6 +134,11 @@ class SR1Rotation(Options): @classmethod def get_rotation(cls, radian: float) -> float: + """ + 实际上就是将弧度转换为角度 (同时自带一个映射表) + :param radian: + :return: + """ if radian in cls.radian_angle_map: return cls.radian_angle_map[radian] else: