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) 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,7 +149,8 @@ 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)] {
for robot in &*robots {
println!("loop"); println!("loop");
let detect_food = robot.detect(food.iter().map(Food::collider)); 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 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() { for (idx, robot) in robots.iter_mut().enumerate() {
robot.policy(&sensors[idx]); robot.policy(&sensors[idx]);
robot.draw(); robot.draw(color);
}
} }
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);
loo += 1;
if loo %2 == 0 {
next_frame().await // TODO: framerate cap next_frame().await // TODO: framerate cap
} }
} }
}