feat: shared blinking textures for power pellets

This commit is contained in:
2025-07-24 16:23:09 -05:00
parent 61ca537909
commit 9b441fa35c
4 changed files with 106 additions and 27 deletions

View File

@@ -4,6 +4,7 @@ use crate::entity::direction::Direction;
use crate::entity::{Entity, Renderable, StaticEntity};
use crate::map::Map;
use crate::texture::atlas::AtlasTexture;
use crate::texture::blinking::BlinkingTexture;
use crate::texture::FrameDrawn;
use glam::{IVec2, UVec2};
use sdl2::{render::Canvas, video::Window};
@@ -17,19 +18,32 @@ pub enum EdibleKind {
Fruit(FruitType),
}
pub enum EdibleSprite {
Pellet(Rc<Box<dyn FrameDrawn>>),
PowerPellet(Rc<RefCell<BlinkingTexture>>),
}
pub struct Edible {
pub base: StaticEntity,
pub kind: EdibleKind,
pub sprite: Rc<AtlasTexture>,
pub sprite: EdibleSprite,
}
impl Edible {
pub fn new(kind: EdibleKind, cell_position: UVec2, sprite: Rc<AtlasTexture>) -> Self {
pub fn new_pellet(cell_position: UVec2, sprite: Rc<Box<dyn FrameDrawn>>) -> Self {
let pixel_position = Map::cell_to_pixel(cell_position);
Edible {
base: StaticEntity::new(pixel_position, cell_position),
kind,
sprite,
kind: EdibleKind::Pellet,
sprite: EdibleSprite::Pellet(sprite),
}
}
pub fn new_power_pellet(cell_position: UVec2, sprite: Rc<RefCell<BlinkingTexture>>) -> Self {
let pixel_position = Map::cell_to_pixel(cell_position);
Edible {
base: StaticEntity::new(pixel_position, cell_position),
kind: EdibleKind::PowerPellet,
sprite: EdibleSprite::PowerPellet(sprite),
}
}
@@ -48,16 +62,19 @@ impl Entity for Edible {
impl Renderable for Edible {
fn render(&self, canvas: &mut Canvas<Window>) {
let pos = self.base.pixel_position;
self.sprite.render(canvas, pos, Direction::Right, Some(0));
match &self.sprite {
EdibleSprite::Pellet(sprite) => sprite.render(canvas, pos, Direction::Right, Some(0)),
EdibleSprite::PowerPellet(sprite) => sprite.borrow().render(canvas, pos, Direction::Right, Some(0)),
}
}
}
/// Reconstruct all edibles from the original map layout
pub fn reconstruct_edibles(
map: Rc<RefCell<Map>>,
pellet_sprite: Rc<AtlasTexture>,
power_pellet_sprite: Rc<AtlasTexture>,
_fruit_sprite: Rc<AtlasTexture>,
pellet_sprite: Rc<Box<dyn FrameDrawn>>,
power_pellet_sprite: Rc<RefCell<BlinkingTexture>>,
_fruit_sprite: Rc<Box<dyn FrameDrawn>>,
) -> Vec<Edible> {
let mut edibles = Vec::new();
for x in 0..BOARD_WIDTH {
@@ -65,14 +82,10 @@ pub fn reconstruct_edibles(
let tile = map.borrow().get_tile(IVec2::new(x as i32, y as i32));
match tile {
Some(MapTile::Pellet) => {
edibles.push(Edible::new(EdibleKind::Pellet, UVec2::new(x, y), Rc::clone(&pellet_sprite)));
edibles.push(Edible::new_pellet(UVec2::new(x, y), Rc::clone(&pellet_sprite)));
}
Some(MapTile::PowerPellet) => {
edibles.push(Edible::new(
EdibleKind::PowerPellet,
UVec2::new(x, y),
Rc::clone(&power_pellet_sprite),
));
edibles.push(Edible::new_power_pellet(UVec2::new(x, y), Rc::clone(&power_pellet_sprite)));
}
// Fruits can be added here if you have fruit positions
_ => {}