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)
|
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 tl = self.pose.translation;
|
||||||
let tl2 = self.view_end();
|
let tl2 = self.view_end();
|
||||||
draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK);
|
draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK);
|
||||||
draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE);
|
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 {
|
fn view_end(&self) -> Vec2 {
|
||||||
|
|
@ -108,16 +108,32 @@ impl Food {
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|
||||||
let mut robots = Vec::new();
|
let mut robots = Vec::new();
|
||||||
robots.push(Robot::new(Vec2 { x: 100.0, y: 100.0 }));
|
let mut blu_robots = Vec::new();
|
||||||
robots.push(Robot::new(Vec2 { x: 140.0, y: 100.0 }));
|
|
||||||
robots.push(Robot::new(Vec2 { x: 140.0, y: 200.0 }));
|
for i in 5..20 {
|
||||||
robots.push(Robot::new(Vec2 { x: 180.0, y: 150.0 }));
|
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();
|
let mut food = Vec::new();
|
||||||
food.push(Food::new(Vec2 { x: 160.0, y: 100.0 }));
|
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: 100.0 }));
|
||||||
food.push(Food::new(Vec2 { x: 360.0, y: 300.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 {
|
loop {
|
||||||
clear_background(LIGHTGRAY);
|
clear_background(LIGHTGRAY);
|
||||||
|
|
||||||
|
|
@ -133,21 +149,26 @@ async fn main() {
|
||||||
food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food
|
food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food
|
||||||
|
|
||||||
let mut sensors = Vec::new();
|
let mut sensors = Vec::new();
|
||||||
for robot in &robots {
|
for (robots, color) in [(&mut robots, RED), (&mut blu_robots, BLUE)] {
|
||||||
println!("loop");
|
for robot in &*robots {
|
||||||
let detect_food = robot.detect(food.iter().map(Food::collider));
|
println!("loop");
|
||||||
let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider));
|
let detect_food = robot.detect(food.iter().map(Food::collider));
|
||||||
let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None};
|
let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider));
|
||||||
if detect_kin.is_some() {sensor = Sensor::Kin}
|
let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None};
|
||||||
println!("Food detection: {:?}",detect_food);
|
if detect_kin.is_some() {sensor = Sensor::Kin}
|
||||||
println!("Kin detection: {:?}",detect_kin);
|
println!("Food detection: {:?}",detect_food);
|
||||||
sensors.push(sensor);
|
println!("Kin detection: {:?}",detect_kin);
|
||||||
}
|
sensors.push(sensor);
|
||||||
for (idx, robot) in robots.iter_mut().enumerate() {
|
}
|
||||||
robot.policy(&sensors[idx]);
|
for (idx, robot) in robots.iter_mut().enumerate() {
|
||||||
robot.draw();
|
robot.policy(&sensors[idx]);
|
||||||
|
robot.draw(color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);
|
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);
|
||||||
next_frame().await // TODO: framerate cap
|
loo += 1;
|
||||||
|
if loo %2 == 0 {
|
||||||
|
next_frame().await // TODO: framerate cap
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue