use bevy::ecs::system::SystemId;
use bevy::prelude::*;
use bevy::window::WindowResized;
use some_bevy_tools::sbs_3d;
use some_bevy_tools::split_screen;
pub fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(sbs_3d::Sbs3DPlugin)
.add_systems(Startup, (setup_sbs, setup_object, register_systems))
.add_systems(Update, (rotate, switch_state))
.run();
}
#[derive(Component)]
struct Rotate;
#[derive(Component)]
struct SbsCamera;
#[derive(Component)]
struct SingleCamera;
#[derive(Resource, Default)]
enum SbsState {
#[default]
Enabled,
Disabled,
}
#[derive(Resource)]
struct SystemIds {
setup_sbs: SystemId,
cleanup_sbs: SystemId,
setup_single: SystemId,
cleanup_single: SystemId,
}
fn register_systems(world: &mut World) {
let setup_sbs = world.register_system(setup_sbs);
let cleanup_sbs = world.register_system(cleanup_sbs);
let setup_single = world.register_system(setup_single);
let cleanup_single = world.register_system(cleanup_single);
world.insert_resource(SystemIds {
setup_sbs,
cleanup_sbs,
setup_single,
cleanup_single,
});
}
fn setup_sbs(
mut commands: Commands,
window_query: Query<(Entity, &Window)>,
mut window_resized: EventWriter<WindowResized>,
) {
commands.spawn((
Camera3dBundle::default(),
split_screen::LeftCamera,
SbsCamera,
));
commands.spawn((
Camera3dBundle::default(),
split_screen::RightCamera,
SbsCamera,
));
commands.spawn((
sbs_3d::SbsCameraBundle::from_transform_and_gap(
Transform::from_xyz(1.0, 1.5, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
0.3,
),
SbsCamera,
));
if let Ok((entity, window)) = window_query.get_single() {
window_resized.send(WindowResized {
window: entity,
width: window.width(),
height: window.height(),
});
}
}
fn cleanup_sbs(mut commands: Commands, sbs_cameras: Query<Entity, With<SbsCamera>>) {
for entity in sbs_cameras.iter() {
commands.entity(entity).despawn_recursive();
}
}
fn setup_single(mut commands: Commands) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(1.0, 1.5, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
},
SingleCamera,
));
}
fn cleanup_single(mut commands: Commands, single_cameras: Query<Entity, With<SingleCamera>>) {
for entity in single_cameras.iter() {
commands.entity(entity).despawn_recursive();
}
}
fn setup_object(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let debug_material = materials.add(StandardMaterial { ..default() });
commands.spawn((
PbrBundle {
mesh: meshes.add(Cuboid::default()),
material: debug_material,
..Default::default()
},
Rotate,
));
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
intensity: 10_000_000.,
range: 100.0,
..default()
},
transform: Transform::from_xyz(8.0, 16.0, 8.0),
..default()
});
}
fn rotate(time: Res<Time>, mut query: Query<&mut Transform, With<Rotate>>) {
for mut transform in query.iter_mut() {
transform.rotate_y(time.delta_seconds());
}
}
fn switch_state(
mut commands: Commands,
input: Res<ButtonInput<KeyCode>>,
mut state: Local<SbsState>,
system_ids: Res<SystemIds>,
) {
if input.just_pressed(KeyCode::Space) {
match *state {
SbsState::Enabled => {
commands.run_system(system_ids.cleanup_sbs);
commands.run_system(system_ids.setup_single);
*state = SbsState::Disabled;
}
SbsState::Disabled => {
commands.run_system(system_ids.cleanup_single);
commands.run_system(system_ids.setup_sbs);
*state = SbsState::Enabled
}
}
}
}