2021-10-03 10:39:27 +00:00
|
|
|
use macroquad::prelude::*;
|
2021-10-03 19:23:32 +00:00
|
|
|
use std::f32::consts::PI;
|
2021-10-03 10:39:27 +00:00
|
|
|
|
|
|
|
fn window_conf() -> Conf {
|
|
|
|
Conf {
|
2021-10-03 19:23:32 +00:00
|
|
|
window_title: "Ants".to_string(),
|
2021-10-03 10:39:27 +00:00
|
|
|
fullscreen: false,
|
|
|
|
window_width: 1280,
|
|
|
|
window_height: 720,
|
|
|
|
..Default::default()
|
|
|
|
}
|
2021-10-03 10:19:24 +00:00
|
|
|
}
|
2021-10-03 10:39:27 +00:00
|
|
|
|
2021-10-03 19:23:32 +00:00
|
|
|
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<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()
|
|
|
|
}
|
|
|
|
|
2021-10-03 10:39:27 +00:00
|
|
|
#[macroquad::main(window_conf)]
|
|
|
|
async fn main() {
|
|
|
|
let map = load_texture("map_scaled.png").await.unwrap();
|
2021-10-03 19:23:32 +00:00
|
|
|
let mut ants = init_ants(16);
|
2021-10-03 10:39:27 +00:00
|
|
|
loop {
|
2021-10-03 19:23:32 +00:00
|
|
|
// actions
|
|
|
|
ants.iter_mut().for_each(|ant| ant.walk());
|
2021-10-03 10:39:27 +00:00
|
|
|
if is_key_down(KeyCode::Escape) || is_quit_requested() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-03 19:23:32 +00:00
|
|
|
// render
|
|
|
|
clear_background(BEIGE);
|
|
|
|
draw_texture(map, 0., 0., WHITE);
|
|
|
|
ants.iter().for_each(|ant| ant.draw());
|
|
|
|
|
2021-10-03 10:39:27 +00:00
|
|
|
next_frame().await
|
|
|
|
}
|
2021-10-03 19:23:32 +00:00
|
|
|
}
|