feat: sr1 render

This commit is contained in:
shenjack 2022-12-29 10:13:20 +08:00
parent 3f8c33852d
commit ab279cb557
3 changed files with 80 additions and 42 deletions

View File

@ -5,6 +5,7 @@
# -------------------------------
from typing import Dict, Union
from dataclasses import dataclass
# pyglet
# import pyglet
@ -14,6 +15,21 @@ from pyglet.image import load, AbstractImage
from Difficult_Rocket.utils.typings import Options
@dataclass
class SR1PartData:
x: float
y: float
id: int
type: str
angle: float
angle_v: float
editor_angle: int
flip_x: bool
flip_y: bool
explode: bool
textures: str
class SR1Textures(Options):
""" 存储 sr1 的材质 img """
def __init__(self, **kwargs):
@ -23,6 +39,10 @@ class SR1Textures(Options):
setattr(self, image_name, load(f'textures/parts/{image_name}.png'))
self.flush_option()
def get_texture(self, name: str):
assert name in self.cached_options
return self.cached_options.get(name)
Battery: AbstractImage = None
Beam: AbstractImage = None
CoverBottom: AbstractImage = None
@ -62,33 +82,33 @@ class SR1Textures(Options):
class SR1PartTexture:
part_type_sprite: Dict[str, str] = {'pod-1 ': 'Pod',
'detacher-1 ': 'DetacherVertical',
'detacher-2 ': 'DetacherRadial',
'wheel-1 ': 'Wheel',
'wheel-2 ': 'Wheel',
'fuselage-1 ': 'Fuselage',
'strut-1 ': 'Beam',
'fueltank-0 ': 'TankTiny',
'fueltank-1 ': 'TankSmall',
'fueltank-2 ': 'TankMedium',
'fueltank-3 ': 'TankLarge',
'fueltank-4 ': 'Puffy750',
'fueltank-5 ': 'SideTank',
'engine-0 ': 'EngineTiny',
'engine-1 ': 'EngineSmall',
'engine-2 ': 'EngineMedium',
'engine-3 ': 'EngineLarge',
'engine-4 ': 'SolidRocketBooster',
'ion-0 ': 'EngineIon',
part_type_sprite: Dict[str, str] = {'pod-1': 'Pod',
'detacher-1': 'DetacherVertical',
'detacher-2': 'DetacherRadial',
'wheel-1': 'Wheel',
'wheel-2': 'Wheel',
'fuselage-1': 'Fuselage',
'strut-1': 'Beam',
'fueltank-0': 'TankTiny',
'fueltank-1': 'TankSmall',
'fueltank-2': 'TankMedium',
'fueltank-3': 'TankLarge',
'fueltank-4': 'Puffy750',
'fueltank-5': 'SideTank',
'engine-0': 'EngineTiny',
'engine-1': 'EngineSmall',
'engine-2': 'EngineMedium',
'engine-3': 'EngineLarge',
'engine-4': 'SolidRocketBooster',
'ion-0': 'EngineIon',
'parachute-1': 'ParachuteCanister',
'nosecone-1 ': 'NoseCone',
'rcs-1 ': 'RcsBlock',
'solar-1 ': 'SolarPanelBase',
'battery-0 ': 'Battery',
'dock-1 ': 'DockingConnector',
'port-1 ': 'DockingPort',
'lander-1 ': 'LanderLegPreview'}
'nosecone-1': 'NoseCone',
'rcs-1': 'RcsBlock',
'solar-1': 'SolarPanelBase',
'battery-0': 'Battery',
'dock-1': 'DockingConnector',
'port-1': 'DockingPort',
'lander-1': 'LanderLegPreview'}
@classmethod
def get_sprite_from_type(cls, name: str) -> Union[None, str]:
@ -112,6 +132,6 @@ class SR1PartTexture:
#
# for part_type in list(part_list_root):
# part_type: Element
# print(f'\'{part_type.attrib.get("id"):<11}\': \'{part_type.attrib.get("sprite")}\'')
# print(f'\'{part_type.attrib.get("id")}\': \'{part_type.attrib.get("sprite")}\'')
#
#

View File

@ -156,6 +156,7 @@ class ClientWindow(Window):
self.command_tree = tree.CommandTree(tree.DR_command)
self.input_box = InputBox(x=50, y=30, width=300, height=20,
batch=self.label_batch) # 实例化
self.input_box.push_handlers(self)
self.push_handlers(self.input_box)
self.input_box.enabled = True
# 设置刷新率

View File

@ -4,21 +4,21 @@
# All rights reserved
# -------------------------------
from typing import List, TYPE_CHECKING, Union
from xml.etree import ElementTree
from typing import List, TYPE_CHECKING, Union
# third party package
from defusedxml.ElementTree import parse
# pyglet
from pyglet.graphics import Batch
from pyglet.resource import texture
from pyglet.graphics import Batch, Group
from pyglet.sprite import Sprite
# Difficult Rocket
from Difficult_Rocket import DR_option
from Difficult_Rocket.api.types.SR1 import SR1Textures, SR1PartTexture
from Difficult_Rocket.command.line import CommandText
from Difficult_Rocket.client.screen import BaseScreen
from Difficult_Rocket.api.types.SR1 import SR1Textures, SR1PartTexture, SR1PartData
if TYPE_CHECKING:
from Difficult_Rocket.client import ClientWindow
@ -36,6 +36,9 @@ class SR1ShipRender(BaseScreen):
self.xml_doc = parse('configs/dock1.xml')
self.xml_root: ElementTree.Element = self.xml_doc.getroot()
self.part_batch = Batch()
self.part_group = Group()
self.part_data = {}
self.parts_sprite = {}
def load_textures(self):
self.textures = SR1Textures()
@ -48,26 +51,40 @@ class SR1ShipRender(BaseScreen):
if part.tag != 'Part':
continue # 如果不是部件,则跳过
# print(f"tag: {part.tag} attrib: {part.attrib}")
part_id = part.attrib.get('id')
part_render = True
part_id = int(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
part_x = float(part.attrib.get('x'))
part_y = float(part.attrib.get('y'))
part_activate = not not (part.attrib.get('activated') or 0)
part_angle = float(part.attrib.get('angle'))
part_angle_v = float(part.attrib.get('angleV'))
part_editor_angle = int(part.attrib.get('editorAngle'))
part_flip_x = not not (part.attrib.get('flippedX') or 0)
part_flip_y = not not (part.attrib.get('flippedY') or 0)
part_explode = not not (part.attrib.get('exploded') or 0)
if part_type not in SR1PartTexture.part_type_sprite:
part_render = False
print('Textures None found!')
part_textures = None
else:
part_textures = SR1PartTexture.get_sprite_from_type(part_type)
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} '
f'flip_x: {part_flip_x} flip_y: {part_flip_y} explode: {part_explode} '
f'textures: {SR1PartTexture.get_sprite_from_type(part_type)}')
if part_id in self.part_data:
print(f'hey! warning! id{part_id}')
part_data = SR1PartData(x=part_x, y=part_y, id=part_id, type=part_type,
angle=part_angle, angle_v=part_angle_v,
editor_angle=part_editor_angle, flip_x=part_flip_x,
flip_y=part_flip_y, explode=part_explode, textures=part_textures)
self.part_data[part_id] = part_data
self.parts_sprite[part_id] = Sprite(img=self.textures.get_texture(part_data.textures),
x=10, y=10, batch=self.part_batch, group=self.part_group)
def on_draw(self):
...
self.part_batch.draw()
def on_file_drop(self, x: int, y: int, paths: List[str]):
self.scale = DR_option.gui_scale