2021-10-03 19:49:30 +00:00
|
|
|
use macroquad::prelude::*;
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
use crate::constants::{FLOOR_COLOR, ANT_SPEED};
|
2021-10-03 19:59:18 +00:00
|
|
|
use macroquad::rand::RandomRange;
|
2021-10-03 19:49:30 +00:00
|
|
|
|
|
|
|
pub(crate) struct Ant {
|
|
|
|
pos: Vec2,
|
|
|
|
dir: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ant {
|
|
|
|
pub(crate)fn walk(&mut self, map_data: &Image) {
|
2021-10-03 19:59:18 +00:00
|
|
|
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.);
|
|
|
|
|
|
|
|
|
2021-10-03 19:49:30 +00:00
|
|
|
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<Ant> {
|
|
|
|
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()
|
|
|
|
}
|