stop when hitting a wall

This commit is contained in:
Felix Ableitner 2021-10-03 21:39:24 +02:00
parent 8052976133
commit 2240666164

View file

@ -12,6 +12,7 @@ fn window_conf() -> Conf {
} }
static ANT_SPEED: f32 = 20.; static ANT_SPEED: f32 = 20.;
static FLOOR_COLOR: Color = Color::new(0.46666667, 0.38431373, 0.3019608, 1.);
struct Ant { struct Ant {
pos: Vec2, pos: Vec2,
@ -19,9 +20,13 @@ struct Ant {
} }
impl Ant { impl Ant {
fn walk(&mut self) { fn walk(&mut self, map_data: &Image) {
let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos());
self.pos += acceleration * ANT_SPEED * get_frame_time(); 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) { fn draw(&self) {
draw_circle(self.pos.x, self.pos.y, 1., WHITE); draw_circle(self.pos.x, self.pos.y, 1., WHITE);
@ -42,10 +47,11 @@ fn init_ants(num_ants: i32) -> Vec<Ant> {
#[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();
let map_data = map.get_texture_data();
let mut ants = init_ants(16); let mut ants = init_ants(16);
loop { loop {
// actions // actions
ants.iter_mut().for_each(|ant| ant.walk()); ants.iter_mut().for_each(|ant| ant.walk(&map_data));
if is_key_down(KeyCode::Escape) || is_quit_requested() { if is_key_down(KeyCode::Escape) || is_quit_requested() {
return; return;
} }