From 33a865d0591cb220b9ff0c37f2484261c3d9c661 Mon Sep 17 00:00:00 2001 From: Nicolas Klier Date: Wed, 8 May 2024 17:50:11 +0200 Subject: [PATCH] Broken build, stuck in refactor. --- src/cell.rs | 1 - src/main.rs | 14 +++++---- src/npc.rs | 25 +++++++++++---- src/stage.rs | 86 +++++++++++++++------------------------------------- 4 files changed, 52 insertions(+), 74 deletions(-) diff --git a/src/cell.rs b/src/cell.rs index 44afcac..89654af 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -1,5 +1,4 @@ use core::fmt; -use std::fmt::write; #[derive(Clone, Copy, Debug, PartialEq)] pub enum CellVariance { diff --git a/src/main.rs b/src/main.rs index 1ac38af..1b1500b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use winit::{ dpi::LogicalSize, event::{Event, VirtualKeyCode}, event_loop::{ControlFlow, EventLoop}, - window::{self, WindowBuilder}, + window::WindowBuilder, }; use winit_input_helper::WinitInputHelper; @@ -47,13 +47,11 @@ fn main() -> Result<(), Error> { let mut paused = false; - let mut draw_state: Option = None; - event_loop.run(move |event, _, control_flow| { if let Event::RedrawRequested(_) = event { stage.draw(pixels.frame_mut()); - if let Err(err) = pixels.render() { + if let Err(_) = pixels.render() { debug_assert!(true, "Oops, I did it again."); *control_flow = ControlFlow::Exit; @@ -80,9 +78,13 @@ fn main() -> Result<(), Error> { stage.scatter(stage::ScatterTypes::Random); } + if input.key_pressed(VirtualKeyCode::H) { + stage.scatter(stage::ScatterTypes::HalfVertical); + } + // Handle mouse. This is a bit involved since support some simple // line drawing (mostly because it makes nice looking patterns). - let (mouse_cell, mouse_prev_cell) = input + let (mouse_cell, _) = input .mouse() .map(|(mx, my)| { let (dx, dy) = input.mouse_diff(); @@ -116,7 +118,7 @@ fn main() -> Result<(), Error> { // Resize the window if let Some(size) = input.window_resized() { - if let Err(err) = pixels.resize_surface(size.width, size.height) { + if let Err(_) = pixels.resize_surface(size.width, size.height) { debug_assert!(true, "Resize failed!"); *control_flow = ControlFlow::Exit; return; diff --git a/src/npc.rs b/src/npc.rs index c33cd7f..950f465 100644 --- a/src/npc.rs +++ b/src/npc.rs @@ -1,4 +1,4 @@ -use crate::{cell::CellVariance, direction::Direction, HEIGHT, WIDTH}; +use crate::{cell::{Cell, CellVariance}, direction::Direction, stage::{self, Stage}, HEIGHT, WIDTH}; #[derive(Clone, Debug)] pub struct NPC { @@ -46,27 +46,40 @@ impl NPC { } } - pub fn bounce_of(&mut self, obstacle: (usize, usize)) -> Direction { + pub fn into_cell(&self) -> Cell { + // TODO: Bad idea. + Cell::new(self.obstructed_from[0]) + } + + pub fn process_bounce(&mut self, mut stage: &Stage) { + println!("Calc bounce for {:?}", self); + let obstacle = self.next_position(); + // Get the current and next positions 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 - if curr_y < next_y || curr_y > next_y { + let hit_head = next_y > curr_y || curr_y > next_y; + + // Hit right from left || Hit left from right + let hit_side = next_x > curr_x || curr_x > next_x; + + if hit_head { next_direction.y *= -1; } // Hit right from left || Hit left from right - if next_x > curr_x || curr_x > next_x { + if hit_side { next_direction.x *= -1; } // Return the new bounce direction self.direction = next_direction; - println!("Next: {:?}", self.direction); - self.direction.clone() + let _ = self.pos_x.checked_add_signed(self.direction.x); + let _ = self.pos_y.checked_add_signed(self.direction.y); } pub fn next_position(&self) -> (usize, usize) { diff --git a/src/stage.rs b/src/stage.rs index 68600a7..e337c47 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -1,3 +1,6 @@ +use std::borrow::Borrow; +use std::cell; + use noise::utils::NoiseMapBuilder; use noise::utils::PlaneMapBuilder; use noise::BasicMulti; @@ -9,13 +12,14 @@ use crate::cell::Cell; use crate::cell::CellVariance; use crate::direction::Direction; use crate::npc::NPC; + +#[derive(Debug, Clone)] pub struct Stage { cells: Vec, /// Used for temporary work item. Replaces `cells` once done. cells_dup: Vec, npcs: Vec, - npc_map: Vec>, width: usize, height: usize, } @@ -34,7 +38,6 @@ impl Stage { cells: vec![Cell::default(); size], cells_dup: vec![Cell::default(); size], npcs: vec![], - npc_map: vec![None; size], width, height, } @@ -132,34 +135,28 @@ impl Stage { pub fn draw(&mut self, screen: &mut [u8]) { debug_assert_eq!(screen.len(), 4 * self.cells.len()); - // Draw NPCs - self.npc_map.fill(None); - for (i, npc) in self.npcs.iter().enumerate() { - let map_pos = npc.pos_x + npc.pos_y * self.width; - // let color = match npc.walk_on { - // CellVariance::DAY => [0x10, 0x41, 0x4F, 0xFF], - // CellVariance::NIGHT => [0xD3, 0xE3, 0xD3, 0xFF], - // _ => { - // panic!("This NPC shouldn't even exist!"); - // } - // }; - - self.npc_map[map_pos] = Some(Cell::new(npc.obstructed_from[0])); - } - // Draw background - for (i, (mut c, pix)) in self + for (i, (c, pix)) in self .cells .iter() .zip(screen.chunks_exact_mut(4)) .enumerate() { - let npc_cell = self.npc_map.get(i).unwrap(); - if let Some(npc_cell) = npc_cell { - c = &npc_cell; + let mut cell: Cell = c.clone(); + + let mut npc_cell: Option = None; + for npc in self.npcs.iter() { + if npc.pos_x + npc.pos_y * self.width == i { + npc_cell = Some(npc.clone()); + } } - let color = match c.variance { + // Draw NPCs + if let Some(npc_cell) = npc_cell { + cell = npc_cell.into_cell(); + } + + let color = match cell.variance { CellVariance::VOID => [0x0, 0x0, 0x0, 0xFF], CellVariance::DAY => [0xD3, 0xE3, 0xD3, 0xFF], CellVariance::NIGHT => [0x10, 0x41, 0x4F, 0xFF], @@ -174,52 +171,19 @@ impl Stage { } pub fn update(&mut self) { - let mut hit_cells: Vec<(usize, usize)> = vec![]; - + // Process bounce logic for npc in self.npcs.iter_mut() { - 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; - - 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; - npc.pos_y = npc.next_position().1 as usize; - - // We can move freely for now. - self.npc_map[index] = Some(Cell::new(npc.obstructed_from[0])); + let me = &self; + npc.process_bounce(self); } + // Write current background for y in 0..self.height { for x in 0..self.width { let idx = x + y * self.width; - // 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(); - } else { - self.cells_dup[idx] = self.cells[idx]; - } + // Write into scratch_cells, since we're still reading from `self.cells` + self.cells_dup[idx] = self.cells[idx]; } }