ants reflect back from map edges

This commit is contained in:
Felix Ableitner 2021-10-07 01:48:24 +02:00
parent 667c34fe92
commit deddb20fad
2 changed files with 16 additions and 2 deletions

View file

@ -24,6 +24,7 @@ impl Ant {
.collect() .collect()
} }
pub(crate) fn walk(&mut self, frame_time: f32, trail_map: &mut Image) { pub(crate) fn walk(&mut self, frame_time: f32, trail_map: &mut Image) {
self.sense(trail_map);
let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32; let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32;
self.dir += new_dir; self.dir += new_dir;
if self.dir > 2. * PI { if self.dir > 2. * PI {
@ -32,16 +33,29 @@ impl Ant {
let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos());
self.pos += acceleration * ANT_SPEED * frame_time; self.pos += acceleration * ANT_SPEED * frame_time;
// 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;
}
self.pos.x = self.pos.x.clamp(0., (MAP_WIDTH - 1) as f32); 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); self.pos.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32);
// TODO: change color over time // TODO: change color over time
trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED); trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED);
} }
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);
}
} }
impl Drawable for Ant { impl Drawable for Ant {
fn draw(&self) { fn draw(&self) {
draw_circle(self.pos.x, self.pos.y, 1., WHITE); //draw_circle(self.pos.x, self.pos.y, 1., WHITE);
} }
} }

View file

@ -1,3 +1,3 @@
pub static ANT_SPEED: f32 = 20.; pub static ANT_SPEED: f32 = 200.;
pub const MAP_WIDTH: u32 = 1280; pub const MAP_WIDTH: u32 = 1280;
pub const MAP_HEIGHT: u32 = 720; pub const MAP_HEIGHT: u32 = 720;