From 22406661644bc080e14790e5afc4c0b877e99fad Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 3 Oct 2021 21:39:24 +0200 Subject: [PATCH] stop when hitting a wall --- src/main.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1461117..641e822 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ fn window_conf() -> Conf { } static ANT_SPEED: f32 = 20.; +static FLOOR_COLOR: Color = Color::new(0.46666667, 0.38431373, 0.3019608, 1.); struct Ant { pos: Vec2, @@ -19,9 +20,13 @@ struct Ant { } impl Ant { - fn walk(&mut self) { + fn walk(&mut self, map_data: &Image) { let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); - self.pos += acceleration * ANT_SPEED * get_frame_time(); + let new_pos = self.pos + acceleration * ANT_SPEED * get_frame_time(); + let dest_pixel = map_data.get_pixel(new_pos.x as u32, new_pos.y as u32); + if dest_pixel == FLOOR_COLOR { + self.pos = new_pos; + } } fn draw(&self) { draw_circle(self.pos.x, self.pos.y, 1., WHITE); @@ -42,10 +47,11 @@ fn init_ants(num_ants: i32) -> Vec { #[macroquad::main(window_conf)] async fn main() { let map = load_texture("map_scaled.png").await.unwrap(); + let map_data = map.get_texture_data(); let mut ants = init_ants(16); loop { // actions - ants.iter_mut().for_each(|ant| ant.walk()); + ants.iter_mut().for_each(|ant| ant.walk(&map_data)); if is_key_down(KeyCode::Escape) || is_quit_requested() { return; }