| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use bevy::prelude::*; |
| 11 |
use bevy::render::{ |
| 12 |
extract_component::{ExtractComponent, ExtractComponentPlugin}, |
| 13 |
sync_world::{RenderEntity, SyncToRenderWorld}, |
| 14 |
Extract, Render, RenderApp, |
| 15 |
}; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[derive(Component, Clone, ExtractComponent, Debug)] |
| 24 |
#[extract_app(RenderApp)] |
| 25 |
struct A(pub f32); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#[derive(Component, Clone, Debug)] |
| 31 |
#[require(SyncToRenderWorld)] |
| 32 |
struct B(pub f32); |
| 33 |
|
| 34 |
|
| 35 |
#[derive(Component, Clone, Debug)] |
| 36 |
#[require(SyncToRenderWorld)] |
| 37 |
struct C(pub f32); |
| 38 |
|
| 39 |
|
| 40 |
#[derive(Message)] |
| 41 |
struct ExtractMessage; |
| 42 |
|
| 43 |
|
| 44 |
#[derive(Resource)] |
| 45 |
struct WorldName(pub String); |
| 46 |
|
| 47 |
fn main() { |
| 48 |
let mut app = App::new(); |
| 49 |
|
| 50 |
|
| 51 |
app.insert_resource(WorldName("Main World".into())) |
| 52 |
.add_plugins(( |
| 53 |
DefaultPlugins, |
| 54 |
|
| 55 |
ExtractComponentPlugin::<A>::default(), |
| 56 |
)) |
| 57 |
.add_message::<ExtractMessage>() |
| 58 |
.add_systems(Startup, setup) |
| 59 |
.add_systems(Update, (set_time, trigger_extraction, display_state)); |
| 60 |
|
| 61 |
let Some(render_app) = app.get_sub_app_mut(RenderApp) else { |
| 62 |
return; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
render_app |
| 67 |
.insert_resource(WorldName("Render World".into())) |
| 68 |
.add_systems(ExtractSchedule, extract_components) |
| 69 |
.add_systems(Render, display_state); |
| 70 |
|
| 71 |
app.run(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
fn setup(mut commands: Commands, time: Res<Time>) { |
| 76 |
commands.spawn(A(time.elapsed_secs())); |
| 77 |
commands.spawn(B(time.elapsed_secs())); |
| 78 |
commands.spawn(C(time.elapsed_secs())); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
fn set_time(mut a: Single<&mut A>, mut b: Single<&mut B>, mut c: Single<&mut C>, time: Res<Time>) { |
| 83 |
a.0 = time.elapsed_secs(); |
| 84 |
b.0 = time.elapsed_secs(); |
| 85 |
c.0 = time.elapsed_secs(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
fn display_state( |
| 90 |
a: Option<Single<(Entity, &A)>>, |
| 91 |
b: Option<Single<(Entity, &B)>>, |
| 92 |
c: Option<Single<(Entity, &C)>>, |
| 93 |
|
| 94 |
|
| 95 |
world_name: Res<WorldName>, |
| 96 |
) { |
| 97 |
let (a, b, c) = ( |
| 98 |
a.map(Single::into_inner), |
| 99 |
b.map(Single::into_inner), |
| 100 |
c.map(Single::into_inner), |
| 101 |
); |
| 102 |
info!(?a, ?b, ?c, "{: >12}", world_name.0); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
fn trigger_extraction(mut writer: MessageWriter<ExtractMessage>, keys: Res<ButtonInput<KeyCode>>) { |
| 107 |
if keys.pressed(KeyCode::Space) { |
| 108 |
writer.write(ExtractMessage); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
fn extract_components( |
| 115 |
b: Extract<Query<(RenderEntity, &B)>>, |
| 116 |
c: Extract<Query<(RenderEntity, &C)>>, |
| 117 |
mut reader: Extract<MessageReader<ExtractMessage>>, |
| 118 |
mut commands: Commands, |
| 119 |
) { |
| 120 |
for (entity, b) in &b { |
| 121 |
commands.entity(entity).insert(b.clone()); |
| 122 |
} |
| 123 |
|
| 124 |
if !reader.is_empty() { |
| 125 |
for (entity, c) in &c { |
| 126 |
commands.entity(entity).insert(c.clone()); |
| 127 |
} |
| 128 |
reader.clear(); |
| 129 |
} |
| 130 |
} |