Fix 3d -> 2d switch

This commit is contained in:
hodasemi 2023-05-14 16:15:17 +02:00
parent 5b11cd45eb
commit c7bff62b8c
3 changed files with 57 additions and 1 deletions

View file

@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
bevy = "0.10.1"
bevy_egui = "0.20.3"
bevy-inspector-egui = "0.18.3"

View file

@ -7,12 +7,23 @@ pub enum Camera {
}
impl Camera {
pub fn is_3d(&self) -> bool {
match self {
Camera::TwoD(_) => false,
Camera::ThreeD(_) => true,
}
}
pub fn is_2d(&self) -> bool {
!self.is_3d()
}
fn create_2d(cmd: &mut Commands) -> Self {
Self::TwoD(cmd.spawn(Camera2dBundle::default()).id())
}
fn create_3d(cmd: &mut Commands, window_extent: Vec2) -> Self {
Self::TwoD(
Self::ThreeD(
cmd.spawn(Camera3dBundle {
transform: Transform::from_xyz(200.0, -600.0, 600.0)
.looking_at(window_extent.extend(0.0), Vec3::Z),
@ -77,3 +88,39 @@ impl Default for GlobalState {
}
}
}
#[cfg(test)]
mod test {
use bevy::{ecs::system::CommandQueue, prelude::*};
use super::GlobalState;
#[test]
fn camera_conversion() {
let world = World::new();
let mut command_queue = CommandQueue::default();
let mut cmd = Commands::new(&mut command_queue, &world);
// default global state
let mut global_state = GlobalState::default();
// create 2d cam
global_state.setup_2d_cam(&mut cmd);
assert!(global_state.camera.is_some());
assert!(global_state.camera.as_ref().unwrap().is_2d());
// switch to 3d cam
global_state.setup_3d_cam(&mut cmd);
assert!(global_state.camera.is_some());
assert!(global_state.camera.as_ref().unwrap().is_3d());
// switch to 2d cam
global_state.setup_2d_cam(&mut cmd);
assert!(global_state.camera.is_some());
assert!(global_state.camera.as_ref().unwrap().is_2d());
}
}

View file

@ -11,6 +11,7 @@ use bevy::{
};
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use global_state::GlobalState;
use polygon::ObjectInfos;
@ -18,6 +19,7 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(EguiPlugin)
.add_plugin(WorldInspectorPlugin::new())
.insert_resource(GlobalState::default())
.insert_resource(ObjectInfos::default())
.add_startup_system(setup_camera)
@ -57,6 +59,9 @@ fn update_ui_side_panel(
});
global_state.setup_3d_cam(&mut commands);
debug_assert!(global_state.camera.is_some());
debug_assert!(global_state.camera.as_ref().unwrap().is_3d());
}
} else {
if ui.button("Change to 2D View").clicked() {
@ -72,6 +77,9 @@ fn update_ui_side_panel(
});
global_state.setup_2d_cam(&mut commands);
debug_assert!(global_state.camera.is_some());
debug_assert!(global_state.camera.as_ref().unwrap().is_2d());
}
}