red vs blue
This commit is contained in:
parent
b279cc91d7
commit
1319968095
1 changed files with 41 additions and 20 deletions
|
|
@ -36,12 +36,12 @@ impl Robot {
|
|||
Vec2::new(self.pose.rotation.re, self.pose.rotation.im)
|
||||
}
|
||||
|
||||
pub fn draw(&self) {
|
||||
pub fn draw(&self, color: Color) {
|
||||
let tl = self.pose.translation;
|
||||
let tl2 = self.view_end();
|
||||
draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK);
|
||||
draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE);
|
||||
draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, RED);
|
||||
draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, color);
|
||||
}
|
||||
|
||||
fn view_end(&self) -> Vec2 {
|
||||
|
|
@ -108,16 +108,32 @@ impl Food {
|
|||
async fn main() {
|
||||
|
||||
let mut robots = Vec::new();
|
||||
robots.push(Robot::new(Vec2 { x: 100.0, y: 100.0 }));
|
||||
robots.push(Robot::new(Vec2 { x: 140.0, y: 100.0 }));
|
||||
robots.push(Robot::new(Vec2 { x: 140.0, y: 200.0 }));
|
||||
robots.push(Robot::new(Vec2 { x: 180.0, y: 150.0 }));
|
||||
let mut blu_robots = Vec::new();
|
||||
|
||||
for i in 5..20 {
|
||||
robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 }));
|
||||
}
|
||||
|
||||
for i in 5..20 {
|
||||
blu_robots.push(Robot::new(Vec2 { x: 850.0, y: i as f32*50.0 }));
|
||||
}
|
||||
|
||||
let mut food = Vec::new();
|
||||
food.push(Food::new(Vec2 { x: 160.0, y: 100.0 }));
|
||||
food.push(Food::new(Vec2 { x: 360.0, y: 100.0 }));
|
||||
food.push(Food::new(Vec2 { x: 360.0, y: 300.0 }));
|
||||
food.push(Food::new(Vec2 { x: 360.0, y: 400.0 }));
|
||||
food.push(Food::new(Vec2 { x: 460.0, y: 400.0 }));
|
||||
food.push(Food::new(Vec2 { x: 260.0, y: 400.0 }));
|
||||
|
||||
for j in 10..30 {
|
||||
for i in 5..20 {
|
||||
food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let mut loo = 0;
|
||||
loop {
|
||||
clear_background(LIGHTGRAY);
|
||||
|
||||
|
|
@ -133,7 +149,8 @@ async fn main() {
|
|||
food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food
|
||||
|
||||
let mut sensors = Vec::new();
|
||||
for robot in &robots {
|
||||
for (robots, color) in [(&mut robots, RED), (&mut blu_robots, BLUE)] {
|
||||
for robot in &*robots {
|
||||
println!("loop");
|
||||
let detect_food = robot.detect(food.iter().map(Food::collider));
|
||||
let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider));
|
||||
|
|
@ -145,9 +162,13 @@ async fn main() {
|
|||
}
|
||||
for (idx, robot) in robots.iter_mut().enumerate() {
|
||||
robot.policy(&sensors[idx]);
|
||||
robot.draw();
|
||||
robot.draw(color);
|
||||
}
|
||||
}
|
||||
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);
|
||||
loo += 1;
|
||||
if loo %2 == 0 {
|
||||
next_frame().await // TODO: framerate cap
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue