use ggez::graphics::Image;
use ggez::{Context, GameResult};
use std::path::Path;
pub struct Resources {
pub background: Image,
pub shop_image: Image,
pub sun_images: Vec<Image>,
pub peashooter_images: Vec<Image>,
pub sunflower_images: Vec<Image>,
pub wallnut_images: Vec<Image>,
pub peashooter_card: Image,
pub sunflower_card: Image,
pub wallnut_card: Image,
pub zombies_walk1_images: Vec<Image>,
pub zombie_attack_images: Vec<Image>,
pub zombie_die_images: Vec<Image>,
pub zombie_head_images: Vec<Image>,
pub cone_zombie_walk_images: Vec<Image>,
pub cone_zombie_attack_images: Vec<Image>,
pub pea_image: Image,
}
fn load_animation_frames(
ctx: &mut Context,
path_pattern: &str,
frame_count: std::ops::RangeInclusive<usize>,
asset_name: &str,
) -> GameResult<Vec<Image>> {
let mut frames = Vec::new();
for i in frame_count {
let path = path_pattern.replace("{}", &i.to_string());
let full_path = Path::new("Resource").join(path.trim_start_matches('/'));
if full_path.exists() {
match Image::new(ctx, &path) {
Ok(img) => frames.push(img),
Err(e) => println!(
"Warning: Failed to load {} image {}: {}",
asset_name, path, e
),
}
} else {
println!("Warning: {} image not found: {}", asset_name, path);
}
}
Ok(frames)
}
impl Resources {
pub fn new(ctx: &mut Context) -> GameResult<Resources> {
let background = Image::new(ctx, "/other_image/Background.png")?;
let shop_image = Image::new(ctx, "/other_image/Shop.png")?;
let sun_images = load_animation_frames(ctx, "/other_image/sun/Sun{}.png", 0..=21, "Sun")?;
let peashooter_images = load_animation_frames(ctx, "/plants/Peashooter/{}.png", 1..=13, "Peashooter")?;
let sunflower_images = load_animation_frames(ctx, "/plants/SunFlower/{}.png", 1..=18, "Sunflower")?;
let wallnut_images = load_animation_frames(ctx, "/plants/WallNut/WallnutFull/{}.png", 1..=16, "Wallnut")?;
let peashooter_card = Image::new(ctx, "/plants/Peashooter.png")?;
let sunflower_card = Image::new(ctx, "/plants/SunFlower.png")?;
let wallnut_card = Image::new(ctx, "/plants/WallNut.png")?;
let zombies_walk1_images = load_animation_frames(ctx, "/zombies/ZombieWalk1/{}.png", 1..=22, "Zombie walk")?;
let zombie_attack_images = load_animation_frames(ctx, "/zombies/ZombieAttack/{}.png", 1..=21, "Zombie attack")?;
let zombie_die_images = load_animation_frames(ctx, "/zombies/ZombieDie/{}.png", 1..=10, "Zombie die")?;
let zombie_head_images = load_animation_frames(ctx, "/zombies/ZombieHead/{}.png", 1..=12, "Zombie head fall")?;
let cone_zombie_walk_images = load_animation_frames(ctx, "/zombies/ConeZombieWalk/{}.png", 1..=21, "Cone Zombie walk")?;
let cone_zombie_attack_images = load_animation_frames(ctx, "/zombies/ConeZombieAttack/{}.png", 1..=11, "Cone Zombie attack")?;
let pea_image = Image::new(ctx, "/plants/Pea.png")?;
Ok(Resources {
background,
shop_image,
sun_images,
peashooter_images,
sunflower_images,
wallnut_images,
peashooter_card,
sunflower_card,
wallnut_card,
zombies_walk1_images,
zombie_attack_images,
zombie_die_images,
zombie_head_images,
cone_zombie_walk_images,
cone_zombie_attack_images,
pea_image,
})
}
}