Skip to content

Commit

Permalink
Bevy 0.15 release (#5)
Browse files Browse the repository at this point in the history
* core: small reorg

* checkpoint 2024-11-29 evening

* deps: compiles with bevy 0.15.0
  • Loading branch information
philiplinden authored Nov 30, 2024
1 parent ae590a3 commit 1c697fb
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 96 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ dev_native = [
]

[dependencies]
bevy = "0.15.0-rc.3"
avian3d = { git = "https://github.com/Jondolf/avian.git", branch = "bevy-0.15", features = ["debug-plugin"] }
bevy = "0.15.0"
avian3d = { git = "https://github.com/Jondolf/avian.git", branch = "main", features = ["debug-plugin"] }
bevy-trait-query = { git = "https://github.com/JoJoJet/bevy-trait-query.git", branch = "bevy-0.15-rc" }
iyes_perf_ui = { git = "https://github.com/JohnathanFL/iyes_perf_ui.git", branch = "main" }

Expand Down
25 changes: 25 additions & 0 deletions docs/devlog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# development log

## 2024-11-30

One last check for third party crates to see if they've officially updated to
Bevy 0.15 support. Looks like they haven't but there are branches for it.

- [x] `avian3d` -> branch `main`
- [x] `bevy-trait-query` -> branch `bevy-0.15-rc`
- [x] `iyes_perf_ui` -> branch `main`

## 2024-11-29

I should lean into the wireframe aesthetic and make this whole thing look like a
retro radar display. That sounds like a fun distraction. I borrowed some code
and shader assets from
[philiplinden/bevy-jam-5](https://github.com/philiplinden/bevy-jam-5).

I got the app to compile and run with the CRT shader. The screen is bent as
expected, but nothing appears in the frame. I'm not sure why.

I'm getting a lot better at Bevy. It takes much less time to make new
components, systems, Uis, etc. I'm also getting faster at finding and fixing
bugs too.

Added togglable performance Ui.

## 2024-11-28

I tinkered with camera controls and the new `require` attribute. Now the camera
Expand Down
2 changes: 1 addition & 1 deletion src/app3d/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Plugin for CameraPlugin {

#[derive(Component, Default)]
#[require(Camera3d, PerspectiveProjection)]
struct MainCamera;
pub struct MainCamera;

/// A resource that stores the currently selected camera target.
#[derive(Resource)]
Expand Down
57 changes: 57 additions & 0 deletions src/app3d/controls.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use bevy::prelude::*;

use crate::simulator::{SimState, time::TimeScaleOptions};

pub struct ControlsPlugin;

impl Plugin for ControlsPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<KeyBindingsConfig>();
app.add_plugins((PausePlayPlugin, ChangeTimeScalePlugin));
}
}

Expand Down Expand Up @@ -42,6 +45,7 @@ pub struct TimeControls {
pub faster: KeyCode,
pub slower: KeyCode,
pub reset_speed: KeyCode,
pub toggle_real_time: KeyCode,
pub scale_step: f32,
}

Expand Down Expand Up @@ -81,7 +85,60 @@ impl Default for TimeControls {
faster: KeyCode::ArrowUp,
slower: KeyCode::ArrowDown,
reset_speed: KeyCode::Backspace,
toggle_real_time: KeyCode::KeyR,
scale_step: 0.1,
}
}
}

// ============================ CONTROL SYSTEMS ================================

struct PausePlayPlugin;

impl Plugin for PausePlayPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, toggle_pause);
}
}

fn toggle_pause(
sim_state: Res<State<SimState>>,
mut next_state: ResMut<NextState<SimState>>,
key_input: Res<ButtonInput<KeyCode>>,
key_bindings: Res<KeyBindingsConfig>,
) {
if key_input.just_pressed(key_bindings.time_controls.toggle_pause) {
match sim_state.as_ref().get() {
SimState::Stopped => next_state.set(SimState::Running),
SimState::Running => next_state.set(SimState::Stopped),
_ => next_state.set(SimState::Running)
}
}
}

