From b3a0e86adee37083b997b4392c873735aebb20e3 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 3 Oct 2021 21:49:30 +0200 Subject: [PATCH] split into separate files --- src/ant.rs | 33 +++++++++++++++++++++++++++++++++ src/constants.rs | 4 ++++ src/main.rs | 39 ++++----------------------------------- 3 files changed, 41 insertions(+), 35 deletions(-) create mode 100644 src/ant.rs create mode 100644 src/constants.rs diff --git a/src/ant.rs b/src/ant.rs new file mode 100644 index 0000000..516f216 --- /dev/null +++ b/src/ant.rs @@ -0,0 +1,33 @@ +use macroquad::prelude::*; +use std::f32::consts::PI; +use crate::constants::{FLOOR_COLOR, ANT_SPEED}; + +pub(crate) struct Ant { + pos: Vec2, + dir: f32, +} + +impl Ant { + pub(crate)fn walk(&mut self, map_data: &Image) { + 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); + if dest_pixel == FLOOR_COLOR { + self.pos = new_pos; + } + } + pub(crate)fn draw(&self) { + draw_circle(self.pos.x, self.pos.y, 1., BLACK); + } +} + +pub(crate) 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() +} \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..ba12931 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,4 @@ +use macroquad::color::Color; + +pub static ANT_SPEED: f32 = 20.; +pub static FLOOR_COLOR: Color = Color::new(0.46666667, 0.38431373, 0.3019608, 1.); diff --git a/src/main.rs b/src/main.rs index 641e822..220c08a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ -use macroquad::prelude::*; -use std::f32::consts::PI; +mod ant; +mod constants; + +use macroquad::prelude::*;use crate::ant::init_ants; fn window_conf() -> Conf { Conf { @@ -11,39 +13,6 @@ fn window_conf() -> Conf { } } -static ANT_SPEED: f32 = 20.; -static FLOOR_COLOR: Color = Color::new(0.46666667, 0.38431373, 0.3019608, 1.); - -struct Ant { - pos: Vec2, - dir: f32, -} - -impl Ant { - fn walk(&mut self, map_data: &Image) { - 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); - if dest_pixel == FLOOR_COLOR { - self.pos = new_pos; - } - } - 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();