2021-10-06 15:47:04 +00:00
|
|
|
use crate::constants::*;
|
|
|
|
use macroquad::color::*;
|
|
|
|
use macroquad::math::Vec2;
|
|
|
|
use macroquad::prelude::Image;
|
2021-10-03 19:59:18 +00:00
|
|
|
use macroquad::rand::RandomRange;
|
2021-10-03 20:23:57 +00:00
|
|
|
use std::f32::consts::PI;
|
2021-10-03 19:49:30 +00:00
|
|
|
|
2021-10-06 23:29:33 +00:00
|
|
|
#[derive(Debug)]
|
2021-10-03 19:49:30 +00:00
|
|
|
pub(crate) struct Ant {
|
2021-10-03 20:23:57 +00:00
|
|
|
pos: Vec2,
|
|
|
|
dir: f32,
|
2021-10-03 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ant {
|
2021-10-03 20:23:57 +00:00
|
|
|
pub(crate) fn init(num: i32) -> Vec<Self> {
|
|
|
|
let dir_steps = PI * 2. / num as f32;
|
|
|
|
(0..num)
|
|
|
|
.into_iter()
|
|
|
|
.map(|n| Ant {
|
2021-10-06 00:56:06 +00:00
|
|
|
pos: Vec2::new((MAP_WIDTH / 2) as f32, (MAP_HEIGHT / 2) as f32),
|
2021-10-03 20:23:57 +00:00
|
|
|
dir: n as f32 * dir_steps,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2021-10-06 00:56:06 +00:00
|
|
|
pub(crate) fn walk(&mut self, frame_time: f32, trail_map: &mut Image) {
|
2021-10-06 23:48:24 +00:00
|
|
|
self.sense(trail_map);
|
2021-10-03 20:23:57 +00:00
|
|
|
let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32;
|
|
|
|
self.dir += new_dir;
|
2021-10-06 00:56:06 +00:00
|
|
|
if self.dir > 2. * PI {
|
|
|
|
self.dir -= 2. * PI;
|
|
|
|
}
|
2021-10-03 19:59:18 +00:00
|
|
|
|
2021-10-03 20:23:57 +00:00
|
|
|
let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos());
|
2021-10-06 00:56:06 +00:00
|
|
|
self.pos += acceleration * ANT_SPEED * frame_time;
|
2021-10-06 23:48:24 +00:00
|
|
|
// if we are out of the map, turn around 180 degrees (or pi in radian)
|
|
|
|
if self.pos.x < 0.0
|
|
|
|
|| self.pos.x >= (MAP_WIDTH - 1) as f32
|
|
|
|
|| self.pos.y < 0.0
|
|
|
|
|| self.pos.y >= (MAP_HEIGHT - 1) as f32
|
|
|
|
{
|
|
|
|
self.dir += PI;
|
|
|
|
}
|
2021-10-06 23:29:33 +00:00
|
|
|
self.pos.x = self.pos.x.clamp(0., (MAP_WIDTH - 1) as f32);
|
|
|
|
self.pos.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32);
|
2021-10-03 19:49:30 +00:00
|
|
|
}
|
2021-10-06 23:48:24 +00:00
|
|
|
fn sense(&self, trail_map: &mut Image) {
|
|
|
|
trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW);
|
|
|
|
trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW);
|
|
|
|
trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW);
|
|
|
|
}
|
2021-10-07 01:19:17 +00:00
|
|
|
// TODO: change color over time or based on other factors
|
|
|
|
pub(crate) fn draw(&self, trail_map: &mut Image) {
|
|
|
|
let radius: i32 = 3;
|
|
|
|
for x in -radius..radius {
|
|
|
|
for y in -radius..radius {
|
|
|
|
let x = (self.pos.x as i32 + x) as u32;
|
|
|
|
let y = (self.pos.y as i32 + y) as u32;
|
|
|
|
if x < (MAP_WIDTH - 1) && y < (MAP_HEIGHT - 1) {
|
|
|
|
trail_map.set_pixel(x, y, RED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 20:23:57 +00:00
|
|
|
}
|
|
|
|
}
|