Skip to content

Commit

Permalink
[demos] Add info "tab" to GUI, and show in it the names of the object…
Browse files Browse the repository at this point in the history
…s the character stands on.
  • Loading branch information
idanarye committed May 20, 2024
1 parent 4540764 commit 17c4ec5
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 40 deletions.
3 changes: 2 additions & 1 deletion demos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default = [
"bevy_xpbd_3d?/parry-f32",
"bevy_xpbd_2d?/parry-f32",
]
egui = ["dep:bevy_egui", "dep:egui_plot"]
egui = ["dep:bevy_egui", "dep:egui_plot", "dep:egui_extras"]
rapier = []
rapier2d = ["rapier", "dep:bevy_rapier2d", "dep:bevy-tnua-rapier2d"]
rapier3d = ["rapier", "dep:bevy_rapier3d", "dep:bevy-tnua-rapier3d"]
Expand Down Expand Up @@ -61,6 +61,7 @@ bevy-tnua-xpbd3d = { path = "../xpbd3d", default-features = false, optional = tr

bevy_egui = { version = "0.27", optional = true, default-features = false, features = ["default_fonts", "render"] }
egui_plot = { version = "0.27", optional = true }
egui_extras = { version = "0.27", optional = true }

clap = { version = "^4", features = ["derive"] }

Expand Down
15 changes: 13 additions & 2 deletions demos/src/bin/platformer_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ use bevy_tnua_xpbd2d::*;
use bevy_xpbd_2d::{prelude as xpbd, prelude::*, PhysicsSchedule};

use tnua_demos_crate::app_setup_options::{AppSetupConfiguration, ScheduleToUse};
use tnua_demos_crate::character_control_systems::info_dumpeing_systems::character_control_info_dumping_system;
use tnua_demos_crate::character_control_systems::platformer_control_systems::{
apply_platformer_controls, CharacterMotionConfigForPlatformerDemo, FallingThroughControlScheme,
};
use tnua_demos_crate::character_control_systems::Dimensionality;
#[cfg(feature = "xpbd2d")]
use tnua_demos_crate::levels_setup::for_2d_platformer::LayerNames;
use tnua_demos_crate::ui::component_alterbation::CommandAlteringSelectors;
use tnua_demos_crate::ui::info::InfoSource;
#[cfg(feature = "egui")]
use tnua_demos_crate::ui::plotting::PlotSource;
use tnua_demos_crate::ui::DemoInfoUpdateSystemSet;
use tnua_demos_crate::MovingPlatformPlugin;

fn main() {
Expand Down Expand Up @@ -101,6 +104,11 @@ fn main() {
}
}

#[cfg(feature = "egui")]
app.add_systems(
Update,
character_control_info_dumping_system.in_set(DemoInfoUpdateSystemSet),
);
app.add_plugins(tnua_demos_crate::ui::DemoUi::<
CharacterMotionConfigForPlatformerDemo,
>::default());
Expand Down Expand Up @@ -339,7 +347,10 @@ fn setup_player(mut commands: Commands) {
// This helper keeps track of air actions like jumps or air dashes.
cmd.insert(TnuaSimpleAirActionsCounter::default());

cmd.insert(tnua_demos_crate::ui::TrackedEntity("Player".to_owned()));
#[cfg(feature = "egui")]
cmd.insert(PlotSource::default());
cmd.insert((
tnua_demos_crate::ui::TrackedEntity("Player".to_owned()),
PlotSource::default(),
InfoSource::default(),
));
}
16 changes: 13 additions & 3 deletions demos/src/bin/platformer_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ use tnua_demos_crate::app_setup_options::{AppSetupConfiguration, ScheduleToUse};
use tnua_demos_crate::character_animating_systems::platformer_animating_systems::{
animate_platformer_character, AnimationState,
};
use tnua_demos_crate::character_control_systems::info_dumpeing_systems::character_control_info_dumping_system;
use tnua_demos_crate::character_control_systems::platformer_control_systems::{
apply_platformer_controls, CharacterMotionConfigForPlatformerDemo, FallingThroughControlScheme,
};
use tnua_demos_crate::character_control_systems::Dimensionality;
#[cfg(feature = "xpbd3d")]
use tnua_demos_crate::levels_setup::for_3d_platformer::LayerNames;
use tnua_demos_crate::ui::component_alterbation::CommandAlteringSelectors;
use tnua_demos_crate::ui::info::InfoSource;
#[cfg(feature = "egui")]
use tnua_demos_crate::ui::plotting::PlotSource;
use tnua_demos_crate::ui::DemoInfoUpdateSystemSet;
use tnua_demos_crate::util::animating::{animation_patcher_system, GltfSceneHandler};
use tnua_demos_crate::MovingPlatformPlugin;

Expand Down Expand Up @@ -101,6 +104,11 @@ fn main() {
}
}

