diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 3b49216..ca24220 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -1,4 +1,5 @@ use std::cmp::min; +use std::collections::HashMap; use std::ptr; use macroquad::prelude::*; @@ -99,10 +100,10 @@ impl Food { (&self.pose, &self.collider) } - fn detect<'a>(&self, things: impl Iterator) -> Option { + fn detect<'a>(&self, things: impl Iterator) -> Option { things - .map(|thing| contact(&self.pose, &self.collider, thing.0, thing.1, 0.0).unwrap()).flatten() - .map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) + .map(|robot| contact(&self.pose, &self.collider, &robot.pose, &robot.collider, 0.0).unwrap().map(|a| (a,robot.team))).flatten() + .map(|(contact,team)| (contact.point1.distance(self.pose.translation),team)).reduce(|x ,y| if x.0 < y.0 {x} else{y} ).map(|(_, team)| team) } } @@ -112,9 +113,11 @@ async fn main() { let mut robots = Vec::new(); + let mut score = HashMap::new(); + for i in 5..20 { robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 }, RED)); - robots.push(Robot::new(Vec2 { x: 850.0, y: i as f32*50.0 }, BLUE)); + robots.push(Robot::new(Vec2 { x: 1150.0, y: i as f32*50.0 }, BLUE)); } let mut food = Vec::new(); @@ -125,21 +128,28 @@ async fn main() { 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 j in 5..50 { for i in 5..20 { food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 })); } } + fn color_hash(color: Color) -> [u32; 4] { + color.to_vec().to_array().map(|f| f.to_bits()) + } + let mut loo = 0; loop { clear_background(LIGHTGRAY); for food in &mut food{ - if let Some(detect_robot) = food.detect(robots.iter().map(Robot::collider)) { - println!("robot detected in contact"); - food.alive = false; + if let Some(team) = food.detect(robots.iter()) { + if food.alive { + score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1); + println!("robot detected in contact"); + food.alive = false; + } } food.draw(); // TODO: die upon collision with robot @@ -178,7 +188,8 @@ async fn main() { robot.policy(&sensors[idx]); robot.draw(); } - draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); + draw_text(format!("RED: {}", score.get(&color_hash(RED)).unwrap_or(&0)), 20.0, 20.0, 30.0, DARKGRAY); + draw_text(format!("BLUE: {}", score.get(&color_hash(BLUE)).unwrap_or(&0)), 20.0, 90.0, 30.0, DARKGRAY); loo += 1; if loo %2 == 0 { next_frame().await // TODO: framerate cap