Checkpoint before refactor
This commit is contained in:
@@ -13,8 +13,8 @@ use winit::{
|
|||||||
};
|
};
|
||||||
use winit_input_helper::WinitInputHelper;
|
use winit_input_helper::WinitInputHelper;
|
||||||
|
|
||||||
const WIDTH: u32 = 64;
|
const WIDTH: usize = 64;
|
||||||
const HEIGHT: u32 = 64;
|
const HEIGHT: usize = 64;
|
||||||
const SCALE_FACTOR: f64 = 10.0;
|
const SCALE_FACTOR: f64 = 10.0;
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
@@ -39,7 +39,7 @@ fn main() -> Result<(), Error> {
|
|||||||
let mut pixels = {
|
let mut pixels = {
|
||||||
let window_size = window.inner_size();
|
let window_size = window.inner_size();
|
||||||
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
|
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);
|
let mut stage = Stage::new(WIDTH as usize, HEIGHT as usize);
|
||||||
|
|||||||
14
src/npc.rs
14
src/npc.rs
@@ -1,4 +1,4 @@
|
|||||||
use crate::{cell::CellVariance, direction::Direction};
|
use crate::{cell::CellVariance, direction::Direction, HEIGHT, WIDTH};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct NPC {
|
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
|
// Get the current and next positions
|
||||||
let (curr_x, curr_y) = (self.pos_x as isize, self.pos_y as isize);
|
let (curr_x, curr_y) = (self.pos_x, self.pos_y);
|
||||||
let (next_x, next_y) = self.next_position();
|
let (next_x, next_y) = obstacle;
|
||||||
let mut next_direction = self.direction.clone();
|
let mut next_direction = self.direction.clone();
|
||||||
|
|
||||||
// Hit top from below || Hit bottom from above
|
// Hit top from below || Hit bottom from above
|
||||||
@@ -69,10 +69,10 @@ impl NPC {
|
|||||||
self.direction.clone()
|
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_x as isize + self.direction.x).clamp(0, WIDTH.try_into().unwrap()) as usize,
|
||||||
(self.pos_y as isize + self.direction.y),
|
(self.pos_y as isize + self.direction.y).clamp(0, HEIGHT.try_into().unwrap()) as usize
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
44
src/stage.rs
44
src/stage.rs
@@ -177,36 +177,30 @@ impl Stage {
|
|||||||
let mut hit_cells: Vec<(usize, usize)> = vec![];
|
let mut hit_cells: Vec<(usize, usize)> = vec![];
|
||||||
|
|
||||||
for npc in self.npcs.iter_mut() {
|
for npc in self.npcs.iter_mut() {
|
||||||
// if (npc.next_position().0 < 0 || npc.next_position().0 > self.width as isize) ||
|
if (npc.next_position().0 < 0 || npc.next_position().0 > self.width)
|
||||||
// (npc.next_position().1 < 0 || npc.next_position().1 > self.height as isize) {
|
|| (npc.next_position().1 < 0 || npc.next_position().1 > self.height)
|
||||||
// npc.bounce();
|
{
|
||||||
// }
|
npc.bounce_of(npc.next_position());
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let index =
|
let index =
|
||||||
npc.next_position().0 as usize + npc.next_position().1 as usize * self.width;
|
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 index > self.cells_dup.len() - 1 {
|
||||||
if npc.obstructed_from.contains(&next_cell.variance) {
|
npc.bounce_of(npc.next_position());
|
||||||
npc.direction.invert();
|
} else {
|
||||||
hit_cells.push((npc.pos_x, npc.pos_y));
|
let next_cell = self.cells_dup[index];
|
||||||
continue;
|
|
||||||
|
// 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_x = npc.next_position().0 as usize;
|
||||||
|
|||||||
Reference in New Issue
Block a user