From ddb8e0423dea9cf2343693764d9da75f26ab4614 Mon Sep 17 00:00:00 2001 From: Nicolas Klier Date: Wed, 8 May 2024 15:00:50 +0200 Subject: [PATCH] Checkpoint before refactor --- src/main.rs | 6 +++--- src/npc.rs | 14 +++++++------- src/stage.rs | 46 ++++++++++++++++++++-------------------------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index e8f9257..1ac38af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,8 @@ use winit::{ }; use winit_input_helper::WinitInputHelper; -const WIDTH: u32 = 64; -const HEIGHT: u32 = 64; +const WIDTH: usize = 64; +const HEIGHT: usize = 64; const SCALE_FACTOR: f64 = 10.0; fn main() -> Result<(), Error> { @@ -39,7 +39,7 @@ fn main() -> Result<(), Error> { let mut pixels = { let window_size = window.inner_size(); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); - Pixels::new(WIDTH, HEIGHT, surface_texture)? + Pixels::new(WIDTH as u32, HEIGHT as u32, surface_texture)? }; let mut stage = Stage::new(WIDTH as usize, HEIGHT as usize); diff --git a/src/npc.rs b/src/npc.rs index 8306c72..c33cd7f 100644 --- a/src/npc.rs +++ b/src/npc.rs @@ -1,4 +1,4 @@ -use crate::{cell::CellVariance, direction::Direction}; +use crate::{cell::CellVariance, direction::Direction, HEIGHT, WIDTH}; #[derive(Clone, Debug)] pub struct NPC { @@ -46,10 +46,10 @@ impl NPC { } } - pub fn bounce(&mut self) -> Direction { + pub fn bounce_of(&mut self, obstacle: (usize, usize)) -> Direction { // Get the current and next positions - let (curr_x, curr_y) = (self.pos_x as isize, self.pos_y as isize); - let (next_x, next_y) = self.next_position(); + let (curr_x, curr_y) = (self.pos_x, self.pos_y); + let (next_x, next_y) = obstacle; let mut next_direction = self.direction.clone(); // Hit top from below || Hit bottom from above @@ -69,10 +69,10 @@ impl NPC { self.direction.clone() } - pub fn next_position(&self) -> (isize, isize) { + pub fn next_position(&self) -> (usize, usize) { ( - (self.pos_x as isize + self.direction.x), - (self.pos_y as isize + self.direction.y), + (self.pos_x as isize + self.direction.x).clamp(0, WIDTH.try_into().unwrap()) as usize, + (self.pos_y as isize + self.direction.y).clamp(0, HEIGHT.try_into().unwrap()) as usize ) } } diff --git a/src/stage.rs b/src/stage.rs index abea87f..68600a7 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -177,36 +177,30 @@ impl Stage { let mut hit_cells: Vec<(usize, usize)> = vec![]; for npc in self.npcs.iter_mut() { - // if (npc.next_position().0 < 0 || npc.next_position().0 > self.width as isize) || - // (npc.next_position().1 < 0 || npc.next_position().1 > self.height as isize) { - // npc.bounce(); - // } - - // Check for collision with window borders - if npc.pos_x <= 0 || npc.pos_x >= self.width { - // Bounce off horizontally - npc.direction.x = -npc.direction.x; - // Move object back inside the window - npc.pos_x = npc.pos_x.clamp(0, self.width - 1); - - } - - if npc.pos_y <= 0 || npc.pos_y >= self.height { - // Bounce off vertically - npc.direction.y = -npc.direction.y; - // Move object back inside the window - npc.pos_y = npc.pos_y.clamp(0, self.height - 1); + if (npc.next_position().0 < 0 || npc.next_position().0 > self.width) + || (npc.next_position().1 < 0 || npc.next_position().1 > self.height) + { + npc.bounce_of(npc.next_position()); } let index = npc.next_position().0 as usize + npc.next_position().1 as usize * self.width; - let next_cell = self.cells_dup[index]; - // If our next cell would be one that we collide with. - if npc.obstructed_from.contains(&next_cell.variance) { - npc.direction.invert(); - hit_cells.push((npc.pos_x, npc.pos_y)); - continue; + if index > self.cells_dup.len() - 1 { + npc.bounce_of(npc.next_position()); + } else { + let next_cell = self.cells_dup[index]; + + // If our next cell would be one that we collide with. + if npc.obstructed_from.contains(&next_cell.variance) { + hit_cells.push(( + npc.next_position().0 as usize, + npc.next_position().1 as usize, + )); + npc.direction.invert(); + + continue; + } } npc.pos_x = npc.next_position().0 as usize; @@ -222,7 +216,7 @@ impl Stage { // Write into scratch_cells, since we're still reading from `self.cells` if hit_cells.contains(&(x, y)) { - self.cells_dup[idx] = self.cells[idx].invert(); + self.cells_dup[idx] = self.cells[idx].invert(); } else { self.cells_dup[idx] = self.cells[idx]; }