ants randomly change direction

This commit is contained in:
Felix Ableitner 2021-10-03 21:59:18 +02:00
parent b3a0e86ade
commit b9bfca0900
2 changed files with 7 additions and 0 deletions

View file

@ -1,6 +1,7 @@
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,
@ -9,6 +10,11 @@ pub(crate) struct Ant {
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);

View file

@ -18,6 +18,7 @@ async fn main() {
let map = load_texture("map_scaled.png").await.unwrap();
let map_data = map.get_texture_data();
let mut ants = init_ants(16);
loop {
// actions
ants.iter_mut().for_each(|ant| ant.walk(&map_data));