added bomb blink

This commit is contained in:
Niklas Kapelle 2023-10-16 00:12:18 +02:00
parent 08b0b655ed
commit 539e8ff21d
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

View File

@ -19,6 +19,10 @@ const FLASHED_X2: f64 = 0.00003113;
const FLASHED_X1: f64 = -0.17396053;
const FLASHED_X0: f64 = 244.51964267;
const BOMB_MAX_TIME: u64 = 40_000;
const BOMB_BLINK_DURATION: u64 = 300;
const BOMB_BLINK_SPEED: u64 = 700;
fn interpolate_flashed(time: u64) -> u8 {
if time == 0 {
return 0;
@ -33,7 +37,7 @@ fn resolve_time_from_flashed(flashed: u8) -> u64 {
let discriminant = FLASHED_X1.powi(2) - 4.0 * FLASHED_X2 * (FLASHED_X0 - flashed as f64);
let x = (-FLASHED_X1 - discriminant.sqrt()) / (2.0 * FLASHED_X2);
return x as u64;
return x.min(5000.0) as u64;
}
pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
@ -44,6 +48,9 @@ pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
let mut interpolated_flashed: u8;
let mut time_since_last_flashed: u64 = 0;
let mut last_up_planted: bool = false;
let mut time_bomb_planted: u64 = 0;
loop {
thread::sleep(std::time::Duration::from_millis(sleep_time));
@ -75,10 +82,6 @@ pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
last_up_flashed = upstream_flashed;
}
if upstream_flashed > 0 || interpolated_flashed > 0 {
println!("{},{}", upstream_flashed, interpolated_flashed);
}
let pack = build_led_package(
len,
(
@ -90,11 +93,31 @@ pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
socket.send_to(&pack, addr).unwrap();
// let planted = state.bomb_planted.load(Ordering::Relaxed);
let planted = true;
// if planted {
// let packet = build_led_package(len, (255, 0, 0));
// socket.send_to(&packet, addr).unwrap();
// continue;
// }
if planted != last_up_planted {
// A bomb has been planted or defused reset the timer
time_bomb_planted = 0;
last_up_planted = planted;
}
if planted {
time_bomb_planted += sleep_time;
println!("Time: {}", time_bomb_planted);
let should_blink = (time_bomb_planted) % (BOMB_BLINK_SPEED * (BOMB_MAX_TIME / time_bomb_planted).max(1)) < BOMB_BLINK_DURATION;
let color = if should_blink {
(255, 0, 0)
} else {
(0, 0, 0)
};
let pack = build_led_package(len, color);
socket.send_to(&pack, addr).unwrap();
}
}
}