Skip to content

Commit

Permalink
refactor: beet_examples directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent 4d832d4 commit 96ca10b
Show file tree
Hide file tree
Showing 42 changed files with 425 additions and 204 deletions.
267 changes: 131 additions & 136 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ net = ["dep:beet_net"]
ui = ["beet_core/ui"]
assets = ["beet_core/assets"]
animation = ["beet_core/animation"]
reflect = ["beet_ecs/reflect"]
tokio = ["beet_net/tokio", "beet_examples/tokio"]

# dynamic_linking = ["bevy/dynamic_linking"]
Expand Down Expand Up @@ -106,15 +107,15 @@ beet_server = { path = "crates/beet_server", version = "0.0.1" }
beet_examples = { path = "crates/beet_examples", version = "0.0.1" }

## local
# forky_core = { path = "../forky/crates/forky/forky_core" }
# forky_bevy = { path = "../forky/crates/forky/forky_bevy" }
# forky_web = { path = "../forky/crates/forky/forky_web", default-features = false }
# sweet = { path = "../forky/crates/sweet", features = ["bevy"] }
forky_core = { path = "../forky/crates/forky/forky_core" }
forky_bevy = { path = "../forky/crates/forky/forky_bevy" }
forky_web = { path = "../forky/crates/forky/forky_web", default-features = false }
sweet = { path = "../forky/crates/sweet", features = ["bevy"] }

forky_core = "0.1.49"
forky_bevy = "0.1.49"
forky_web = "0.1.49"
sweet = { version = "0.1.49", features = ["bevy"] }
# forky_core = "0.1.49"
# forky_bevy = "0.1.49"
# forky_web = "0.1.49"
# sweet = { version = "0.1.49", features = ["bevy"] }

## logging
log = "0.4"
Expand Down Expand Up @@ -165,12 +166,7 @@ bincode = "1"
ron = "0.8"

