Skip to content

tools: Bevy features and debug console #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ authors = ["Philip Linden <lindenphilipj@gmail.com>"]
edition = "2021"

[dependencies]
log = { version = "0.4.22", features = [
log = { version = "0.4.25", features = [
# Disable low-severity logs at compile time for performance.
"max_level_debug",
"release_max_level_warn",
] }
hifitime = "4.0.1"
thiserror = "2.0.7"
bevy = { version = "0.15", default-features = false, features = [
"bevy_asset",
"bevy_state",
"bevy_window",
"multi_threaded",
]}
hifitime = "4.0.2"
bevy = "0.15.1"
bevy_console = { version = "0.13.1", optional = true }
clap = { version = "4.5.27", optional = true }

[features]
default = ["dev"]
dev = [
"bevy/dynamic_linking"
"bevy/dynamic_linking",
"bevy/bevy_dev_tools",
"bevy/bevy_debug_stepping",
"bevy_console",
"clap",
]

# Idiomatic Bevy code often triggers these lints, and the CI workflow treats them as errors.
Expand Down Expand Up @@ -58,7 +58,7 @@ codegen-units = 1
lto = "thin"
# Optimize with size in mind (also try "z", sometimes it is better).
# Slightly slows compile times, great improvements to file size and runtime performance.
opt-level = "s"
opt-level = "z"
# Strip all debugging information from the binary to slightly reduce file size.
strip = "debuginfo"

Expand Down
1 change: 1 addition & 0 deletions docs/dev/log/2025-01-06.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
One interesting analogy for gravity is a conveyor belt. the belt is moving, but if you're on the belt you move with it unless acted on by a force.
3 changes: 3 additions & 0 deletions docs/dev/log/2025-01-23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
An interesting concept is [timescapes](https://youtu.be/SXg6YVcdOcA) --- a model wherein the apparent expansion of the universe is a byproduct of time moving faster in open areas than areas with lots of matter.

 [arXiv:gr-qc/0702082v4](https://arxiv.org/abs/gr-qc/0702082v4)
6 changes: 3 additions & 3 deletions docs/src/relativity/general/00-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ following video does an excellent job of explaining it:

Video: [Gravity comes from time curving space](https://youtu.be/OpOER8Eec2A)

![alt text](/relativity/general/assets/image.png)
![alt text](./assets/image.png)

![alt text](/relativity/general/assets/image2.png)
![alt text](./assets/image2.png)

![alt text](/relativity/general/assets/image3.png)
![alt text](./assets/image3.png)

## Curved spacetime

Expand Down
15 changes: 15 additions & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use bevy::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.add_systems(Startup, setup_camera);
}

#[derive(Component)]
struct ViewportCamera;

fn setup_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
ViewportCamera,
));
}
87 changes: 87 additions & 0 deletions src/dev/console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use bevy::{
prelude::*,
log::{self, LogPlugin},
};
use bevy_console::{self, make_layer, AddConsoleCommand, ConsoleCommand, ConsoleConfiguration, ConsoleOpen};
use clap::Parser;

const OPEN_BY_DEFAULT: bool = false;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((
bevy_console::ConsolePlugin,
LogPlugin {
level: log::Level::INFO,
filter: "info,capture_bevy_logs=info".to_owned(),
custom_layer: make_layer,
}))
.insert_resource(ConsoleOpen {
open: OPEN_BY_DEFAULT,
})
.insert_resource(ConsoleConfiguration {
top_pos: 0.0,
left_pos: 5.0,
height: 500.0,
width: 1280.0,
show_title_bar: false,
..Default::default()
});

// Add commands plugins
app.add_plugins((
SpawnCommandsPlugin,
));
}

struct SpawnCommandsPlugin;

impl Plugin for SpawnCommandsPlugin {
fn build(&self, app: &mut App) {
app.add_console_command::<SpawnCommand, _>(spawn_command)
.add_console_command::<DespawnCommand, _>(despawn_command);
}
}

/// Spawns an entity of the specified type
#[derive(Parser, ConsoleCommand)]
#[command(name = "spawn")]
struct SpawnCommand {
/// Type of entity to spawn
entity_type: String,
/// Optional name for the entity
#[arg(short, long)]
name: Option<String>,
}

fn spawn_command(mut log: ConsoleCommand<SpawnCommand>, mut commands: Commands) {
if let Some(Ok(SpawnCommand { entity_type, name })) = log.take() {
match entity_type.as_str() {
"something" => {
let entity_name = name.clone().unwrap_or_else(|| entity_type.clone());
let entity = commands.spawn(
Name::new(entity_name),
).id();
log.reply_ok(format!("Spawned new {} with entity id: {:?}", entity_type, entity));
}
_ => {
log.reply_failed(format!("Unknown entity type: {}", entity_type));
}
}
}
}

#[derive(Parser, ConsoleCommand)]
#[command(name = "despawn")]
struct DespawnCommand {
/// Entity to remove
entity_id: u32,
}

fn despawn_command(mut log: ConsoleCommand<DespawnCommand>, mut commands: Commands) {
if let Some(Ok(DespawnCommand { entity_id })) = log.take() {
commands
.entity(Entity::from_raw(entity_id))
.despawn_recursive();
log.ok();
}
}
20 changes: 20 additions & 0 deletions src/dev/debug_ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use bevy::{
dev_tools::ui_debug_overlay::{DebugUiPlugin, UiDebugOptions},
input::common_conditions::input_just_pressed,
prelude::*,
};

const TOGGLE_KEY: KeyCode = KeyCode::F4;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((
DebugUiPlugin,
)).add_systems(
Update,
toggle_debug_ui.run_if(input_just_pressed(TOGGLE_KEY)),
);
}

fn toggle_debug_ui(mut options: ResMut<UiDebugOptions>) {
options.toggle();
}
9 changes: 9 additions & 0 deletions src/dev/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod console;
mod debug_ui;

use bevy::prelude::*;

pub(super) fn plugin(app: &mut App) {
console::plugin(app);
debug_ui::plugin(app);
}
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod camera;

#[cfg(feature = "dev")]
mod dev;

use bevy::{
prelude::*,
asset::AssetMetaCheck,
window::{EnabledButtons, WindowTheme},
};

pub struct AppPlugin;
Expand All @@ -21,22 +24,24 @@ impl Plugin for AppPlugin {
})
.set(WindowPlugin {
primary_window: Window {
title: "🕳️".to_string(),
title: "s p a c e t i m e".to_string(),
canvas: Some("#bevy".to_string()),
fit_canvas_to_parent: true,
prevent_default_event_handling: true,
resolution: (1640., 1019.).into(),
resizable: false,
window_theme: Some(WindowTheme::Dark),
enabled_buttons: EnabledButtons {
maximize: false,
..Default::default()
},
..default()
}
.into(),
..default()
})
.build()
.disable::<bevy::log::LogPlugin>(), // Logging is configured by the dev plugin
);

app.add_plugins((
camera::plugin,
));

#[cfg(feature = "dev")]
app.add_plugins(dev::plugin);
}
}
Loading