use new draw function which covers bigger area

This commit is contained in:
Felix Ableitner 2021-10-07 03:19:17 +02:00
parent deddb20fad
commit 8b49ae2792
4 changed files with 18 additions and 53 deletions

View file

@ -1,7 +1,6 @@
use crate::constants::ANT_SPEED; use crate::constants::ANT_SPEED;
use crate::constants::MAP_HEIGHT; use crate::constants::MAP_HEIGHT;
use crate::constants::MAP_WIDTH; use crate::constants::MAP_WIDTH;
use crate::Drawable;
use macroquad::prelude::*; use macroquad::prelude::*;
use macroquad::rand::RandomRange; use macroquad::rand::RandomRange;
use std::f32::consts::PI; use std::f32::consts::PI;
@ -43,19 +42,23 @@ impl Ant {
} }
self.pos.x = self.pos.x.clamp(0., (MAP_WIDTH - 1) as f32); self.pos.x = self.pos.x.clamp(0., (MAP_WIDTH - 1) as f32);
self.pos.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32); self.pos.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32);
// TODO: change color over time
trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED);
} }
fn sense(&self, trail_map: &mut Image) { fn sense(&self, trail_map: &mut Image) {
trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW); trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW);
trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW); trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW);
trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW); trail_map.set_pixel(self.dir.sin() as u32, -self.dir.cos() as u32, YELLOW);
} }
} // TODO: change color over time or based on other factors
pub(crate) fn draw(&self, trail_map: &mut Image) {
impl Drawable for Ant { let radius: i32 = 3;
fn draw(&self) { for x in -radius..radius {
//draw_circle(self.pos.x, self.pos.y, 1., WHITE); for y in -radius..radius {
let x = (self.pos.x as i32 + x) as u32;
let y = (self.pos.y as i32 + y) as u32;
if x < (MAP_WIDTH - 1) && y < (MAP_HEIGHT - 1) {
trail_map.set_pixel(x, y, RED);
}
}
}
} }
} }

View file

@ -1,3 +1,3 @@
pub static ANT_SPEED: f32 = 200.; pub static ANT_SPEED: f32 = 20.;
pub const MAP_WIDTH: u32 = 1280; pub const MAP_WIDTH: u32 = 1280;
pub const MAP_HEIGHT: u32 = 720; pub const MAP_HEIGHT: u32 = 720;

View file

@ -1,34 +0,0 @@
use crate::Drawable;
use macroquad::color::GREEN;
use macroquad::math::Vec2;
use macroquad::prelude::*;
pub(crate) struct Food {
pos: Vec2,
}
#[allow(dead_code)]
impl Food {
pub(crate) fn new(pos: Vec2) -> Self {
Food { pos }
}
#[rustfmt::skip]
pub(crate) fn init() -> Vec<Self> {
vec![
Food::new(Vec2::new(335., 75.)),
Food::new(Vec2::new(340., 75.)),
Food::new(Vec2::new(335., 80.)),
Food::new(Vec2::new(340., 80.)),
Food::new(Vec2::new(615., 195.)),
Food::new(Vec2::new(620., 195.)),
Food::new(Vec2::new(615., 200.)),
Food::new(Vec2::new(620., 200.)),
]
}
}
impl Drawable for Food {
fn draw(&self) {
draw_circle(self.pos.x, self.pos.y, 1., GREEN);
}
}

View file

@ -2,7 +2,6 @@ extern crate macroquad;
mod ant; mod ant;
mod constants; mod constants;
mod food;
use crate::ant::Ant; use crate::ant::Ant;
use constants::{MAP_HEIGHT, MAP_WIDTH}; use constants::{MAP_HEIGHT, MAP_WIDTH};
@ -18,15 +17,11 @@ fn window_conf() -> Conf {
} }
} }
trait Drawable {
fn draw(&self);
}
#[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 mut trail_map = Image::gen_image_color(MAP_WIDTH as u16, MAP_HEIGHT as u16, BLACK); let mut trail_map = Image::gen_image_color(MAP_WIDTH as u16, MAP_HEIGHT as u16, BLACK);
let mut ants = Ant::init(1024); let mut ants = Ant::init(8);
loop { loop {
// fade trails (doesnt really work) // fade trails (doesnt really work)
@ -41,8 +36,10 @@ async fn main() {
} }
} }
// actions // actions
ants.iter_mut() ants.iter_mut().for_each(|ant| {
.for_each(|ant| ant.walk(frame_time, &mut trail_map)); ant.walk(frame_time, &mut trail_map);
ant.draw(&mut trail_map);
});
if is_key_down(KeyCode::Escape) || is_quit_requested() { if is_key_down(KeyCode::Escape) || is_quit_requested() {
return; return;
} }
@ -56,7 +53,6 @@ async fn main() {
clear_background(BLACK); clear_background(BLACK);
let texture = Texture2D::from_image(&trail_map); let texture = Texture2D::from_image(&trail_map);
draw_texture(texture, 0., 0., WHITE); draw_texture(texture, 0., 0., WHITE);
ants.iter().for_each(|ant| ant.draw());
next_frame().await next_frame().await
} }