diff --git a/src/actors.rs b/src/actors.rs index 1b21847..37953f7 100644 --- a/src/actors.rs +++ b/src/actors.rs @@ -9,7 +9,7 @@ pub struct Actor { } //Called before simulations -pub fn add_actors( +pub fn fill_actors( mut chunk_manager: ResMut, actors: Query<&Actor>, mut dirty_rects: ResMut, @@ -33,7 +33,7 @@ pub fn add_actors( } //Called after simulation, before actor update -pub fn remove_actors( +pub fn unfill_actors( mut chunk_manager: ResMut, actors: Query<&Actor>, materials: (Res>, Res), @@ -246,18 +246,9 @@ impl Plugin for ActorsPlugin { app.add_systems( FixedUpdate, ( - add_actors - .before(chunk_manager_update) - .before(remove_actors) - .before(update_actors), - remove_actors - .after(chunk_manager_update) - .after(add_actors) - .before(update_actors), - update_actors - .after(remove_actors) - .after(add_actors) - .after(chunk_manager_update), + fill_actors.before(chunk_manager_update), + unfill_actors.after(chunk_manager_update), + update_actors.after(unfill_actors), ), ); } diff --git a/src/animation.rs b/src/animation.rs index 7856e0e..dbdf306 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -34,6 +34,6 @@ fn animate_sprite( pub struct AnimationPlugin; impl Plugin for AnimationPlugin { fn build(&self, app: &mut App) { - app.add_systems(FixedUpdate, animate_sprite.after(update_player_sprite)); + app.add_systems(FixedUpdate, animate_sprite.after(update_player)); } } diff --git a/src/particles.rs b/src/particles.rs index b5eb5e1..860b10e 100644 --- a/src/particles.rs +++ b/src/particles.rs @@ -241,8 +241,8 @@ impl Plugin for ParticlesPlugin { app.add_systems( FixedUpdate, ( - hydrate_particles.before(update_particles), - update_particles.after(chunk_manager_update), + hydrate_particles.after(update_player), + update_particles.before(chunk_manager_update), ), ); } diff --git a/src/player.rs b/src/player.rs index 046af63..057de14 100644 --- a/src/player.rs +++ b/src/player.rs @@ -112,7 +112,7 @@ pub fn player_setup( /// Updates player pub fn update_player( - input: (ResMut>, EventReader), + input: (Res, EventReader), mut player: Query<(&mut Actor, &mut Player, &mut AnimationIndices)>, chunk_manager: ResMut, materials: (Res>, Res), @@ -120,7 +120,7 @@ pub fn update_player( mut zoom: ResMut, ) { let (mut actor, mut player, mut anim_idxs) = player.single_mut(); - let (keys, mut scroll_evr) = input; + let (inputs, mut scroll_evr) = input; let materials = materials.0.get(materials.1 .0.clone()).unwrap(); // Gravity @@ -134,7 +134,7 @@ pub fn update_player( } // Movement - let x = -(keys.pressed(KeyCode::A) as u8 as f32) + keys.pressed(KeyCode::D) as u8 as f32; + let x = inputs.right - inputs.left; actor.vel.x = x * RUN_SPEED; let on_ground = on_ground(&chunk_manager, &actor, materials); @@ -153,7 +153,7 @@ pub fn update_player( } // Jump - if keys.just_pressed(KeyCode::Space) { + if inputs.jump_just_pressed { if on_ground { actor.vel.y -= JUMP_MAG; player.state = PlayerState::Jumping(time.elapsed_seconds_wrapped_f64()); @@ -165,7 +165,7 @@ pub fn update_player( //Jump higher when holding space if let PlayerState::Jumping(jump_start) = player.state { - if keys.pressed(KeyCode::Space) + if inputs.jump_pressed && time.elapsed_seconds_wrapped_f64() - jump_start < TIME_JUMP_PRESSED { actor.vel.y -= PRESSED_JUMP_MAG @@ -175,7 +175,7 @@ pub fn update_player( // Jetpack let mut new_up = false; if let PlayerState::Jetpack(_) = player.state { - if player.fuel > 0. && keys.pressed(KeyCode::Space) { + if player.fuel > 0. && inputs.jump_pressed { actor.vel.y = (actor.vel.y - JETPACK_FORCE).clamp(-JETPACK_MAX, f32::MAX); player.fuel -= FUEL_COMSUMPTON; new_up = true; @@ -211,13 +211,13 @@ pub fn update_player( } //Change shooting atoms - if keys.just_pressed(KeyCode::Key1) { + if inputs.numbers[0] { player.atom_id = 2; - } else if keys.just_pressed(KeyCode::Key2) { + } else if inputs.numbers[1] { player.atom_id = 3; - } else if keys.just_pressed(KeyCode::Key3) { + } else if inputs.numbers[2] { player.atom_id = 4; - } else if keys.just_pressed(KeyCode::Key4) { + } else if inputs.numbers[3] { player.atom_id = 5; } } @@ -228,18 +228,14 @@ pub fn tool_system( mut camera: Query<(&Camera, &GlobalTransform), Without>, tool_front_ent: Query>, querys: (Query<&Window>, Query<(&mut TextureAtlasSprite, &Player)>), - resources: ( - ResMut, - ResMut, - Res>, - ), + resources: (ResMut, ResMut, Res), materials: (Res>, Res), ) { let (mut tool_transform, tool_gtransform, mut tool_sprite) = tool.single_mut(); let (camera, camera_gtransform) = camera.single_mut(); let (window, mut player) = querys; let (mut textatlas_sprite, player) = player.single_mut(); - let (mut chunk_manager, mut dirty_rects, mouse) = resources; + let (mut chunk_manager, mut dirty_rects, inputs) = resources; let Ok(window) = window.get_single() else { return; }; @@ -271,8 +267,8 @@ pub fn tool_system( let tool_front = center_vec_y_flipped + tool_slope * 5.; let mut pos_to_update = vec![]; - if mouse.pressed(MouseButton::Right) { - let new_tool_front = tool_front + tool_slope * 2.; + if inputs.push { + let new_tool_front = tool_front + tool_slope * 3.5; let n = 6; for i in 0..=n { @@ -299,7 +295,7 @@ pub fn tool_system( } } } - } else if mouse.pressed(MouseButton::Left) { + } else if inputs.pull { let center_bound = tool_front + tool_slope * TOOL_DISTANCE; let bound1 = (center_bound + bound_slope * TOOL_RANGE).as_ivec2(); @@ -343,18 +339,80 @@ pub fn update_player_sprite(mut query: Query<(&mut Transform, &Actor), With>); +pub fn get_input( + keys: Res>, + mouse_buttons: Res>, + mut inputs: ResMut, +) { + //Jump + if keys.just_pressed(KeyCode::Space) { + inputs.jump_just_pressed = true; + inputs.jump_pressed = true; + } else if inputs.jump_pressed { + inputs.jump_pressed = true; + } + + //Movement + if keys.pressed(KeyCode::A) { + inputs.left = 1.; + } + if keys.pressed(KeyCode::D) { + inputs.right = 1.; + } + + //Tool + if mouse_buttons.pressed(MouseButton::Left) { + inputs.pull = true; + } + if mouse_buttons.pressed(MouseButton::Right) { + inputs.push = true; + } + + //Numbers, to select atoms + if keys.just_pressed(KeyCode::Key1) { + inputs.numbers[0] = true; + } else if keys.just_pressed(KeyCode::Key2) { + inputs.numbers[1] = true; + } else if keys.just_pressed(KeyCode::Key3) { + inputs.numbers[2] = true; + } else if keys.just_pressed(KeyCode::Key4) { + inputs.numbers[3] = true; + } +} + +pub fn clear_input(mut inputs: ResMut) { + *inputs = Inputs::default(); +} + +#[derive(Resource, Default)] +pub struct Inputs { + left: f32, + right: f32, + + pull: bool, + push: bool, + + jump_pressed: bool, + jump_just_pressed: bool, + + numbers: [bool; 4], +} + pub struct PlayerPlugin; impl Plugin for PlayerPlugin { fn build(&self, app: &mut App) { app.add_systems( FixedUpdate, ( - update_player.after(chunk_manager_update), - update_player_sprite, - tool_system.after(chunk_manager_update), + update_player.before(update_actors), + update_player_sprite.after(update_actors), + tool_system.before(chunk_manager_update), + clear_input.after(update_player).after(tool_system), ), ) - .insert_resource(SavingTask::default()) + .add_systems(PreUpdate, get_input) + .init_resource::() + .init_resource::() .add_systems(PostStartup, player_setup.after(manager_setup)); } }