mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 03:15:48 -06:00
feat: smooth back-forth sprite frame ticks, sprite rotation
This commit is contained in:
@@ -4,8 +4,13 @@ use sdl2::{
|
||||
video::Window,
|
||||
};
|
||||
|
||||
use crate::direction::Direction;
|
||||
|
||||
pub struct AnimatedTexture<'a> {
|
||||
raw_texture: Texture<'a>,
|
||||
ticker: u32,
|
||||
reversed: bool,
|
||||
ticks_per_frame: u32,
|
||||
frame_count: u32,
|
||||
frame_width: u32,
|
||||
frame_height: u32,
|
||||
@@ -13,39 +18,66 @@ pub struct AnimatedTexture<'a> {
|
||||
|
||||
impl<'a> AnimatedTexture<'a> {
|
||||
pub fn new(
|
||||
texture: &'a Texture<'a>,
|
||||
texture:Texture<'a>,
|
||||
ticks_per_frame: u32,
|
||||
frame_count: u32,
|
||||
frame_width: u32,
|
||||
frame_height: u32,
|
||||
) -> Self {
|
||||
AnimatedTexture {
|
||||
raw_texture: texture,
|
||||
current_frame: 0,
|
||||
ticker: 0,
|
||||
reversed: false,
|
||||
ticks_per_frame,
|
||||
frame_count,
|
||||
frame_width,
|
||||
frame_height,
|
||||
}
|
||||
}
|
||||
|
||||
fn current_frame(&self) -> u32 {
|
||||
self.ticker / self.ticks_per_frame
|
||||
}
|
||||
|
||||
fn next_frame(&mut self) {
|
||||
self.current_frame = (self.current_frame + 1) % self.frame_count;
|
||||
if self.reversed {
|
||||
self.ticker -= 1;
|
||||
|
||||
if self.ticker == 0 {
|
||||
self.reversed = !self.reversed;
|
||||
}
|
||||
} else {
|
||||
self.ticker += 1;
|
||||
|
||||
if self.ticker > self.ticks_per_frame * self.frame_count {
|
||||
self.reversed = !self.reversed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_frame_rect(&self) -> Rect {
|
||||
Rect::new(
|
||||
(self.current_frame * self.frame_width) as i32,
|
||||
self.current_frame() as i32 * self.frame_width as i32,
|
||||
0,
|
||||
self.frame_width,
|
||||
self.frame_height,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn render(&mut self, canvas: &mut Canvas<Window>, position: (i32, i32)) {
|
||||
pub fn render(&mut self, canvas: &mut Canvas<Window>, position: (i32, i32), direction: Direction) {
|
||||
let frame_rect = self.get_frame_rect();
|
||||
let position_rect = Rect::new(position.0, position.1, self.frame_width, self.frame_height);
|
||||
|
||||
canvas
|
||||
.copy(&self.raw_texture, frame_rect, position_rect)
|
||||
.expect("Could not render sprite on canvas");
|
||||
.copy_ex(
|
||||
&self.raw_texture,
|
||||
Some(frame_rect),
|
||||
Some(position_rect),
|
||||
direction.angle(),
|
||||
None,
|
||||
false,
|
||||
false
|
||||
).expect("Could not render texture on canvas");
|
||||
|
||||
self.next_frame();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
pub fn angle(&self) -> f64 {
|
||||
match self {
|
||||
Direction::Right => 0f64,
|
||||
Direction::Down => 90f64,
|
||||
Direction::Left => 180f64,
|
||||
Direction::Up => 270f64,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,6 @@ pub trait Entity {
|
||||
fn position(&self) -> (i32, i32);
|
||||
// Returns the cell position of the entity (XY position within the grid)
|
||||
fn cell_position(&self) -> (u32, u32);
|
||||
// Tick the entity (move it, perform collision checks, etc)
|
||||
fn tick(&mut self);
|
||||
}
|
||||
@@ -10,16 +10,16 @@ pub struct Pacman<'a> {
|
||||
}
|
||||
|
||||
impl Pacman<'_> {
|
||||
pub fn new<'a>(starting_position: Option<(i32, i32)>, atlas: &'a Texture<'a>) -> Pacman<'a> {
|
||||
pub fn new<'a>(starting_position: Option<(i32, i32)>, atlas: Texture<'a>) -> Pacman<'a> {
|
||||
Pacman {
|
||||
position: starting_position.unwrap_or((0i32, 0i32)),
|
||||
direction: Direction::Right,
|
||||
sprite: AnimatedTexture::new(atlas, 2, 24, 24),
|
||||
sprite: AnimatedTexture::new(atlas, 4, 3, 32,32),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
|
||||
self.sprite.render(canvas, self.position);
|
||||
self.sprite.render(canvas, self.position, self.direction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,4 +38,21 @@ impl Entity for Pacman<'_> {
|
||||
let (x, y) = self.position();
|
||||
(x as u32 / 24, y as u32 / 24)
|
||||
}
|
||||
|
||||
fn tick(&mut self) {
|
||||
match self.direction {
|
||||
Direction::Right => {
|
||||
self.position.0 += 1;
|
||||
}
|
||||
Direction::Left => {
|
||||
self.position.0 -= 1;
|
||||
}
|
||||
Direction::Up => {
|
||||
self.position.1 -= 1;
|
||||
}
|
||||
Direction::Down => {
|
||||
self.position.1 += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user