1
Fork 0

keep score

This commit is contained in:
Andy Killorin 2026-11-09 14:42:01 -05:00
parent 6ebb6753fd
commit 5e72d94080
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -1,4 +1,5 @@
use std::cmp::min; use std::cmp::min;
use std::collections::HashMap;
use std::ptr; use std::ptr;
use macroquad::prelude::*; use macroquad::prelude::*;
@ -99,10 +100,10 @@ impl Food {
(&self.pose, &self.collider) (&self.pose, &self.collider)
} }
fn detect<'a>(&self, things: impl Iterator<Item = (&'a Pose, &'a dyn Shape)>) -> Option<f32> { fn detect<'a>(&self, things: impl Iterator<Item = &'a Robot>) -> Option<Color> {
things things
.map(|thing| contact(&self.pose, &self.collider, thing.0, thing.1, 0.0).unwrap()).flatten() .map(|robot| contact(&self.pose, &self.collider, &robot.pose, &robot.collider, 0.0).unwrap().map(|a| (a,robot.team))).flatten()
.map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) .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 robots = Vec::new();
let mut score = HashMap::new();
for i in 5..20 { 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: 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(); 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: 460.0, y: 400.0 }));
food.push(Food::new(Vec2 { x: 260.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 { for i in 5..20 {
food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 })); 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; let mut loo = 0;
loop { loop {
clear_background(LIGHTGRAY); clear_background(LIGHTGRAY);
for food in &mut food{ for food in &mut food{
if let Some(detect_robot) = food.detect(robots.iter().map(Robot::collider)) { if let Some(team) = food.detect(robots.iter()) {
println!("robot detected in contact"); if food.alive {
food.alive = false; score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1);
println!("robot detected in contact");
food.alive = false;
}
} }
food.draw(); food.draw();
// TODO: die upon collision with robot // TODO: die upon collision with robot
@ -178,7 +188,8 @@ async fn main() {
robot.policy(&sensors[idx]); robot.policy(&sensors[idx]);
robot.draw(); 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; loo += 1;
if loo %2 == 0 { if loo %2 == 0 {
next_frame().await // TODO: framerate cap next_frame().await // TODO: framerate cap