struct ChangeTimeScalePlugin;

impl Plugin for ChangeTimeScalePlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, modify_time_scale);
}
}

fn modify_time_scale(
mut time_options: ResMut<TimeScaleOptions>,
key_input: Res<ButtonInput<KeyCode>>,
key_bindings: Res<KeyBindingsConfig>,
) {
if key_input.just_pressed(key_bindings.time_controls.faster) {
time_options.multiplier += key_bindings.time_controls.scale_step;
}
if key_input.just_pressed(key_bindings.time_controls.slower) {
time_options.multiplier -= key_bindings.time_controls.scale_step;
}
if key_input.just_pressed(key_bindings.time_controls.reset_speed) {
time_options.reset();
}
if key_input.just_pressed(key_bindings.time_controls.toggle_real_time) {
time_options.toggle_real_time();
}
}
143 changes: 131 additions & 12 deletions src/app3d/dev_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use bevy::{
input::common_conditions::input_just_pressed,
prelude::*,
};
use iyes_perf_ui::{PerfUiSet, prelude::*};

use crate::simulator::SimState;

use super::{controls::KeyBindingsConfig, gizmos::ForceGizmos};
use crate::simulator::{SimState, forces::Force};
use super::controls::KeyBindingsConfig;

pub struct DevToolsPlugin;

