From b9bfca09006bb7e1b407e94eea1101b2f306d0a3 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 3 Oct 2021 21:59:18 +0200 Subject: [PATCH] ants randomly change direction --- src/ant.rs | 6 ++++++ src/main.rs | 1 + 2 files changed, 7 insertions(+) diff --git a/src/ant.rs b/src/ant.rs index 516f216..5bd5615 100644 --- a/src/ant.rs +++ b/src/ant.rs @@ -1,6 +1,7 @@ use macroquad::prelude::*; use std::f32::consts::PI; use crate::constants::{FLOOR_COLOR, ANT_SPEED}; +use macroquad::rand::RandomRange; pub(crate) struct Ant { pos: Vec2, @@ -9,6 +10,11 @@ pub(crate) struct Ant { impl Ant { pub(crate)fn walk(&mut self, map_data: &Image) { + let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32; + self.dir = self.dir + new_dir; + self.dir = self.dir.clamp(0., PI * 2.); + + let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); 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); diff --git a/src/main.rs b/src/main.rs index 220c08a..77f535c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ 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(&map_data));