use crate::constants::ANT_SPEED; use crate::constants::MAP_HEIGHT; use crate::constants::MAP_WIDTH; use crate::Drawable; use macroquad::prelude::*; use macroquad::rand::RandomRange; use std::f32::consts::PI; pub(crate) struct Ant { pos: Vec2, dir: f32, } impl Ant { pub(crate) fn init(num: i32) -> Vec { let dir_steps = PI * 2. / num as f32; (0..num) .into_iter() .map(|n| Ant { pos: Vec2::new((MAP_WIDTH / 2) as f32, (MAP_HEIGHT / 2) as f32), dir: n as f32 * dir_steps, }) .collect() } pub(crate) fn walk(&mut self, frame_time: f32, trail_map: &mut Image) { let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32; self.dir += new_dir; if self.dir > 2. * PI { self.dir -= 2. * PI; } let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); self.pos += acceleration * ANT_SPEED * frame_time; // TODO: this is not fixing out of bounds write self.pos.x = self.pos.x.clamp(0., MAP_WIDTH as f32); self.pos.y = self.pos.y.clamp(0., MAP_HEIGHT as f32); // TODO: change color over time trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED); } } impl Drawable for Ant { fn draw(&self) { draw_circle(self.pos.x, self.pos.y, 1., WHITE); } }