split into separate files

This commit is contained in:
Felix Ableitner 2021-10-03 21:49:30 +02:00
parent 2240666164
commit b3a0e86ade
3 changed files with 41 additions and 35 deletions

33
src/ant.rs Normal file
View file

@ -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<Ant> {
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()
}

4
src/constants.rs Normal file
View file

@ -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.);

View file

@ -1,5 +1,7 @@
use macroquad::prelude::*; mod ant;
use std::f32::consts::PI; mod constants;
use macroquad::prelude::*;use crate::ant::init_ants;
fn window_conf() -> Conf { fn window_conf() -> 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<Ant> {
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)] #[macroquad::main(window_conf)]
async fn main() { async fn main() {
let map = load_texture("map_scaled.png").await.unwrap(); let map = load_texture("map_scaled.png").await.unwrap();