From 7076f48a46c7bcdd7d002cea1cd6f1bf688f54ae Mon Sep 17 00:00:00 2001 From: march3 Date: Thu, 16 Mar 2023 17:27:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E7=B3=BB=E4=B8=89=E4=BD=93?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/system.py | 10 ++++----- simulators/ursina/ursina_config.py | 4 ++++ simulators/ursina/ursina_ui.py | 24 ++++++++++++++++++++ simulators/views/ursina_view.py | 36 +++++++++++++----------------- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/common/system.py b/common/system.py index a41fed5..a1dacf0 100644 --- a/common/system.py +++ b/common/system.py @@ -80,11 +80,11 @@ class System(object): """ if not body.appeared: # 不显示 return False - if self.max_distance > 0: - # 超过了 max_distance 距离,则不显示,并消失 - if calculate_distance(body.position) > self.max_distance: - body.appeared = False - return False + # if self.max_distance > 0: + # # 超过了 max_distance 距离,则不显示,并消失 + # if calculate_distance(body.position) > self.max_distance: + # body.appeared = False + # return False return True diff --git a/simulators/ursina/ursina_config.py b/simulators/ursina/ursina_config.py index c707b5a..9c094c9 100644 --- a/simulators/ursina/ursina_config.py +++ b/simulators/ursina/ursina_config.py @@ -28,6 +28,10 @@ class UrsinaConfig: __on_reset_funcs = [] + show_trail = False + # 拖尾球体的数量 + trail_length = 200 + @property @classmethod def run_speed_factor(cls): diff --git a/simulators/ursina/ursina_ui.py b/simulators/ursina/ursina_ui.py index 5d3921b..adcba8c 100644 --- a/simulators/ursina/ursina_ui.py +++ b/simulators/ursina/ursina_ui.py @@ -39,10 +39,23 @@ class UrsinaUI: self.slider_body_spin_factor.on_value_changed = self.on_slider_body_spin_changed self.slider_run_speed_factor.on_value_changed = self.on_slider_run_speed_changed self.slider_control_speed_factor.on_value_changed = self.on_slider_control_speed_changed + + self.slider_trail_length = Slider(text="拖尾长度", y=-.1, step=1, min=30, max=500, default=UrsinaConfig.trail_length, + color=color.rgba(0.0, 0.0, 0.0, 0.5)) + self.slider_trail_length.on_value_changed = self.on_slider_trail_length_changed + + self.on_off_switch = ButtonGroup(('||', '○'), min_selection=1, y=0, default='○', selected_color=color.green, ignore_paused=True, color=color.rgba(0.0, 0.0, 0.0, 0.5)) self.on_off_switch.selected_color = color.red + + self.on_off_trail = ButtonGroup((' ', '...'), min_selection=1, y=0, default=' ', + selected_color=color.green, ignore_paused=True, + color=color.rgba(0.0, 0.0, 0.0, 0.5)) + + self.on_off_trail.on_value_changed = self.on_off_trail_changed + self.point_button = Button(text='寻找', origin=(0, 0), y=2, on_click=self.on_point_button_click, color=color.rgba(0.0, 0.0, 0.0, 0.5)) self.reset_button = Button(text='重置', origin=(0, 0), y=2, @@ -57,6 +70,8 @@ class UrsinaUI: self.point_button, self.reset_button, self.on_off_switch, + self.on_off_trail, + self.slider_trail_length, self.slider_body_spin_factor, self.slider_run_speed_factor, self.slider_control_speed_factor @@ -67,6 +82,12 @@ class UrsinaUI: wp.x = -wp.scale_x self.wp = wp + def on_off_trail_changed(self): + if self.on_off_trail.value == "...": + UrsinaConfig.show_trail = True + else: + UrsinaConfig.show_trail = False + def on_point_button_click(self): pass @@ -81,6 +102,9 @@ class UrsinaUI: self.on_off_switch.selected_color = color.red application.paused = False + def on_slider_trail_length_changed(self): + UrsinaConfig.trail_length = int(self.slider_trail_length.value) + def on_slider_control_speed_changed(self): application.time_scale = self.slider_control_speed_factor.value diff --git a/simulators/views/ursina_view.py b/simulators/views/ursina_view.py index 9e54800..5356071 100644 --- a/simulators/views/ursina_view.py +++ b/simulators/views/ursina_view.py @@ -76,19 +76,9 @@ class Planet(Entity): def on_reset(self): # 删除拖尾 - for entity, pos in self.trails.items(): - destroy(entity) + self.clear_trails() self.body_view.body.reset() - # pos = body.init_position * body.distance_scale * UrsinaConfig.SCALE_FACTOR - # vel = body.init_velocity - # self.x = -pos[1] - # self.y = pos[2] - # self.z = pos[0] - # self.x = pos[0] - # self.y = pos[1] - # self.z = pos[2] - def __init__(self, body_view: BodyView): self.body_view = body_view self.rotation_speed = self.body_view.body.rotation_speed @@ -139,8 +129,6 @@ class Planet(Entity): """ # 存放拖尾球体 self.trails = {} - # 拖尾球体的数量 - self.trail_len = 100 # 根据天体的颜色获取拖尾的颜色 trail_color = conv_to_vec4_color(self.body_view.body.color) @@ -190,7 +178,7 @@ class Planet(Entity): self.trails[self.create_trail(pos)] = pos # 计算拖尾球体超过的数量 - trail_overflow_count = len(self.trails) - self.trail_len + trail_overflow_count = len(self.trails) - UrsinaConfig.trail_length if trail_overflow_count > 0: # 如果拖尾球体超过的数量,就删除之前的拖尾球体 @@ -248,9 +236,12 @@ class Planet(Entity): self.rotation_y, self.rotation_z) - # 有时候第一个位置不正确,所以判断一下有历史记录后在创建 - if len(self.body_view.body.his_position()) > 1: - self.create_trails() + if UrsinaConfig.show_trail: + # 有时候第一个位置不正确,所以判断一下有历史记录后在创建 + if len(self.body_view.body.his_position()) > 1: + self.create_trails() + else: + self.clear_trails() def follow_parent(self): if not hasattr(self.body_view, "bodies_system"): @@ -284,13 +275,18 @@ class Planet(Entity): # 设置行星环不受灯光影响,否则看不清行星环 self.ring.set_light_off() + def clear_trails(self): + if not hasattr(self, "trails"): + return + # 删除拖尾 + for entity, pos in self.trails.items(): + destroy(entity) + def destroy_all(self): # 从天体系统中移除自己(TODO:暂时还不能移除) # self.body_view.bodies_system.bodies.remove(self.body_view.body) - # 删除拖尾 - for entity, pos in self.trails.items(): - destroy(entity) + self.clear_trails() # 如果有行星环,则删除行星环 if hasattr(self, "ring"): destroy(self.ring) -- GitLab