use crate::ui::grid::{GRID_CELL_HEIGHT, GRID_CELL_WIDTH, GRID_START_X, GRID_START_Y};
use crate::entities::sun::Sun;
use crate::entities::pea::{Pea, PeaType};
use crate::plants::plant_trait::PlantTrait;
use crate::core::resources::Resources;
use ggez::graphics;
use crate::zombies::Zombie;
pub struct Peashooter {
shoot_timer: u64,
}
impl Peashooter {
pub fn new() -> Self {
Peashooter {
shoot_timer: 0,
}
}
}
const INITIAL_HEALTH: i32 = 300;
const COOLDOWN: u64 = 1400; const COST: i32 = 100;
impl PlantTrait for Peashooter {
fn get_initial_health(&self) -> i32 {
INITIAL_HEALTH
}
fn get_cooldown(&self) -> u64 {
COOLDOWN
}
fn get_frame_count(&self) -> usize {
13 }
fn update_action(&mut self, grid_x: usize, grid_y: usize, _suns: &mut Vec<Sun>, peas: &mut Vec<Pea>, zombies: &Vec<Zombie>) {
let has_zombie_in_row = zombies.iter().any(|zombie| {
!zombie.is_dying && zombie.row == grid_y && zombie.x > (GRID_START_X + ((grid_x as f32) * GRID_CELL_WIDTH/2.0))
});
if has_zombie_in_row {
let x = GRID_START_X + (grid_x as f32) * GRID_CELL_WIDTH + GRID_CELL_WIDTH * 0.8;
let y = GRID_START_Y + (grid_y as f32) * GRID_CELL_HEIGHT + GRID_CELL_HEIGHT * 0.3;
let new_pea = Pea::new(x, y, grid_y, PeaType::Normal);
peas.push(new_pea);
self.shoot_timer = 0;
}
}
fn get_cost(&self) -> i32 {
COST
}
fn get_card_image<'a>(&self, resources: &'a Resources) -> &'a graphics::Image {
&resources.peashooter_card
}
fn get_current_frame_image<'a>(&self, resources: &'a Resources, animation_frame: usize) -> &'a graphics::Image {
let frame_count = resources.peashooter_images.len();
if frame_count > 0 {
let safe_index = animation_frame % frame_count;
&resources.peashooter_images[safe_index]
} else {
&resources.peashooter_card
}
}
}