Synced resource example
parent
c08abc5b67
commit
93814e9841
@ -0,0 +1,50 @@
|
|||||||
|
use bevy::input::mouse::MouseMotion;
|
||||||
|
use games::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
App::new()
|
||||||
|
.init_resource::<Thing>()
|
||||||
|
.add_plugins(BaseGamePlugin { camera: CameraType::Camera3d })
|
||||||
|
.add_systems(Startup, init_ui)
|
||||||
|
.add_systems(Update, (
|
||||||
|
sync_resource_to_ui::<Thing>.run_if(resource_changed::<Thing>),
|
||||||
|
update_foo.run_if(on_event::<MouseMotion>)
|
||||||
|
))
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource, Debug, Default)]
|
||||||
|
struct Thing(u8);
|
||||||
|
|
||||||
|
impl Display for Thing {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_ui(
|
||||||
|
mut commands: Commands
|
||||||
|
) {
|
||||||
|
commands.spawn((
|
||||||
|
Node {
|
||||||
|
align_self: AlignSelf::Start,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Text("Displays a sync'd resource tracking the number of mouse events (mod 256)".into()),
|
||||||
|
));
|
||||||
|
commands.spawn((Node {
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
..default()
|
||||||
|
}, Text("Placeholder".into()), SyncResource::<Thing>::default()));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_foo(
|
||||||
|
mut events: EventReader<MouseMotion>,
|
||||||
|
mut thing: ResMut<Thing>,
|
||||||
|
) {
|
||||||
|
events.read().for_each(|_| {
|
||||||
|
thing.0 = thing.0.overflowing_add(1).0;
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,16 +0,0 @@
|
|||||||
use games::*;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
App::new()
|
|
||||||
.add_plugins(BaseGamePlugin { camera: CameraType::Camera3d })
|
|
||||||
.add_systems(Update, (
|
|
||||||
f.run_if(any_component_added::<Foo>),
|
|
||||||
f.run_if(any_component_changed::<Foo>),
|
|
||||||
))
|
|
||||||
.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
struct Foo;
|
|
||||||
|
|
||||||
fn f() { }
|
|
||||||
@ -1,9 +1,13 @@
|
|||||||
mod base_game;
|
mod base_game;
|
||||||
mod debug;
|
mod debug;
|
||||||
mod scheduling;
|
mod scheduling;
|
||||||
|
mod ui;
|
||||||
|
|
||||||
pub use bevy::{prelude::*, input::{keyboard::KeyboardInput, ButtonState}};
|
pub use std::fmt::Display;
|
||||||
|
|
||||||
|
pub use bevy::{prelude::*, input::{keyboard::KeyboardInput, ButtonState}, color::palettes::css::{BLACK, RED, WHITE}};
|
||||||
|
|
||||||
pub use base_game::*;
|
pub use base_game::*;
|
||||||
pub use debug::*;
|
pub use debug::*;
|
||||||
pub use scheduling::*;
|
pub use scheduling::*;
|
||||||
|
pub use ui::*;
|
||||||
|
|||||||
@ -0,0 +1,17 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// Marker component for handling Resource -> Ui Sync
|
||||||
|
#[derive(Component, Default, Debug)]
|
||||||
|
pub struct SyncResource<R: Resource + Default + Display>(R);
|
||||||
|
|
||||||
|
/// Sync a generic resource to the UI
|
||||||
|
///
|
||||||
|
/// Mostly useful for quick n' dirty getting data to the user
|
||||||
|
pub fn sync_resource_to_ui<R: Resource + Default + Display>(
|
||||||
|
mut q: Query<&mut Text, With<SyncResource<R>>>,
|
||||||
|
r: Res<R>,
|
||||||
|
) {
|
||||||
|
q.iter_mut().for_each(|mut t| {
|
||||||
|
t.0 = format!("{}", *r);
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue