mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-14 02:12:26 -06:00
reformat: improve AnimatedTexture API with paused animation abilities
This commit is contained in:
@@ -38,11 +38,13 @@ impl<'a> AnimatedTexture<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the current frame number
|
||||||
fn current_frame(&self) -> u32 {
|
fn current_frame(&self) -> u32 {
|
||||||
self.ticker / self.ticks_per_frame
|
self.ticker / self.ticks_per_frame
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_frame(&mut self) {
|
// Move to the next frame. If we are at the end of the animation, reverse the direction
|
||||||
|
pub fn tick(&mut self) {
|
||||||
if self.reversed {
|
if self.reversed {
|
||||||
self.ticker -= 1;
|
self.ticker -= 1;
|
||||||
|
|
||||||
@@ -58,9 +60,14 @@ impl<'a> AnimatedTexture<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_frame_rect(&self) -> Rect {
|
// Calculate the frame rect (portion of the texture to render) for the given frame.
|
||||||
|
fn get_frame_rect(&self, frame: u32) -> Rect {
|
||||||
|
if frame >= self.frame_count {
|
||||||
|
panic!("Frame {} is out of bounds for this texture", frame);
|
||||||
|
}
|
||||||
|
|
||||||
Rect::new(
|
Rect::new(
|
||||||
self.current_frame() as i32 * self.frame_width as i32,
|
frame as i32 * self.frame_width as i32,
|
||||||
0,
|
0,
|
||||||
self.frame_width,
|
self.frame_width,
|
||||||
self.frame_height,
|
self.frame_height,
|
||||||
@@ -73,7 +80,18 @@ impl<'a> AnimatedTexture<'a> {
|
|||||||
position: (i32, i32),
|
position: (i32, i32),
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
) {
|
) {
|
||||||
let frame_rect = self.get_frame_rect();
|
self.render_static(canvas, position, direction, Some(self.current_frame()));
|
||||||
|
self.tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_static(
|
||||||
|
&mut self,
|
||||||
|
canvas: &mut Canvas<Window>,
|
||||||
|
position: (i32, i32),
|
||||||
|
direction: Direction,
|
||||||
|
frame: Option<u32>,
|
||||||
|
) {
|
||||||
|
let frame_rect = self.get_frame_rect(frame.unwrap_or(self.current_frame()));
|
||||||
let position_rect = Rect::new(
|
let position_rect = Rect::new(
|
||||||
position.0 + self.offset.0,
|
position.0 + self.offset.0,
|
||||||
position.1 + self.offset.1,
|
position.1 + self.offset.1,
|
||||||
@@ -92,7 +110,5 @@ impl<'a> AnimatedTexture<'a> {
|
|||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
.expect("Could not render texture on canvas");
|
.expect("Could not render texture on canvas");
|
||||||
|
|
||||||
self.next_frame();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user