mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-10 22:07:55 -06:00
50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
//! This module provides a simple animation and atlas system for textures.
|
|
use anyhow::Result;
|
|
use sdl2::render::WindowCanvas;
|
|
|
|
use crate::texture::sprite::AtlasTile;
|
|
|
|
/// An animated texture using a texture atlas.
|
|
#[derive(Clone)]
|
|
pub struct AnimatedTexture {
|
|
pub frames: Vec<AtlasTile>,
|
|
pub ticks_per_frame: u32,
|
|
pub ticker: u32,
|
|
pub reversed: bool,
|
|
pub paused: bool,
|
|
}
|
|
|
|
impl AnimatedTexture {
|
|
pub fn new(frames: Vec<AtlasTile>, ticks_per_frame: u32) -> Self {
|
|
AnimatedTexture {
|
|
frames,
|
|
ticks_per_frame,
|
|
ticker: 0,
|
|
reversed: false,
|
|
paused: false,
|
|
}
|
|
}
|
|
|
|
/// Advances the animation by one tick, unless paused.
|
|
pub fn tick(&mut self) {
|
|
if self.paused || self.ticks_per_frame == 0 {
|
|
return;
|
|
}
|
|
|
|
self.ticker += 1;
|
|
}
|
|
|
|
pub fn current_tile(&self) -> &AtlasTile {
|
|
if self.ticks_per_frame == 0 {
|
|
return &self.frames[0];
|
|
}
|
|
let frame_index = (self.ticker / self.ticks_per_frame) as usize % self.frames.len();
|
|
&self.frames[frame_index]
|
|
}
|
|
|
|
pub fn render(&self, canvas: &mut WindowCanvas, dest: sdl2::rect::Rect) -> Result<()> {
|
|
let tile = self.current_tile();
|
|
tile.render(canvas, dest)
|
|
}
|
|
}
|