1
Fork 0

red vs blue

This commit is contained in:
Andy Killorin 2026-11-09 14:06:27 -05:00
parent b279cc91d7
commit 1319968095
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -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,21 +149,26 @@ async fn main() {
food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food
let mut sensors = Vec::new();
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));
let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None};
if detect_kin.is_some() {sensor = Sensor::Kin}
println!("Food detection: {:?}",detect_food);
println!("Kin detection: {:?}",detect_kin);
sensors.push(sensor);
}
for (idx, robot) in robots.iter_mut().enumerate() {
robot.policy(&sensors[idx]);
robot.draw();
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));
let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None};
if detect_kin.is_some() {sensor = Sensor::Kin}
println!("Food detection: {:?}",detect_food);
println!("Kin detection: {:?}",detect_kin);
sensors.push(sensor);
}
for (idx, robot) in robots.iter_mut().enumerate() {
robot.policy(&sensors[idx]);
robot.draw(color);
}
}
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
}
}
}