#[cfg(feature = "egui")]
app.add_systems(
Update,
character_control_info_dumping_system.in_set(DemoInfoUpdateSystemSet),
);
app.add_plugins(tnua_demos_crate::ui::DemoUi::<
CharacterMotionConfigForPlatformerDemo,
>::default());
Expand Down Expand Up @@ -364,7 +372,9 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
cmd.insert(TnuaSimpleAirActionsCounter::default());

#[cfg(feature = "egui")]
cmd.insert(tnua_demos_crate::ui::TrackedEntity("Player".to_owned()));
#[cfg(feature = "egui")]
cmd.insert(PlotSource::default());
cmd.insert((
tnua_demos_crate::ui::TrackedEntity("Player".to_owned()),
PlotSource::default(),
InfoSource::default(),
));
}
15 changes: 13 additions & 2 deletions demos/src/bin/shooter_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tnua_demos_crate::app_setup_options::{AppSetupConfiguration, ScheduleToUse};
use tnua_demos_crate::character_animating_systems::platformer_animating_systems::{
animate_platformer_character, AnimationState,
};
use tnua_demos_crate::character_control_systems::info_dumpeing_systems::character_control_info_dumping_system;
use tnua_demos_crate::character_control_systems::platformer_control_systems::{
apply_platformer_controls, CharacterMotionConfigForPlatformerDemo, FallingThroughControlScheme,
ForwardFromCamera,
Expand All @@ -31,8 +32,10 @@ use tnua_demos_crate::character_control_systems::Dimensionality;
#[cfg(feature = "xpbd3d")]
use tnua_demos_crate::levels_setup::for_3d_platformer::LayerNames;
use tnua_demos_crate::ui::component_alterbation::CommandAlteringSelectors;
use tnua_demos_crate::ui::info::InfoSource;
#[cfg(feature = "egui")]
use tnua_demos_crate::ui::plotting::PlotSource;
use tnua_demos_crate::ui::DemoInfoUpdateSystemSet;
use tnua_demos_crate::util::animating::{animation_patcher_system, GltfSceneHandler};
use tnua_demos_crate::MovingPlatformPlugin;

Expand Down Expand Up @@ -103,6 +106,11 @@ fn main() {
}
}

#[cfg(feature = "egui")]
app.add_systems(
Update,
character_control_info_dumping_system.in_set(DemoInfoUpdateSystemSet),
);
app.add_plugins(tnua_demos_crate::ui::DemoUi::<
CharacterMotionConfigForPlatformerDemo,
>::default());
Expand Down Expand Up @@ -377,9 +385,12 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
// This helper keeps track of air actions like jumps or air dashes.
cmd.insert(TnuaSimpleAirActionsCounter::default());

cmd.insert(tnua_demos_crate::ui::TrackedEntity("Player".to_owned()));
#[cfg(feature = "egui")]
cmd.insert(PlotSource::default());
cmd.insert((
tnua_demos_crate::ui::TrackedEntity("Player".to_owned()),
PlotSource::default(),
InfoSource::default(),
));
}

fn grab_ungrab_mouse(
Expand Down
42 changes: 42 additions & 0 deletions demos/src/character_control_systems/info_dumpeing_systems.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use bevy::prelude::*;
use bevy_tnua::{TnuaGhostSensor, TnuaProximitySensor};

use crate::ui::info::InfoSource;

pub fn character_control_info_dumping_system(
mut query: Query<(
&mut InfoSource,
&TnuaProximitySensor,
Option<&TnuaGhostSensor>,
)>,
names_query: Query<&Name>,
) {
for (mut info_source, sensor, ghost_sensor) in query.iter_mut() {
if !info_source.is_active() {
continue;
}
if let Some(sensor_output) = sensor.output.as_ref() {
if let Ok(name) = names_query.get(sensor_output.entity) {
info_source.label("Standing on", name.as_str());
} else {
info_source.label("Standing on", format!("{:?}", sensor_output.entity));
}
} else {
info_source.label("Standing on", "<Nothing>");
}
if let Some(ghost_sensor) = ghost_sensor.as_ref() {
let mut text = String::new();
for hit in ghost_sensor.iter() {
if !text.is_empty() {
text.push_str(", ");
}
if let Ok(name) = names_query.get(hit.entity) {
text.push_str(name.as_str());
} else {
text.push_str(&format!("{:?}", hit.entity));
}
}
info_source.label("Ghost sensor", text);
}
}
}
1 change: 1 addition & 0 deletions demos/src/character_control_systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod info_dumpeing_systems;
pub mod platformer_control_systems;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
35 changes: 26 additions & 9 deletions demos/src/levels_setup/for_2d_platformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum LayerNames {
}

pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
let mut cmd = commands.spawn_empty();
let mut cmd = commands.spawn(Name::new("Floor"));
cmd.insert(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(128.0, 0.5)),
Expand All @@ -36,20 +36,34 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
cmd.insert(xpbd::Collider::halfspace(Vector2::Y));
}

for ([width, height], transform) in [
for (name, [width, height], transform) in [
(
"Moderate Slope",
[10.0, 0.1],
Transform::from_xyz(7.0, 7.0, 0.0).with_rotation(Quat::from_rotation_z(0.6)),
),
(
"Steep Slope",
[10.0, 0.1],
Transform::from_xyz(14.0, 14.0, 0.0).with_rotation(Quat::from_rotation_z(1.0)),
),
([4.0, 2.0], Transform::from_xyz(-4.0, 1.0, 0.0)),
([6.0, 1.0], Transform::from_xyz(-10.0, 4.0, 0.0)),
([6.0, 1.0], Transform::from_xyz(-20.0, 2.6, 0.0)),
(
"Box to Step on",
[4.0, 2.0],
Transform::from_xyz(-4.0, 1.0, 0.0),
),
(
"Floating Box",
[6.0, 1.0],
Transform::from_xyz(-10.0, 4.0, 0.0),
),
(
"Box to Crawl Under",
[6.0, 1.0],
Transform::from_xyz(-20.0, 2.6, 0.0),
),
] {
let mut cmd = commands.spawn_empty();
let mut cmd = commands.spawn(Name::new(name));
cmd.insert(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(width, height)),
Expand All @@ -72,8 +86,8 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
}

// Fall-through platforms
for y in [5.0, 7.5] {
let mut cmd = commands.spawn_empty();
for (i, y) in [5.0, 7.5].into_iter().enumerate() {
let mut cmd = commands.spawn(Name::new(format!("Fall Through #{}", i + 1)));
cmd.insert(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(6.0, 0.5)),
Expand Down Expand Up @@ -104,6 +118,7 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
}

commands.spawn((
Name::new("Collision Groups"),
TransformBundle::from_transform(Transform::from_xyz(10.0, 2.0, 0.0)),
#[cfg(feature = "rapier2d")]
(
Expand Down Expand Up @@ -137,6 +152,7 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
#[cfg(feature = "rapier2d")]
{
commands.spawn((
Name::new("Solver Groups"),
TransformBundle::from_transform(Transform::from_xyz(15.0, 2.0, 0.0)),
rapier::Collider::ball(1.0),
SolverGroups {
Expand All @@ -160,6 +176,7 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
}

commands.spawn((
Name::new("Sensor"),
TransformBundle::from_transform(Transform::from_xyz(20.0, 2.0, 0.0)),
#[cfg(feature = "rapier2d")]
(rapier::Collider::ball(1.0), rapier::Sensor),
Expand All @@ -186,7 +203,7 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {

// spawn moving platform
{
let mut cmd = commands.spawn_empty();
let mut cmd = commands.spawn(Name::new("Moving Platform"));
cmd.insert(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(4.0, 1.0)),
Expand Down
38 changes: 27 additions & 11 deletions demos/src/levels_setup/for_3d_platformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn setup_level(
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
let mut cmd = commands.spawn_empty();
let mut cmd = commands.spawn(Name::new("Floor"));
cmd.insert(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(128.0, 128.0)),
material: materials.add(Color::WHITE),
Expand All @@ -39,20 +39,34 @@ pub fn setup_level(
}

let obstacles_material = materials.add(Color::GRAY);
for ([width, height, depth], transform) in [
for (name, [width, height, depth], transform) in [
(
"Moderate Slope",
[10.0, 0.1, 2.0],
Transform::from_xyz(7.0, 7.0, 0.0).with_rotation(Quat::from_rotation_z(0.6)),
),
(
"Steep Slope",
[10.0, 0.1, 2.0],
Transform::from_xyz(14.0, 14.0, 0.0).with_rotation(Quat::from_rotation_z(1.0)),
),
([4.0, 2.0, 2.0], Transform::from_xyz(-4.0, 1.0, 0.0)),
([6.0, 1.0, 2.0], Transform::from_xyz(-10.0, 4.0, 0.0)),
([6.0, 1.0, 2.0], Transform::from_xyz(0.0, 2.6, -5.0)),
(
"Box to Step on",
[4.0, 2.0, 2.0],
Transform::from_xyz(-4.0, 1.0, 0.0),
),
(
"Floating Box",
[6.0, 1.0, 2.0],
Transform::from_xyz(-10.0, 4.0, 0.0),
),
(
"Box to Crawl Under",
[6.0, 1.0, 2.0],
Transform::from_xyz(0.0, 2.6, -5.0),
),
] {
let mut cmd = commands.spawn_empty();
let mut cmd = commands.spawn(Name::new(name));
cmd.insert(PbrBundle {
mesh: meshes.add(Cuboid::new(width, height, depth)),
material: obstacles_material.clone(),
Expand All @@ -78,8 +92,8 @@ pub fn setup_level(

// Fall-through platforms
let fall_through_obstacles_material = materials.add(Color::PINK.with_a(0.8));
for y in [2.0, 4.5] {
let mut cmd = commands.spawn_empty();
for (i, y) in [2.0, 4.5].into_iter().enumerate() {
let mut cmd = commands.spawn(Name::new(format!("Fall Through #{}", i + 1)));
cmd.insert(PbrBundle {
mesh: meshes.add(Cuboid::new(6.0, 0.5, 2.0)),
material: fall_through_obstacles_material.clone(),
Expand Down Expand Up @@ -107,6 +121,7 @@ pub fn setup_level(
}

commands.spawn((
Name::new("Collision Groups"),
SceneBundle {
scene: asset_server.load("collision-groups-text.glb#Scene0"),
transform: Transform::from_xyz(10.0, 2.0, 1.0), // .with_scale(0.01 * Vec3::ONE),
Expand All @@ -130,6 +145,7 @@ pub fn setup_level(

#[cfg(feature = "rapier3d")]
commands.spawn((
Name::new("Solver Groups"),
SceneBundle {
scene: asset_server.load("solver-groups-text.glb#Scene0"),
transform: Transform::from_xyz(15.0, 2.0, 1.0), // .with_scale(0.01 * Vec3::ONE),
Expand All @@ -143,6 +159,7 @@ pub fn setup_level(
));

commands.spawn((
Name::new("Sensor"),
SceneBundle {
scene: asset_server.load("sensor-text.glb#Scene0"),
transform: Transform::from_xyz(20.0, 2.0, 1.0), // .with_scale(0.01 * Vec3::ONE),
Expand All @@ -160,8 +177,7 @@ pub fn setup_level(

// spawn moving platform
{
let mut cmd = commands.spawn_empty();

let mut cmd = commands.spawn(Name::new("Moving Platform"));
cmd.insert(PbrBundle {
mesh: meshes.add(Cuboid::new(4.0, 1.0, 4.0)),
material: materials.add(Color::BLUE),
Expand Down Expand Up @@ -194,7 +210,7 @@ pub fn setup_level(

// spawn spinning platform
{
let mut cmd = commands.spawn_empty();
let mut cmd = commands.spawn(Name::new("Spinning Platform"));

cmd.insert(PbrBundle {
mesh: meshes.add(Cylinder {
Expand Down
Loading

0 comments on commit 17c4ec5

Please sign in to comment.