place food, add drawable trait
This commit is contained in:
parent
b9bfca0900
commit
b7101f46c6
4 changed files with 84 additions and 33 deletions
58
src/ant.rs
58
src/ant.rs
|
@ -1,39 +1,41 @@
|
|||
use crate::constants::{ANT_SPEED, FLOOR_COLOR};
|
||||
use crate::Drawable;
|
||||
use macroquad::prelude::*;
|
||||
use std::f32::consts::PI;
|
||||
use crate::constants::{FLOOR_COLOR, ANT_SPEED};
|
||||
use macroquad::rand::RandomRange;
|
||||
use std::f32::consts::PI;
|
||||
|
||||
pub(crate) struct Ant {
|
||||
pos: Vec2,
|
||||
dir: f32,
|
||||
pos: Vec2,
|
||||
dir: f32,
|
||||
}
|
||||
|
||||
impl Ant {
|
||||
pub(crate)fn walk(&mut self, map_data: &Image) {
|
||||
let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32;
|
||||
self.dir = self.dir + new_dir;
|
||||
self.dir = self.dir.clamp(0., PI * 2.);
|
||||
|
||||
|
||||
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 init(num: i32) -> Vec<Self> {
|
||||
let dir_steps = PI * 2. / num as f32;
|
||||
(0..num)
|
||||
.into_iter()
|
||||
.map(|n| Ant {
|
||||
pos: Vec2::new(400., 200.),
|
||||
dir: n as f32 * dir_steps,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
pub(crate) fn walk(&mut self, map_data: &Image) {
|
||||
let new_dir = PI / 8. * RandomRange::gen_range(-2, 2) as f32;
|
||||
self.dir += new_dir;
|
||||
self.dir = self.dir.clamp(0., PI * 2.);
|
||||
|
||||
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()
|
||||
impl Drawable for Ant {
|
||||
fn draw(&self) {
|
||||
draw_circle(self.pos.x, self.pos.y, 1., BLACK);
|
||||
}
|
||||
}
|
|
@ -2,3 +2,5 @@ use macroquad::color::Color;
|
|||
|
||||
pub static ANT_SPEED: f32 = 20.;
|
||||
pub static FLOOR_COLOR: Color = Color::new(0.46666667, 0.38431373, 0.3019608, 1.);
|
||||
pub static SCREEN_WIDTH: i32 = 1280;
|
||||
pub static SCREEN_HEIGHT: i32 = 720;
|
||||
|
|
33
src/food.rs
Normal file
33
src/food.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use crate::Drawable;
|
||||
use macroquad::color::GREEN;
|
||||
use macroquad::math::Vec2;
|
||||
use macroquad::prelude::*;
|
||||
|
||||
pub(crate) struct Food {
|
||||
pos: Vec2,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
22
src/main.rs
22
src/main.rs
|
@ -1,23 +1,32 @@
|
|||
mod ant;
|
||||
mod constants;
|
||||
mod food;
|
||||
|
||||
use macroquad::prelude::*;use crate::ant::init_ants;
|
||||
use crate::ant::Ant;
|
||||
use crate::constants::{SCREEN_HEIGHT, SCREEN_WIDTH};
|
||||
use crate::food::Food;
|
||||
use macroquad::prelude::*;
|
||||
|
||||
fn window_conf() -> Conf {
|
||||
Conf {
|
||||
window_title: "Ants".to_string(),
|
||||
fullscreen: false,
|
||||
window_width: 1280,
|
||||
window_height: 720,
|
||||
window_width: SCREEN_WIDTH,
|
||||
window_height: SCREEN_HEIGHT,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
trait Drawable {
|
||||
fn draw(&self);
|
||||
}
|
||||
|
||||
#[macroquad::main(window_conf)]
|
||||
async fn main() {
|
||||
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 = Ant::init(16);
|
||||
let mut food = Food::init();
|
||||
|
||||
loop {
|
||||
// actions
|
||||
|
@ -25,11 +34,16 @@ async fn main() {
|
|||
if is_key_down(KeyCode::Escape) || is_quit_requested() {
|
||||
return;
|
||||
}
|
||||
if is_mouse_button_down(MouseButton::Left) {
|
||||
let mouse = mouse_position();
|
||||
food.push(Food::new(Vec2::new(mouse.0, mouse.1)));
|
||||
}
|
||||
|
||||
// render
|
||||
clear_background(BEIGE);
|
||||
draw_texture(map, 0., 0., WHITE);
|
||||
ants.iter().for_each(|ant| ant.draw());
|
||||
food.iter().for_each(|food| food.draw());
|
||||
|
||||
next_frame().await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue