Difficult-Rocket/Difficult_Rocket/client/render/sr1_ship.py

81 lines
2.9 KiB
Python
Raw Normal View History

2022-12-11 10:39:05 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
2022-12-26 11:46:05 +08:00
from typing import List, TYPE_CHECKING, Union
from xml.etree import ElementTree
2022-12-11 10:39:05 +08:00
# third party package
2022-12-26 11:46:05 +08:00
from defusedxml.ElementTree import parse
2022-12-25 23:15:49 +08:00
# pyglet
from pyglet.graphics import Batch
from pyglet.resource import texture
2022-12-11 10:39:05 +08:00
# Difficult Rocket
2022-12-26 11:46:05 +08:00
from Difficult_Rocket import DR_option
2022-12-26 18:27:39 +08:00
from Difficult_Rocket.api.types.SR1 import SR1Textures, SR1PartTexture
2022-12-26 11:46:05 +08:00
from Difficult_Rocket.command.line import CommandText
from Difficult_Rocket.client.screen import BaseScreen
if TYPE_CHECKING:
from Difficult_Rocket.client import ClientWindow
2022-12-25 23:15:49 +08:00
class SR1ShipRender(BaseScreen):
"""用于渲染 sr1 船的类"""
2022-12-26 11:46:05 +08:00
def __init__(self,
main_window: "ClientWindow",
scale: float):
2022-12-25 23:15:49 +08:00
super().__init__(main_window)
self.scale = scale
2022-12-26 11:46:05 +08:00
self.textures: Union[SR1Textures, None] = None
self.xml_doc = parse('configs/dock1.xml')
self.xml_root: ElementTree.Element = self.xml_doc.getroot()
2022-12-25 23:15:49 +08:00
self.part_batch = Batch()
2022-12-26 11:46:05 +08:00
def load_textures(self):
self.textures = SR1Textures()
def render_ship(self):
if self.textures is None:
self.load_textures()
parts = self.xml_root.find('Parts')
for part in parts:
if part.tag != 'Part':
continue # 如果不是部件,则跳过
# print(f"tag: {part.tag} attrib: {part.attrib}")
part_id = part.attrib.get('id')
part_type = part.attrib.get('partType')
part_x = part.attrib.get('x')
part_y = part.attrib.get('y')
part_activate = part.attrib.get('activated') or 0
part_angle = part.attrib.get('angle')
part_angle_v = part.attrib.get('angleV')
part_editor_angle = part.attrib.get('editorAngle')
part_flip_x = part.attrib.get('flippedX') or 0
part_flip_y = part.attrib.get('flippedY') or 0
part_explode = part.attrib.get('exploded') or 0
2022-12-26 18:27:39 +08:00
if part_type not in SR1PartTexture.part_type_sprite:
2022-12-26 11:46:05 +08:00
print('Textures None found!')
print(f'id: {part_id:<4} type: {part_type:<10} x: {part_x} y: {part_y} activated: {part_activate} '
f'angle: {part_angle} angle_v: {part_angle_v} editor_angle: {part_editor_angle} '
2022-12-26 18:27:39 +08:00
f'flip_x: {part_flip_x} flip_y: {part_flip_y} explode: {part_explode} '
f'textures: {SR1PartTexture.get_sprite_from_type(part_type)}')
2022-12-26 11:46:05 +08:00
2022-12-25 23:15:49 +08:00
def on_draw(self):
...
2022-12-11 10:39:05 +08:00
2022-12-26 11:46:05 +08:00
def on_file_drop(self, x: int, y: int, paths: List[str]):
self.scale = DR_option.gui_scale
self.render_ship()
2022-12-25 23:15:49 +08:00
...
2022-12-26 11:46:05 +08:00
def on_command(self, command: CommandText):
if command.match('render'):
print('rua, render ship!')
self.render_ship()