Expand All @@ -26,6 +26,7 @@ impl Plugin for DevToolsPlugin {
app.add_plugins((
// physics
PhysicsDebugPlugin::default(),
ForceArrowsPlugin,
// performance
FrameTimeDiagnosticsPlugin,
EntityCountDiagnosticsPlugin,
Expand All @@ -38,13 +39,12 @@ impl Plugin for DevToolsPlugin {

app.add_systems(Update, (
log_transitions::<SimState>,
// show_performance_stats,
show_force_gizmos,
show_physics_gizmos,
));

// Wireframe doesn't work on WASM
#[cfg(not(target_arch = "wasm32"))]
app.add_systems(Update, toggle_debug_ui);
app.add_systems(Update, toggle_debug_ui.before(PerfUiSet::Setup));
// #[cfg(feature = "inspect")]
// {
// use bevy_inspector_egui::quick::WorldInspectorPlugin;
Expand All @@ -55,6 +55,7 @@ impl Plugin for DevToolsPlugin {

#[derive(Debug, Resource)]
struct DebugState {
performance: bool,
wireframe: bool,
forces: bool,
physics: bool,
Expand All @@ -63,6 +64,7 @@ struct DebugState {
impl Default for DebugState {
fn default() -> Self {
Self {
performance: true,
wireframe: false,
forces: true,
physics: false,
Expand All @@ -71,6 +73,10 @@ impl Default for DebugState {
}

impl DebugState {
fn toggle_performance(&mut self) {
self.performance = !self.performance;
warn!("performance debug: {}", self.performance);
}
fn toggle_wireframe(&mut self) {
self.wireframe = !self.wireframe;
warn!("wireframe debug: {}", self.wireframe);
Expand All @@ -85,10 +91,6 @@ impl DebugState {
}
}

#[allow(dead_code)]
#[derive(Component, Default)]
struct DebugUi;

#[cfg(not(target_arch = "wasm32"))]
fn toggle_debug_ui(
mut wireframe_config: ResMut<WireframeConfig>,
Expand All @@ -97,17 +99,54 @@ fn toggle_debug_ui(
key_bindings: Res<KeyBindingsConfig>,
) {
if key_input.just_pressed(key_bindings.debug_controls.toggle_1) {
debug_state.toggle_performance();
}
if key_input.just_pressed(key_bindings.debug_controls.toggle_2) {
// Wireframe doesn't work on WASM
#[cfg(not(target_arch = "wasm32"))]
debug_state.toggle_wireframe();
wireframe_config.global = !wireframe_config.global;
}
if key_input.just_pressed(key_bindings.debug_controls.toggle_2) {
if key_input.just_pressed(key_bindings.debug_controls.toggle_3) {
debug_state.toggle_forces();
}
if key_input.just_pressed(key_bindings.debug_controls.toggle_3) {
if key_input.just_pressed(key_bindings.debug_controls.toggle_4) {
debug_state.toggle_physics();
}
}

#[derive(Component, Default)]
struct PerformanceDebugUi;

Check failure on line 119 in src/app3d/dev_tools.rs

View workflow job for this annotation

GitHub Actions / build

struct `PerformanceDebugUi` is never constructed

fn show_performance_stats(

Check failure on line 121 in src/app3d/dev_tools.rs

View workflow job for this annotation

GitHub Actions / build

function `show_performance_stats` is never used
mut commands: Commands,
debug_state: Res<DebugState>,
ui_root: Query<Entity, With<PerformanceDebugUi>>,
) {
if debug_state.is_changed() {
if debug_state.performance {
if let Ok(entity) = ui_root.get_single() {
commands.entity(entity).despawn_descendants();
}
commands.spawn((
PerformanceDebugUi,
PerfUiRoot {
position: PerfUiPosition::TopLeft,
..default()
},
PerfUiEntryFPS::default(),
PerfUiEntryFixedTimeStep::default(),
PerfUiEntryFixedOverstep::default(),

));
} else {
if let Ok(entity) = ui_root.get_single() {
commands.entity(entity).despawn_descendants();
}
}
}
}

fn show_force_gizmos(
debug_state: Res<DebugState>,
mut gizmo_store: ResMut<GizmoConfigStore>
Expand Down Expand Up @@ -135,3 +174,83 @@ fn show_physics_gizmos(
}
}
}

const ARROW_SCALE: f32 = 0.1;

pub struct ForceArrowsPlugin;

impl Plugin for ForceArrowsPlugin {
fn build(&self, app: &mut App) {
app.init_gizmo_group::<ForceGizmos>();
app.register_type::<ForceGizmos>();
app.add_systems(
PostUpdate,
force_arrows.run_if(
|store: Res<GizmoConfigStore>| {
store.config::<ForceGizmos>().0.enabled
}),
);
}
}

fn force_arrows(
query: Query<&dyn Force>,
mut gizmos: Gizmos,
) {
for forces in query.iter() {
for force in forces.iter() {
let start = force.point_of_application();
let end = start + force.force() * ARROW_SCALE;
let color = match force.color() {
Some(c) => c,
None => RED.into(),
};
gizmos.arrow(start, end, color).with_tip_length(0.3);
}
}
}

#[derive(Reflect, GizmoConfigGroup)]
pub struct ForceGizmos {
/// The scale of the force arrows.
pub arrow_scale: Option<f32>,
/// The color of the force arrows. If `None`, the arrows will not be rendered.
pub arrow_color: Option<Color>,
/// The length of the arrow tips.
pub tip_length: Option<f32>,
/// Determines if the forces should be hidden when not active.
pub enabled: bool,
}

impl Default for ForceGizmos {
fn default() -> Self {
Self {
arrow_scale: Some(0.1),
arrow_color: Some(RED.into()),
tip_length: Some(0.3),
enabled: false,
}
}
}

impl ForceGizmos {
/// Creates a [`ForceGizmos`] configuration with all rendering options enabled.
pub fn all() -> Self {
Self {
arrow_scale: Some(0.1),
arrow_color: Some(RED.into()),
tip_length: Some(0.3),
enabled: true,
}
}

/// Creates a [`ForceGizmos`] configuration with debug rendering enabled but all options turned off.
pub fn none() -> Self {
Self {
arrow_scale: None,
arrow_color: None,
tip_length: None,
enabled: false,
}
}
}
Loading

0 comments on commit 1c697fb

Please sign in to comment.