From d591dcfe53a1517cdfed34cb0caad1a4eb6c596d Mon Sep 17 00:00:00 2001 From: Isaac Turci <78173025+Zac8668@users.noreply.github.com> Date: Fri, 12 Jan 2024 16:33:47 -0300 Subject: [PATCH] feat: Some polishing (#55) * Fixed input frame dependent bug * Polishing * Better particle performance * Cargo fmt --- src/chunk.rs | 18 ++++++++++-------- src/chunk_manager.rs | 2 +- src/consts.rs | 8 ++++---- src/main.rs | 1 + src/particles.rs | 38 ++++++++++++++++++++++++++------------ src/player.rs | 14 ++++++++++++-- 6 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/chunk.rs b/src/chunk.rs index dc0dfa7..e1ab739 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -1,5 +1,4 @@ use bevy::render::render_resource::*; -use std::cmp::Ordering; use std::collections::HashSet; use crate::prelude::*; @@ -26,21 +25,24 @@ impl Chunk { pub fn new(texture: Handle, index: IVec2) -> Chunk { let mut atoms = [Atom::default(); CHUNK_LEN]; - match index.y.cmp(&2) { - Ordering::Less => {} - Ordering::Equal => { + match index.y { + i32::MIN..=0 => {} + 1 => { for (i, atom) in atoms.iter_mut().enumerate() { let id = match i { 0..=511 => 6, - 512..=2815 => 7, - _ => 4, + _ => 7, }; *atom = Atom::new(id); } } - - Ordering::Greater => { + 2 => { + for atom in &mut atoms { + *atom = Atom::new(4); + } + } + 3..=i32::MAX => { for atom in &mut atoms { *atom = Atom::new(8); } diff --git a/src/chunk_manager.rs b/src/chunk_manager.rs index e72835d..88acad3 100644 --- a/src/chunk_manager.rs +++ b/src/chunk_manager.rs @@ -9,7 +9,7 @@ use smallvec::SmallVec; use crate::prelude::*; /// Updates and do the chunks logic -#[derive(Default, Resource, Clone)] +#[derive(Default, Resource)] pub struct ChunkManager { pub chunks: HashMap, pub pos: IVec2, diff --git a/src/consts.rs b/src/consts.rs index aa7ae12..b9583bd 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -22,10 +22,10 @@ pub const JETPACK_MAX: f32 = 3.; pub const JUMP_MAG: f32 = 9.; pub const PRESSED_JUMP_MAG: f32 = 0.6; pub const TIME_JUMP_PRESSED: f64 = 0.8; -pub const RUN_SPEED: f32 = 3.5; +pub const RUN_SPEED: f32 = 2.5; pub const TOOL_DISTANCE: f32 = 32.; -pub const TOOL_RANGE: f32 = 12.; +pub const TOOL_RANGE: f32 = 16.; pub const ZOOM_LOWER_BOUND: f32 = 0.15; pub const ZOOM_UPPER_BOUND: f32 = 0.30; @@ -40,8 +40,8 @@ pub const GRAVITY: u8 = 1; pub const TERM_VEL: u8 = 10; pub const FRAMES_SLEEP: u8 = 1; //Has to be even -pub const LOAD_WIDTH: i32 = 20; -pub const LOAD_HEIGHT: i32 = 12; +pub const LOAD_WIDTH: i32 = 32; +pub const LOAD_HEIGHT: i32 = 18; pub const _CAMERA_SPEED: f32 = 10.; diff --git a/src/main.rs b/src/main.rs index eefb014..76881ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ mod prelude { pub use std::fs::File; pub use std::io::Write; pub use std::io::{BufReader, BufWriter}; + pub use std::sync::{Arc, RwLock}; pub use crate::materials::Material; } diff --git a/src/particles.rs b/src/particles.rs index 860b10e..0ebe375 100644 --- a/src/particles.rs +++ b/src/particles.rs @@ -73,12 +73,13 @@ pub fn update_particles( let compute_pool = ComputeTaskPool::get(); compute_pool.scope(|deferred_scope| { - let chunks = &chunk_manager.chunks.clone(); let manager_pos = &chunk_manager.pos.clone(); + let chunk_manager = Arc::new(RwLock::new(&mut chunk_manager)); let (particles_send, particles_recv) = async_channel::unbounded::(); let particle_send = &particles_send; + let chunk_manager_deffered = Arc::clone(&chunk_manager); deferred_scope.spawn(async move { while let Ok(update) = particles_recv.recv().await { if update.remove { @@ -86,20 +87,30 @@ pub fn update_particles( continue; } - if let Some(atom) = chunk_manager.get_mut_atom(update.chunk_pos) { + let mut change_atom = false; + if let Some(atom) = chunk_manager_deffered + .read() + .unwrap() + .get_atom(&update.chunk_pos) + { if materials[atom.id].is_void() { - *atom = update.atom; - commands.entity(update.ent).despawn(); - - update_dirty_rects(&mut dirty_rects.render, update.chunk_pos); - update_dirty_rects_3x3(&mut dirty_rects.current, update.chunk_pos); + change_atom = true; } } + + if change_atom { + let atom = &mut chunk_manager_deffered.write().unwrap()[update.chunk_pos]; + *atom = update.atom; + commands.entity(update.ent).despawn(); + + update_dirty_rects(&mut dirty_rects.render, update.chunk_pos); + update_dirty_rects_3x3(&mut dirty_rects.current, update.chunk_pos); + } } }); particles - .iter_mut() + .par_iter_mut() .for_each(|(mut particle, mut transform, ent)| { let mut dest_pos = transform.translation.xy(); dest_pos.y *= -1.; @@ -140,10 +151,13 @@ pub fn update_particles( let chunk_pos = global_to_chunk(pos); let prev_chunk_pos = global_to_chunk(prev_pos); - let atom = chunks.get(&chunk_pos.chunk).unwrap().atoms - [chunk_pos.atom.d1()]; - let prev_atom = chunks.get(&prev_chunk_pos.chunk).unwrap().atoms - [prev_chunk_pos.atom.d1()]; + let atom = + *chunk_manager.read().unwrap().get_atom(&chunk_pos).unwrap(); + let prev_atom = *chunk_manager + .read() + .unwrap() + .get_atom(&prev_chunk_pos) + .unwrap(); if particle.state == PartState::Normal && !materials[atom.id].is_void() diff --git a/src/player.rs b/src/player.rs index 057de14..83d3812 100644 --- a/src/player.rs +++ b/src/player.rs @@ -302,8 +302,18 @@ pub fn tool_system( let bound2 = (center_bound + -bound_slope * TOOL_RANGE).as_ivec2(); for bound_vec in Line::new(bound1, bound2 - bound1) { - for vec in Line::new(tool_front.as_ivec2(), bound_vec - tool_front.as_ivec2()) { + for vec in Line::new( + (tool_front - 4. * tool_slope).as_ivec2(), + bound_vec - (tool_front - 4. * tool_slope).as_ivec2(), + ) { let chunk_pos = global_to_chunk(vec); + if (vec.distance_squared((tool_front - 6. * tool_slope).as_ivec2()) as f32) + .sqrt() + < 6. + { + continue; + } + if let Some(atom) = chunk_manager.get_mut_atom(chunk_pos) { if !materials[atom.id].is_void() && !materials[atom.id].is_object() { commands.spawn(Particle { @@ -348,7 +358,7 @@ pub fn get_input( if keys.just_pressed(KeyCode::Space) { inputs.jump_just_pressed = true; inputs.jump_pressed = true; - } else if inputs.jump_pressed { + } else if keys.pressed(KeyCode::Space) { inputs.jump_pressed = true; }