From deddb20fade69b91f29c99824ee6fa092572ba6e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 7 Oct 2021 01:48:24 +0200 Subject: [PATCH] ants reflect back from map edges --- src/ant.rs | 16 +++++++++++++++- src/constants.rs | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ant.rs b/src/ant.rs index 47895ef..99f2fd6 100644 --- a/src/ant.rs +++ b/src/ant.rs @@ -24,6 +24,7 @@ impl Ant { .collect() } 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; self.dir += new_dir; if self.dir > 2. * PI { @@ -32,16 +33,29 @@ impl Ant { let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); 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.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32); // TODO: change color over time 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 { fn draw(&self) { - draw_circle(self.pos.x, self.pos.y, 1., WHITE); + //draw_circle(self.pos.x, self.pos.y, 1., WHITE); } } diff --git a/src/constants.rs b/src/constants.rs index 0fe2f29..28be27d 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -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_HEIGHT: u32 = 720;