## game
# two requirements for back to release:
# 1. recursive type registration https://github.com/bevyengine/bevy/tree/d3e44325b45daf13087a6fe7a425b1744b5f2379
# 2. remove_by_id https://github.com/bevyengine/bevy/pull/9663
# bevy = { path = "../bevy", default-features = false, features = ["bevy_scene"] }
# bevy = { git = "https://github.com/bevyengine/bevy.git", rev = "f516de456be2b65572de1af6956d52157663b9cc", default-features = false, features = [
bevy = { version = "=0.14.0-rc.2", default-features = false, features = [
bevy = { version = "=0.14.0-rc.4", default-features = false, features = [
"bevy_scene",
"serialize",
# "bevy_color",
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_ecs/src/lifecycle/lifecycle_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Plugin for LifecyclePlugin {
InsertInDuration<RunResult>,
InsertOnRun<RunResult>,
LogOnRun,
CallOnRun,
// CallOnRun,
Repeat,
SetOnSpawn<Score>,
// selectors
Expand Down
10 changes: 8 additions & 2 deletions crates/beet_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ tokio = ["beet/tokio"]

[dependencies]
forky_core.workspace = true
beet.workspace = true
beet = { workspace = true }
# beet = { workspace = true, features = ["reflect"] }
bevy = { workspace = true, default-features = true }
log.workspace = true
pretty_env_logger.workspace = true
Expand All @@ -30,4 +31,9 @@ web-sys.workspace = true
anyhow.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio.workspace = true
tokio.workspace = true


[[example]]
name = "basics"
path = "examples/basics/main.rs"
18 changes: 18 additions & 0 deletions crates/beet_examples/examples/basics/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! In this example we will create an action
//! and then combine it with some built-in actions to run a behavior.
use beet::prelude::*;
use beet_examples::prelude::*;
use bevy::prelude::*;
mod scenes;

fn main() {
let mut app = App::new();

app.add_plugins((
ExamplePluginText::default(),
DefaultBeetPlugins::default(),
))
.add_systems(Startup, scenes::hello_world)
.add_systems(PostStartup, save_scene("target/scenes/hello_world.ron"))
.run();
}
1 change: 1 addition & 0 deletions crates/beet_examples/examples/basics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod scenes;
17 changes: 17 additions & 0 deletions crates/beet_examples/examples/basics/scenes/hello_world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use beet::prelude::*;
use bevy::prelude::*;

pub fn hello_world(mut commands: Commands) {
commands
.spawn((SequenceSelector::default(), Running))
.with_children(|parent| {
parent.spawn((
LogOnRun("Hello".into()),
InsertOnRun(RunResult::Success),
));
parent.spawn((
LogOnRun("World".into()),
InsertOnRun(RunResult::Success),
));
});
}
3 changes: 3 additions & 0 deletions crates/beet_examples/examples/basics/scenes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod hello_world;
#[allow(unused_imports)]
pub use self::hello_world::*;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prelude::*;
use beet::prelude::*;
use bevy::prelude::*;

pub fn bee_bundle() -> impl Bundle {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
67 changes: 67 additions & 0 deletions crates/beet_examples/src/components/log_to_ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::prelude::DoNotSerialize;
use beet::prelude::LogOnRun;
use beet::prelude::Running;
use bevy::prelude::*;

#[derive(Debug, Default, Component)]
pub struct LogToUi;



pub fn log_to_ui(
actions: Query<&LogOnRun, Added<Running>>,
mut query: Query<&mut Text, With<LogToUi>>,
) {
for mut text in query.iter_mut() {
for log in actions.iter() {
text.sections[0].value.push_str(&log.0);
text.sections[0].value.push_str("\n");
}
}
}

const TEST: &str = r#"
val 1
val 2
val 3
val 4
val 5
val 6
val 7
val 8
val 9
val 10
val 11
"#;


pub fn spawn_log_to_ui(mut commands: Commands) {
commands
.spawn((
NodeBundle {
style: Style {
flex_direction: FlexDirection::ColumnReverse,
// align_items: AlignItems::Center,
..default()
},
..default()
},
// ScrollingList::default(),
// AccessibilityNode(NodeBuilder::new(Role::List)),
))
.with_children(|parent| {
parent.spawn((
DoNotSerialize,
LogToUi,
TextBundle::from_sections([TextSection::new(
TEST,
TextStyle {
// This font is loaded and will be used instead of the default font.
// font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
..default()
},
)]),
));
});
}
30 changes: 30 additions & 0 deletions crates/beet_examples/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub mod auto_spawn;
#[allow(unused_imports)]
pub use self::auto_spawn::*;
pub mod beet_finished_loading;
#[allow(unused_imports)]
pub use self::beet_finished_loading::*;
pub mod bundles;
#[allow(unused_imports)]
pub use self::bundles::*;
pub mod camera_distance;
#[allow(unused_imports)]
pub use self::camera_distance::*;
pub mod dialog_panel;
#[allow(unused_imports)]
pub use self::dialog_panel::*;
pub mod follow_cursor;
#[allow(unused_imports)]
pub use self::follow_cursor::*;
pub mod log_to_ui;
#[allow(unused_imports)]
pub use self::log_to_ui::*;
pub mod randomize_position;
#[allow(unused_imports)]
pub use self::randomize_position::*;
pub mod render_text;
#[allow(unused_imports)]
pub use self::render_text::*;
pub mod wrap_around;
#[allow(unused_imports)]
pub use self::wrap_around::*;
File renamed without changes.
File renamed without changes.
39 changes: 15 additions & 24 deletions crates/beet_examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
// #![allow(unused, dead_code)]
mod beet_finished_loading;
mod camera_distance;

#[cfg(target_arch = "wasm32")]
mod postmessage_input;
pub mod wasm;
#[cfg(target_arch = "wasm32")]
pub use postmessage_input::*;
pub use wasm::*;
pub mod serde_utils;

pub mod components;
pub mod plugins;


mod example_plugin;
mod example_plugin_2d;
mod example_plugin_3d;
pub use example_plugin::*;
pub use example_plugin_2d::*;
pub use example_plugin_3d::*;
mod dialog_panel;
pub use beet_finished_loading::*;
pub use camera_distance::*;
pub use dialog_panel::*;
mod auto_spawn;
pub use auto_spawn::*;
mod follow_cursor;
pub use follow_cursor::*;
mod randomize_position;
pub use randomize_position::*;
mod render_text;
pub use render_text::*;
mod wrap_around;
pub use wrap_around::*;
pub mod prelude {
pub use crate::components::*;
pub use crate::plugins::*;
pub use crate::serde_utils::*;
#[cfg(target_arch = "wasm32")]
pub use wasm::*;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::OnPlayerMessage;
use crate::prelude::*;
use beet::prelude::*;
use bevy::asset::AssetMetaCheck;
use bevy::prelude::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::example_plugin::ExamplePlugin;
use crate::*;
use crate::prelude::*;
use beet::prelude::*;
use bevy::prelude::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::prelude::*;
use bevy::pbr::CascadeShadowConfigBuilder;
use bevy::prelude::*;
use std::f32::consts::PI;
Expand Down
23 changes: 23 additions & 0 deletions crates/beet_examples/src/plugins/example_plugin_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::prelude::*;
use bevy::prelude::*;

pub struct ExamplePluginText {}



impl Default for ExamplePluginText {
fn default() -> Self { Self {} }
}


impl Plugin for ExamplePluginText {
fn build(&self, app: &mut App) {
app.add_plugins(ExamplePlugin)
.add_systems(Startup, (setup, spawn_log_to_ui))
.add_systems(Update, log_to_ui);
}
}

fn setup(mut commands: Commands) {
commands.spawn((DoNotSerialize, Camera2dBundle::default()));
}
12 changes: 12 additions & 0 deletions crates/beet_examples/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod example_plugin;
#[allow(unused_imports)]
pub use self::example_plugin::*;
pub mod example_plugin_2d;
#[allow(unused_imports)]
pub use self::example_plugin_2d::*;
pub mod example_plugin_3d;
#[allow(unused_imports)]
pub use self::example_plugin_3d::*;
pub mod example_plugin_text;
#[allow(unused_imports)]
pub use self::example_plugin_text::*;
3 changes: 3 additions & 0 deletions crates/beet_examples/src/serde_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod save_scene;
#[allow(unused_imports)]
pub use self::save_scene::*;
57 changes: 57 additions & 0 deletions crates/beet_examples/src/serde_utils/save_scene.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;
use bevy::tasks::IoTaskPool;
use std::fs::File;
use std::fs::{
self,
};
use std::io::Write;


#[derive(Component)]
pub struct DoNotSerialize;

fn entities_to_serialize(world: &World) -> Vec<Entity> {
world
.iter_entities()
.map(|entity| entity.id())
.filter(|entity| world.get::<DoNotSerialize>(*entity).is_none())
.collect()
}


pub fn save_scene(filename: &'static str) -> SystemConfigs {
(move |world: &mut World| {
let scene = DynamicSceneBuilder::from_world(world)
.extract_entities(entities_to_serialize(world).into_iter())
.build();

// Scenes can be serialized like this:
let type_registry = world.resource::<AppTypeRegistry>();
let type_registry = type_registry.read();
let serialized_scene = scene.serialize(&type_registry).unwrap();

// Showing the scene in the console
info!("{}", serialized_scene);

// Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system
// as they are blocking
// This can't work in WASM as there is no filesystem access
#[cfg(not(target_arch = "wasm32"))]
IoTaskPool::get()
.spawn(async move {
let dir_path = std::path::Path::new(filename).parent().unwrap();
fs::create_dir_all(dir_path)
.expect("Error while creating directory");

// Write the scene RON data to file
File::create(filename)
.and_then(|mut file| {
file.write(serialized_scene.as_bytes())
})
.expect("Error while writing scene to file");
})
.detach();
})
.into_configs()
}
3 changes: 3 additions & 0 deletions crates/beet_examples/src/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod postmessage_input;
#[allow(unused_imports)]
pub use self::postmessage_input::*;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(target_arch = "wasm32")]
use crate::OnPlayerMessage;
use crate::prelude::*;
use bevy::prelude::*;
use flume::Receiver;
use wasm_bindgen::prelude::Closure;
Expand Down
Loading

0 comments on commit 96ca10b

Please sign in to comment.