use crate::config::CONFIG;
use bracket_lib::{
random::RandomNumberGenerator,
terminal::{BTerm, Rect, WHITE},
};
pub(crate) struct Pipe {
pub x: f64,
pub y: i32,
pub passed: bool,
}
impl Pipe {
pub fn new() -> Pipe {
Pipe {
x: CONFIG.width as f64,
y: (RandomNumberGenerator::new().rand::<u32>() % (CONFIG.height - CONFIG.pipe_mouth_height)) as i32,
passed: false,
}
}
pub fn update(&mut self) {
self.x -= CONFIG.pipe_speed;
if (self.x + CONFIG.pipe_width as f64) < CONFIG.width as f64 / 2. {
self.passed = true;
}
}
pub fn collide(&self, x: f64, y: f64) -> bool {
self.x <= x
&& x <= self.x + CONFIG.pipe_width as f64
&& (y < self.y as f64 || y > (self.y + CONFIG.pipe_mouth_height as i32) as f64)
}
pub fn print(&self, ctx: &mut BTerm) {
ctx.fill_region(
Rect::with_size(
self.x as i32,
0,
CONFIG.pipe_width as i32,
CONFIG.height as i32 - (self.y + CONFIG.pipe_mouth_height as i32),
),
0,
WHITE,
WHITE,
);
ctx.fill_region(
Rect::with_size(
self.x as i32,
CONFIG.height as i32 - self.y,
CONFIG.pipe_width as i32,
self.y,
),
0,
WHITE,
WHITE,
);
}
}