Move input_manager and camera as resource
This commit is contained in:
parent
6ba61e040e
commit
07056fc0ce
5 changed files with 72 additions and 68 deletions
|
@ -5,6 +5,7 @@ use super::settings_scene::SettingsScene;
|
|||
use crate::core::app::DEPTH_IMAGE_ID;
|
||||
use crate::core::app::context::WindowContext;
|
||||
use crate::core::app::user_event::UserEvent;
|
||||
use crate::core::input::InputManagerResource;
|
||||
use crate::core::render::primitives::camera::Camera3D;
|
||||
use crate::core::render::primitives::transform::Transform;
|
||||
use crate::core::render::primitives::velocity::Velocity;
|
||||
|
@ -44,11 +45,12 @@ pub struct Cube;
|
|||
pub struct MainSceneState {
|
||||
square: SquareMesh,
|
||||
obj: ObjMesh,
|
||||
camera: Camera3D,
|
||||
speed: f32,
|
||||
scheduler: Schedule,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct CameraSpeed(f32);
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MainScene {
|
||||
state: Option<MainSceneState>,
|
||||
|
@ -115,19 +117,19 @@ impl AsScene for MainScene {
|
|||
1000.0,
|
||||
)
|
||||
});
|
||||
world.insert_resource(CameraSpeed(50.0));
|
||||
world.insert_resource(camera);
|
||||
|
||||
let mut scheduler = Schedule::default();
|
||||
scheduler.add_systems(update_velocity_system);
|
||||
scheduler.add_systems(update_timer_system);
|
||||
|
||||
scheduler.add_systems(update_camera_system);
|
||||
world.insert_resource(Timer::new());
|
||||
Self::create_entities(world, 100, 10.0, 10.0);
|
||||
|
||||
self.state = Some(MainSceneState {
|
||||
square,
|
||||
obj,
|
||||
camera,
|
||||
speed: 50.0,
|
||||
scheduler,
|
||||
});
|
||||
|
||||
|
@ -141,50 +143,41 @@ impl AsScene for MainScene {
|
|||
) -> Result<(), Box<dyn Error>> {
|
||||
let state = self.state.as_mut().unwrap();
|
||||
|
||||
window_context.with_input_manager(|input_manager| {
|
||||
window_context.with_timer(|timer| {
|
||||
state.camera.update(
|
||||
input_manager,
|
||||
timer,
|
||||
state.speed,
|
||||
10.0,
|
||||
window_context.get_aspect_ratio(),
|
||||
);
|
||||
});
|
||||
});
|
||||
{
|
||||
let mut camera = world.resource_mut::<Camera3D>();
|
||||
camera.update_projection(window_context.get_aspect_ratio());
|
||||
}
|
||||
|
||||
{
|
||||
let input_manager = world.resource::<InputManagerResource>().0.read().unwrap();
|
||||
|
||||
if input_manager.get_virtual_input_state("mouse_left") > 0.0 {
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorVisible(window_context.window_id, false));
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorGrabMode(
|
||||
window_context.window_id,
|
||||
CursorGrabMode::Locked,
|
||||
));
|
||||
}
|
||||
|
||||
if input_manager.get_virtual_input_state("mouse_right") > 0.0 {
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorVisible(window_context.window_id, true));
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorGrabMode(
|
||||
window_context.window_id,
|
||||
CursorGrabMode::None,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
state.scheduler.run(world);
|
||||
|
||||
if window_context
|
||||
.with_input_manager(|input_manager| input_manager.get_virtual_input_state("mouse_left"))
|
||||
> 0.0
|
||||
{
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorVisible(window_context.window_id, false));
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorGrabMode(
|
||||
window_context.window_id,
|
||||
CursorGrabMode::Locked,
|
||||
));
|
||||
}
|
||||
|
||||
if window_context.with_input_manager(|input_manager| {
|
||||
input_manager.get_virtual_input_state("mouse_right")
|
||||
}) > 0.0
|
||||
{
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorVisible(window_context.window_id, true));
|
||||
let _ = window_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorGrabMode(
|
||||
window_context.window_id,
|
||||
CursorGrabMode::None,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -220,8 +213,8 @@ impl AsScene for MainScene {
|
|||
|
||||
// Create camera uniform using the actual camera
|
||||
let camera_uniform = Arc::new(
|
||||
state
|
||||
.camera
|
||||
world
|
||||
.resource::<Camera3D>()
|
||||
.create_buffer(VulkanMemoryAllocator::get_from_world(world))?,
|
||||
);
|
||||
let square_transforms = world
|
||||
|
@ -285,13 +278,15 @@ impl AsScene for MainScene {
|
|||
|
||||
let swapchain_image_view =
|
||||
window_context.with_renderer(|renderer| renderer.swapchain_image_view().clone());
|
||||
let input_manager_status =
|
||||
window_context.with_input_manager(|input_manager| format!("{:#?}", input_manager));
|
||||
let input_manager_status = {
|
||||
let input_manager = world.resource::<InputManagerResource>().0.read().unwrap();
|
||||
format!("{:#?}", input_manager)
|
||||
};
|
||||
let event_loop_proxy = window_context.event_loop_proxy.clone();
|
||||
let delta_time = window_context.get_delta_time();
|
||||
let window_id = window_context.window_id;
|
||||
let window_size = window_context.get_window_size();
|
||||
|
||||
let camera = world.resource::<Camera3D>();
|
||||
let render_future = window_context.with_gui_mut(|gui| {
|
||||
gui.immediate_ui(|gui| {
|
||||
let ctx = gui.context();
|
||||
|
@ -323,7 +318,7 @@ impl AsScene for MainScene {
|
|||
ui.separator();
|
||||
|
||||
ui.label("Position caméra:");
|
||||
let position = state.camera.get_position();
|
||||
let position = camera.get_position();
|
||||
ui.label(format!(" X: {:.2}", position[0]));
|
||||
ui.label(format!(" Y: {:.2}", position[1]));
|
||||
ui.label(format!(" Z: {:.2}", position[2]));
|
||||
|
@ -331,7 +326,7 @@ impl AsScene for MainScene {
|
|||
ui.separator();
|
||||
|
||||
ui.label("Rotation caméra:");
|
||||
let rotation = state.camera.get_rotation();
|
||||
let rotation = camera.get_rotation();
|
||||
ui.label(format!(" Yaw: {:.2}°", rotation.y.to_degrees()));
|
||||
ui.label(format!(" Pitch: {:.2}°", rotation.x.to_degrees()));
|
||||
|
||||
|
@ -437,3 +432,13 @@ fn update_velocity_system(mut query: Query<(&mut Transform, &Velocity)>, time: R
|
|||
fn update_timer_system(mut timer: ResMut<Timer>) {
|
||||
timer.update();
|
||||
}
|
||||
|
||||
fn update_camera_system(
|
||||
mut camera: ResMut<Camera3D>,
|
||||
input_manager: Res<InputManagerResource>,
|
||||
timer: Res<Timer>,
|
||||
camera_speed: Res<CameraSpeed>,
|
||||
) {
|
||||
let input_manager = input_manager.0.read().unwrap();
|
||||
camera.update(&input_manager, &timer, camera_speed.0, 10.0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue