Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Some polishing #55

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bevy::render::render_resource::*;
use std::cmp::Ordering;
use std::collections::HashSet;

use crate::prelude::*;
Expand All @@ -26,21 +25,24 @@ impl Chunk {
pub fn new(texture: Handle<Image>, 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IVec2, Chunk>,
pub pos: IVec2,
Expand Down
8 changes: 4 additions & 4 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.;

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
38 changes: 26 additions & 12 deletions src/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,44 @@ 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::<DeferredParticleUpdate>();
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 {
commands.entity(update.ent).despawn();
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.;
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down