Skip to content

Commit

Permalink
Extract example app creation into utility function (#369)
Browse files Browse the repository at this point in the history
Extract the creation of the Bevy app for all examples into a utility
function. The app creation is somewhat interesting to look at once, but
is otherwise very repetitive and clutters the examples with 20+ lines of
code, removing the focus on the actual specifics a particular example is
trying to demonstrate.

This change also ensures consistency across all examples, ensures
they're returning an error code on termination if any error occurs (for
CI), and make examples easier to maintain.
  • Loading branch information
djeedai authored Aug 16, 2024
1 parent e75fa2b commit 14fc6b2
Show file tree
Hide file tree
Showing 21 changed files with 207 additions and 576 deletions.
34 changes: 5 additions & 29 deletions examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,21 @@
//! particle above or below the reference square.

use bevy::{
log::LogPlugin,
prelude::*,
render::camera::ScalingMode,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod utils;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.insert_resource(ClearColor(Color::BLACK))
.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,2d=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — 2d".to_string(),
..default()
}),
..default()
}),
)
.add_plugins(HanabiPlugin);

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());

app.add_systems(Startup, setup)
.add_systems(Update, (utils::close_on_esc, update_plane))
let app_exit = utils::make_test_app("2d")
.add_systems(Startup, setup)
.add_systems(Update, update_plane)
.run();

Ok(())
app_exit.into_result()
}

fn setup(
Expand Down
38 changes: 6 additions & 32 deletions examples/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,18 @@
//! [`KillAabbModifier`] to ensure the bubble particles never escape water, and
//! are despawned when reaching the surface.

use bevy::{
core_pipeline::tonemapping::Tonemapping, log::LogPlugin, prelude::*,
render::camera::ScalingMode,
};
use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, render::camera::ScalingMode};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod utils;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.insert_resource(ClearColor(Color::BLACK))
.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,activate=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — activate".to_string(),
..default()
}),
..default()
}),
)
.add_plugins(HanabiPlugin);

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());

app.add_systems(Startup, setup)
.add_systems(Update, (utils::close_on_esc, update))
let app_exit = utils::make_test_app("activate")
.add_systems(Startup, setup)
.add_systems(Update, update)
.run();

Ok(())
app_exit.into_result()
}

#[derive(Component)]
Expand Down
37 changes: 6 additions & 31 deletions examples/billboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,18 @@

use std::f32::consts::FRAC_PI_2;

use bevy::{
core_pipeline::tonemapping::Tonemapping, log::LogPlugin, prelude::*, render::camera::Projection,
};
use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, render::camera::Projection};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod utils;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.insert_resource(ClearColor(Color::BLACK))
.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,billboard=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — billboard".to_string(),
..default()
}),
..default()
}),
)
.add_plugins(HanabiPlugin);

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());

app.add_systems(Startup, setup)
.add_systems(Update, (utils::close_on_esc, rotate_camera))
let app_exit = utils::make_test_app("billboard")
.add_systems(Startup, setup)
.add_systems(Update, rotate_camera)
.run();

Ok(())
app_exit.into_result()
}

fn setup(
Expand Down
35 changes: 6 additions & 29 deletions examples/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,20 @@

use std::f32::consts::FRAC_PI_2;

use bevy::{core_pipeline::tonemapping::Tonemapping, log::LogPlugin, prelude::*};
use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod texutils;
mod utils;

use texutils::make_anim_img;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.insert_resource(ClearColor(Color::BLACK))
.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,circle=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — circle".to_string(),
..default()
}),
..default()
}),
)
.add_systems(Update, utils::close_on_esc)
.add_plugins(HanabiPlugin);

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());

app.add_systems(Startup, setup).run();

Ok(())
let app_exit = utils::make_test_app("circle")
.add_systems(Startup, setup)
.run();
app_exit.into_result()
}

fn setup(
Expand Down
33 changes: 5 additions & 28 deletions examples/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,18 @@

use bevy::{
core_pipeline::{bloom::BloomSettings, tonemapping::Tonemapping},
log::LogPlugin,
prelude::*,
};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod utils;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,expr=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — expr".to_string(),
..default()
}),
..default()
}),
)
.add_systems(Update, utils::close_on_esc)
.add_plugins(HanabiPlugin);

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());

app.add_systems(Startup, setup).run();

Ok(())
let app_exit = utils::make_test_app("expr")
.add_systems(Startup, setup)
.run();
app_exit.into_result()
}

fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
Expand Down
33 changes: 5 additions & 28 deletions examples/firework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,18 @@

use bevy::{
core_pipeline::{bloom::BloomSettings, tonemapping::Tonemapping},
log::LogPlugin,
prelude::*,
};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod utils;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,firework=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — firework".to_string(),
..default()
}),
..default()
}),
)
.add_systems(Update, utils::close_on_esc)
.add_plugins(HanabiPlugin);

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());

app.add_systems(Startup, setup).run();

Ok(())
let app_exit = utils::make_test_app("firework")
.add_systems(Startup, setup)
.run();
app_exit.into_result()
}

fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
Expand Down
35 changes: 8 additions & 27 deletions examples/force_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,17 @@
//! to the projection on screen; however those particles are actually at a
//! different depth, in front or behind the sphere.

use bevy::{core_pipeline::tonemapping::Tonemapping, log::LogPlugin, prelude::*};
use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;

mod utils;
use utils::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::default();
app.insert_resource(ClearColor(Color::BLACK))
.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::INFO,
filter: "bevy_hanabi=warn,force_field=trace".to_string(),
..default()
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "🎆 Hanabi — force field".to_string(),
..default()
}),
..default()
}),
)
.add_plugins(HanabiPlugin);
let mut app = utils::make_test_app("force_field");

#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default())
.init_resource::<inspector::Configuration>()
app.init_resource::<inspector::Configuration>()
.register_type::<inspector::Configuration>()
.add_systems(Update, inspector::inspector_ui)
.add_systems(
Expand All @@ -52,10 +33,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);

app.add_systems(Startup, setup)
.add_systems(Update, (utils::close_on_esc, spawn_on_click, move_repulsor))
.add_systems(Update, (spawn_on_click, move_repulsor))
.run();

Ok(())
app.run().into_result()
}

const BALL_RADIUS: f32 = 0.05;
Expand Down Expand Up @@ -227,7 +208,7 @@ fn setup(
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(6., 4., 6.)),
material: materials.add(StandardMaterial {
base_color: Color::linear_rgba(0., 0.7, 0., 0.3),
base_color: Color::linear_rgba(0., 0.7, 0., 0.05),
unlit: true,
alpha_mode: bevy::prelude::AlphaMode::Blend,
..Default::default()
Expand All @@ -239,7 +220,7 @@ fn setup(
commands.spawn(PbrBundle {
mesh: meshes.add(Sphere { radius: 0.6 }),
material: materials.add(StandardMaterial {
base_color: Color::linear_rgba(0.7, 0., 0., 0.3),
base_color: Color::linear_rgba(0.7, 0., 0., 0.2),
unlit: true,
alpha_mode: bevy::prelude::AlphaMode::Blend,
..Default::default()
Expand Down
Loading

0 comments on commit 14fc6b2

Please sign in to comment.