From 8052976133b5976205d1ab41eb411e4711552e72 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 3 Oct 2021 21:23:32 +0200 Subject: [PATCH] have a couple ants walking in different directions --- src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index a276486..1461117 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ use macroquad::prelude::*; +use std::f32::consts::PI; fn window_conf() -> Conf { Conf { + window_title: "Ants".to_string(), fullscreen: false, window_width: 1280, window_height: 720, @@ -9,18 +11,50 @@ fn window_conf() -> Conf { } } +static ANT_SPEED: f32 = 20.; + +struct Ant { + pos: Vec2, + dir: f32, +} + +impl Ant { + fn walk(&mut self) { + let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); + self.pos += acceleration * ANT_SPEED * get_frame_time(); + } + fn draw(&self) { + draw_circle(self.pos.x, self.pos.y, 1., WHITE); + } +} + +fn init_ants(num_ants: i32) -> Vec { + 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() +} + #[macroquad::main(window_conf)] async fn main() { let map = load_texture("map_scaled.png").await.unwrap(); + let mut ants = init_ants(16); loop { - clear_background(BEIGE); - - draw_texture(map, 0., 0., WHITE); - + // actions + ants.iter_mut().for_each(|ant| ant.walk()); if is_key_down(KeyCode::Escape) || is_quit_requested() { return; } + // render + clear_background(BEIGE); + draw_texture(map, 0., 0., WHITE); + ants.iter().for_each(|ant| ant.draw()); + next_frame().await } -} \ No newline at end of file +}