From 5552d087c5b02f7ae9b61171b6b7cb4c6da79ba0 Mon Sep 17 00:00:00 2001 From: march3 Date: Tue, 4 Jul 2023 10:59:06 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?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 --- sim_scenes/func.py | 4 ++++ sim_scenes/science/speed_of_light_3d.py | 12 ++++++++++-- simulators/ursina/ursina_event.py | 15 +++++++++++++++ simulators/ursina_simulator.py | 5 ++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/sim_scenes/func.py b/sim_scenes/func.py index d404eef..2064cc8 100644 --- a/sim_scenes/func.py +++ b/sim_scenes/func.py @@ -307,11 +307,15 @@ def create_3d_card(left=-.885, top=0.495, width=0.02, height=0.02): position=(left, top, 0) ) + panel.switch_flag = 0 + def switch_color(): if panel.color == color.black: panel.color = color.white + panel.switch_flag = 1 else: panel.color = color.black + panel.switch_flag = 0 panel.switch_color = switch_color diff --git a/sim_scenes/science/speed_of_light_3d.py b/sim_scenes/science/speed_of_light_3d.py index 1c3743c..76f29cd 100644 --- a/sim_scenes/science/speed_of_light_3d.py +++ b/sim_scenes/science/speed_of_light_3d.py @@ -98,12 +98,18 @@ def on_timer_changed(time_data: TimeData): distance = round(init.light_ship.position[2] / AU, 4) text = init.arrived_info.replace("${distance}", "%.4f AU" % distance) init.text_panel.text = text.replace("${speed}", str(round(velocity / LIGHT_SPEED, 1)) + "倍光速") - init._3d_card.switch_color() - light_ship.switch_position() + # if time_data.total_seconds > 20: # wait_for(0.03) +def on_before_evolving(evolve_args): + init._3d_card.switch_color() + light_ship.switch_position() + if init._3d_card.switch_flag == 1: + evolve_args["evolve_dt"] = 0.0 + + # 订阅重新开始事件 # 按键盘的 “O” 重置键会触发 on_reset UrsinaEvent.on_reset_subscription(on_reset) @@ -113,6 +119,8 @@ UrsinaEvent.on_ready_subscription(on_ready) # 运行中,每时每刻都会触发 on_timer_changed UrsinaEvent.on_timer_changed_subscription(on_timer_changed) +UrsinaEvent.on_before_evolving_subscription(on_before_evolving) + def body_arrived(body): # # 到达每个行星都会触发,对光速飞船进行加速,超光速前进(使用未来曲率引擎技术) diff --git a/simulators/ursina/ursina_event.py b/simulators/ursina/ursina_event.py index 9532cc6..a0bc6db 100644 --- a/simulators/ursina/ursina_event.py +++ b/simulators/ursina/ursina_event.py @@ -36,6 +36,8 @@ class UrsinaEvent: UrsinaEvent.on_body_size_changed_callback = [] # 逐步演变触发的订阅事件 UrsinaEvent.on_evolving_callback = [] + # 逐步演变触发前的订阅事件 + UrsinaEvent.on_before_evolving_callback = [] # 计时器触发的订阅事件 UrsinaEvent.on_timer_changed_callback = [] @@ -78,6 +80,19 @@ class UrsinaEvent: for f in UrsinaEvent.on_evolving_callback: f(evolve_dt) + @staticmethod + def on_before_evolving_subscription(fun): + UrsinaEvent.on_before_evolving_callback.append(fun) + + @staticmethod + def on_before_evolving_unsubscription(fun): + UrsinaEvent.on_before_evolving_callback.remove(fun) + + @staticmethod + def on_before_evolving(evolve_args): + for f in UrsinaEvent.on_before_evolving_callback: + f(evolve_args) + @staticmethod def on_application_run_callback_subscription(fun): UrsinaEvent.on_application_run_callback.append(fun) diff --git a/simulators/ursina_simulator.py b/simulators/ursina_simulator.py index 1223d95..e196871 100644 --- a/simulators/ursina_simulator.py +++ b/simulators/ursina_simulator.py @@ -224,7 +224,10 @@ class UrsinaSimulator(Simulator): UrsinaEvent.on_evolving(evolve_dt) # interval_fator 能让更新天体运行状态(位置、速度)更精确 evolve_dt = evolve_dt * self.interval_fator - super().evolve(evolve_dt) + evolve_args = {"evolve_dt": evolve_dt} + UrsinaEvent.on_before_evolving(evolve_args) + # if evolve_args["evolve_dt"] > 0: + super().evolve(evolve_args["evolve_dt"]) if self.show_timer or self.timer_enabled: timer = BodyTimer() -- GitLab