59 lines
1.2 KiB
Rust
59 lines
1.2 KiB
Rust
#[derive(Clone)]
|
|
pub struct Enemy {
|
|
pub x: f32,
|
|
pub y: f32,
|
|
pub width: f32,
|
|
pub height: f32,
|
|
pub alive: bool,
|
|
}
|
|
|
|
impl Enemy {
|
|
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
|
|
Self { x, y, width, height, alive: true }
|
|
}
|
|
|
|
pub fn scroll(&mut self, speed: f32, dt: f32) {
|
|
self.x -= speed * dt;
|
|
}
|
|
|
|
pub fn is_off_screen(&self) -> bool {
|
|
self.x + self.width < 0.0
|
|
}
|
|
|
|
pub fn top(&self) -> f32 {
|
|
self.y
|
|
}
|
|
|
|
pub fn bottom(&self) -> f32 {
|
|
self.y + self.height
|
|
}
|
|
|
|
pub fn right(&self) -> f32 {
|
|
self.x + self.width
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn scroll_moves_enemy_left() {
|
|
let mut e = Enemy::new(200.0, 400.0, 40.0, 40.0);
|
|
e.scroll(200.0, 0.5);
|
|
assert!((e.x - 100.0).abs() < 1e-4);
|
|
}
|
|
|
|
#[test]
|
|
fn off_screen_when_right_edge_is_negative() {
|
|
let e = Enemy::new(-50.0, 400.0, 40.0, 40.0);
|
|
assert!(e.is_off_screen());
|
|
}
|
|
|
|
#[test]
|
|
fn not_off_screen_when_partially_visible() {
|
|
let e = Enemy::new(-20.0, 400.0, 40.0, 40.0);
|
|
assert!(!e.is_off_screen());
|
|
}
|
|
}
|