diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 187 |
1 files changed, 37 insertions, 150 deletions
diff --git a/src/main.rs b/src/main.rs index 0daef91..704a276 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod camera; mod config; mod content; +mod input; mod renderer; mod sim; @@ -10,12 +11,8 @@ use rand::random_range; use std::{collections::VecDeque, sync::Arc, time::Instant}; use winit::{ application::ApplicationHandler, - event::{ - ElementState, KeyEvent, MouseButton, - WindowEvent::{self}, - }, + event::WindowEvent::{self}, event_loop::{ActiveEventLoop, ControlFlow, EventLoop}, - keyboard::{KeyCode, PhysicalKey}, window::Window, }; @@ -26,12 +23,14 @@ use crate::{ entities::{entity_cube::entity_cube_def, entity_grenade::entity_grenade_def}, materials::MaterialId, }, + input::{ + Input::{self}, + InputManager, + }, renderer::RendererState, sim::{ cell::Cell, - cell_manager::manager::CellManager, - entity::EntityId, - lib::force::{apply_bullet, apply_explosion}, + lib::force::apply_bullet, rb_manager::DebugRenderMode, sim_manager::{SimCtx, SimManager}, }, @@ -50,26 +49,6 @@ struct Config { debug_render_mode: DebugRenderMode, } -struct Input { - last_mouse_pos_on_screen: Option<(f32, f32)>, - last_mouse_world_pos: Option<(f32, f32)>, - last_mouse_chunk_pos: Option<(i32, i32)>, - last_mouse_local_pos: Option<(u8, u8)>, - is_lmb_pressed: bool, - is_rmb_pressed: bool, - trigger_test_1: bool, - trigger_test_2: bool, - trigger_test_3: bool, - trigger_test_4: bool, - trigger_test_5: bool, - - // keybindings - is_up_pressed: bool, - is_left_pressed: bool, - is_down_pressed: bool, - is_right_pressed: bool, -} - struct Diagnostics { frame_times: VecDeque<f32>, fps: f32, @@ -84,7 +63,7 @@ struct App { bullet_origin: Option<Vec2>, // game - input: Input, + input_manager: InputManager, camera: Option<Camera>, sim_manager: Option<SimManager>, @@ -102,87 +81,64 @@ impl App { fn update(&mut self, delta_time: f32) { // apply inputs if let Some(camera) = &mut self.camera { - camera.handle_camera_input(&self.input, delta_time) + camera.handle_camera_input(&self.input_manager, delta_time) } if let Some(sim) = &mut self.sim_manager { // --TEST SPAWNING-- - if self.input.trigger_test_1 - && let Some(lm) = self.input.last_mouse_world_pos - { - self.input.trigger_test_1 = false; + if self.input_manager.pressed(Input::Action1) { sim.create_entity(entity_cube_def( - Vec2::new(lm.0, lm.1), + self.input_manager.world_mouse_pos, self.config.dropper_material, )); } - if self.input.trigger_test_2 - && let Some(lm) = self.input.last_mouse_world_pos - { - self.input.trigger_test_2 = false; - sim.create_entity(entity_grenade_def(Vec2::new(lm.0, lm.1), 3.0)); + if self.input_manager.pressed(Input::Action2) { + sim.create_entity(entity_grenade_def(self.input_manager.world_mouse_pos, 3.0)); } - if self.input.trigger_test_3 - && let Some(lm) = self.input.last_mouse_world_pos - { - self.input.trigger_test_3 = false; + if self.input_manager.pressed(Input::Action3) { if let Some(origin) = self.bullet_origin { apply_bullet( &mut SimCtx { + input_manager: &self.input_manager, cell_manager: &mut sim.cell_manager, particle_manager: &mut sim.particle_manager, rb_manager: &mut sim.rb_manager, }, origin, - Vec2::new(lm.0, lm.1), + self.input_manager.world_mouse_pos, 800, ); self.bullet_origin = None; } else { - self.bullet_origin = Some(Vec2::new(lm.0, lm.1)); + self.bullet_origin = Some(self.input_manager.world_mouse_pos); } } - if self.input.trigger_test_4 - && let Some(lm) = self.input.last_mouse_world_pos - { - let mouse = Vec2::new(lm.0, lm.1); - self.input.trigger_test_4 = false; - apply_explosion( - &mut SimCtx { - cell_manager: &mut sim.cell_manager, - particle_manager: &mut sim.particle_manager, - rb_manager: &mut sim.rb_manager, - }, - mouse, - 30, - Vec2::new(0.0, -0.6), - 300.0, - ); - } - // --TEST DRAWING-- - if (self.input.is_lmb_pressed || self.input.is_rmb_pressed) - && let Some(lm) = self.input.last_mouse_world_pos - { + if self.input_manager.lmb_held || self.input_manager.rmb_held { // start with the bounding box of the drawing brush circle + some margin // clamp the bounding box to the board sie - let bb_xl = (lm.0 - self.config.brush_radius).round() as i32; - let bb_xu = (lm.0 + self.config.brush_radius).round() as i32; - let bb_yl = (lm.1 - self.config.brush_radius).round() as i32; - let bb_yu = (lm.1 + self.config.brush_radius).round() as i32; + let bb_xl = (self.input_manager.world_mouse_pos.x - self.config.brush_radius) + .round() as i32; + let bb_xu = (self.input_manager.world_mouse_pos.x + self.config.brush_radius) + .round() as i32; + let bb_yl = (self.input_manager.world_mouse_pos.y - self.config.brush_radius) + .round() as i32; + let bb_yu = (self.input_manager.world_mouse_pos.y + self.config.brush_radius) + .round() as i32; // for each point, check if the distance is less than the brush size and write the pixel for x in bb_xl..bb_xu { for y in bb_yl..bb_yu { let r = random_range(0.0..1.0); - if ((x - lm.0.round() as i32).pow(2) + (y - lm.1.round() as i32).pow(2)) + if ((x - self.input_manager.world_mouse_pos.x.round() as i32).pow(2) + + (y - self.input_manager.world_mouse_pos.y.round() as i32).pow(2)) < (self.config.brush_radius as i32).pow(2) && r > 0.9 { - let cell = if self.input.is_lmb_pressed { + let cell = if self.input_manager.lmb_held { let mut cell = Cell::from_material(self.config.brush_material); // ensure we simulate on the first tick cell.match_parity(sim.cell_manager.seqno); @@ -198,7 +154,8 @@ impl App { } } - sim.update(&self.config, delta_time); + sim.update(&self.config, &self.input_manager, delta_time); + self.input_manager.reset_for_frame(); } } } @@ -211,24 +168,7 @@ impl Default for App { bullet_origin: None, - input: Input { - last_mouse_pos_on_screen: None, - last_mouse_world_pos: None, - last_mouse_chunk_pos: None, - last_mouse_local_pos: None, - - trigger_test_1: false, - trigger_test_2: false, - trigger_test_3: false, - trigger_test_4: false, - trigger_test_5: false, - is_lmb_pressed: false, - is_rmb_pressed: false, - is_up_pressed: false, - is_left_pressed: false, - is_down_pressed: false, - is_right_pressed: false, - }, + input_manager: InputManager::new(), camera: None, @@ -290,64 +230,11 @@ impl ApplicationHandler for App { } } - match event { - WindowEvent::KeyboardInput { - event: - KeyEvent { - physical_key: PhysicalKey::Code(code), - state, - repeat, - .. - }, - .. - } => { - if let Some(sim) = &mut self.sim_manager { - let pressed = state.is_pressed(); - match code { - KeyCode::KeyW => self.input.is_up_pressed = pressed, - KeyCode::KeyA => self.input.is_left_pressed = pressed, - KeyCode::KeyS => self.input.is_down_pressed = pressed, - KeyCode::KeyD => self.input.is_right_pressed = pressed, - KeyCode::KeyC => sim.cell_manager = CellManager::from_default_size(), - KeyCode::KeyP => sim.particle_manager.particles = Vec::new(), - KeyCode::KeyV => { - let ids: Vec<EntityId> = sim.entities.keys().cloned().collect(); - for id in ids { - sim.destroy_entity(id); - } - } - KeyCode::Space if pressed => sim.paused = !sim.paused, - KeyCode::KeyX if pressed => sim.ignore_pause_next_tick = true, - KeyCode::Digit1 if pressed && !repeat => self.input.trigger_test_1 = true, - KeyCode::Digit2 if pressed && !repeat => self.input.trigger_test_2 = true, - KeyCode::Digit3 if pressed && !repeat => self.input.trigger_test_3 = true, - KeyCode::Digit4 if pressed && !repeat => self.input.trigger_test_4 = true, - KeyCode::Digit5 if pressed && !repeat => self.input.trigger_test_5 = true, - _ => {} - } - } - } - WindowEvent::CursorMoved { position, .. } => { - self.input.last_mouse_pos_on_screen = Some((position.x as f32, position.y as f32)); - - if let Some(camera) = &mut self.camera { - let world_pos = - camera.screen_position_to_world(position.x as f32, position.y as f32); + if let Some(camera) = &self.camera { + self.input_manager.apply_event(&event, &camera); + } - self.input.last_mouse_world_pos = Some(world_pos); - let ((cx, cy), (lx, ly)) = - CellManager::split_game_position(world_pos.0 as i32, world_pos.1 as i32); - self.input.last_mouse_chunk_pos = Some((cx, cy)); - self.input.last_mouse_local_pos = Some((lx, ly)); - } - } - WindowEvent::MouseInput { state, button, .. } => { - if button == MouseButton::Left { - self.input.is_lmb_pressed = state == ElementState::Pressed - } else if button == MouseButton::Right { - self.input.is_rmb_pressed = state == ElementState::Pressed - } - } + match event { WindowEvent::Resized(size) => { if let Some(renderer_state) = &mut self.renderer_state && let Some(camera) = &mut self.camera @@ -391,7 +278,7 @@ impl ApplicationHandler for App { camera, &mut self.config, &self.diagnostics, - &self.input, + &self.input_manager, ); } } |
