started debug display

This commit is contained in:
JOLIMAITRE Matthieu 2022-09-01 19:42:03 +02:00
parent 86ca928a3f
commit e0098a380f
4 changed files with 32 additions and 3 deletions

3
patterns/blowup.txt Normal file
View file

@ -0,0 +1,3 @@
# #
# #
###

View file

@ -81,10 +81,12 @@ where
loop { loop {
handle_inputs(&receiver, &mut view_origin, &mut delay); handle_inputs(&receiver, &mut view_origin, &mut delay);
handle.set_delay(delay); handle.set_delay(delay);
let world = handle.snapshot();
let mut canvas = Canvas::from_screen(); let mut canvas = Canvas::from_screen();
dbg_layer(&mut canvas, &world, view_origin);
grid_layer(&mut canvas, view_origin); grid_layer(&mut canvas, view_origin);
world_layer(&mut canvas, handle.snapshot(), view_origin); world_layer(&mut canvas, &world, view_origin);
title_layer(&mut canvas, handle.delay()); title_layer(&mut canvas, handle.delay());
canvas.display(); canvas.display();
} }
@ -109,7 +111,11 @@ fn handle_inputs(receiver: &mpsc::Receiver<InputCmd>, view_origin: &mut Pos, del
Dir::Right => pos!(2 * MOVEMENT_STEP, 0), Dir::Right => pos!(2 * MOVEMENT_STEP, 0),
} }
} }
InputCmd::Accelerate => *delay -= 100, InputCmd::Accelerate => {
if *delay > 0 {
*delay -= 100
}
}
InputCmd::Decelerate => *delay += 100, InputCmd::Decelerate => *delay += 100,
} }
} }
@ -127,7 +133,7 @@ fn grid_layer(canvas: &mut Canvas, view_origin: Pos) {
}) })
} }
fn world_layer<W>(canvas: &mut Canvas, world: W, view_origin: Pos) fn world_layer<W>(canvas: &mut Canvas, world: &W, view_origin: Pos)
where where
W: World, W: World,
{ {
@ -161,3 +167,18 @@ fn title_layer(canvas: &mut Canvas, de: usize) {
.and_then(|line| line.chars().nth(x as usize)) .and_then(|line| line.chars().nth(x as usize))
}) })
} }
fn dbg_layer<W>(canvas: &mut Canvas, world: &W, view_origin: Pos)
where
W: World,
{
canvas.layer(|local_pos| {
let pos = screen_pos_to_world(local_pos, view_origin);
world.dbg_is_loaded(pos).then_some('.')
})
}
fn screen_pos_to_world(mut pos: Pos, screen_origin: Pos) -> Pos {
pos.y *= 2;
pos + screen_origin
}

View file

@ -23,6 +23,7 @@ pub trait World: Default + Clone + Send + 'static {
fn get(&self, pos: Pos) -> Cell; fn get(&self, pos: Pos) -> Cell;
fn set(&mut self, pos: Pos, cell: Cell); fn set(&mut self, pos: Pos, cell: Cell);
fn actives(&self) -> Vec<Pos>; fn actives(&self) -> Vec<Pos>;
fn dbg_is_loaded(&self, pos: Pos) -> bool;
} }
pub use hashed_world::HashedWorld; pub use hashed_world::HashedWorld;

View file

@ -109,4 +109,8 @@ impl World for HashedWorld {
}) })
.collect() .collect()
} }
fn dbg_is_loaded(&self, pos: Pos) -> bool {
self.get_chunk(pos).is_some()
}
} }