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, dir: f32, } 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); if dest_pixel == FLOOR_COLOR { self.pos = new_pos; } } pub(crate)fn draw(&self) { draw_circle(self.pos.x, self.pos.y, 1., BLACK); } } pub(crate) fn init_ants(num_ants: i32) -> Vec { let dir_steps = PI * 2. / num_ants as f32; (0..num_ants) .into_iter() .map(|n| Ant { pos: Vec2::new(400., 200.), dir: n as f32 * dir_steps, }) .collect() }