ants/src/ant.rs

33 lines
833 B
Rust
Raw Normal View History

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