Compare commits

..

1 Commits

Author SHA1 Message Date
443afb1223 feat: new PacMan entity, entity trait implementation 2025-06-17 11:50:04 -05:00

41
src/pacman.rs Normal file
View File

@@ -0,0 +1,41 @@
use sdl2::{render::{Canvas, Texture}, video::Window};
use crate::{direction::Direction, entity::Entity, animation::AnimatedTexture};
pub struct Pacman<'a> {
// Absolute position on the board (precise)
pub position: (i32, i32),
pub direction: Direction,
sprite: AnimatedTexture<'a>,
}
impl Pacman<'_> {
pub fn new<'a>(starting_position: Option<(i32, i32)>, atlas: &'a Texture<'a>) -> Pacman<'a> {
Pacman {
position: starting_position.unwrap_or((0i32, 0i32)),
direction: Direction::Right,
sprite: AnimatedTexture::new(atlas, 2, 24, 24),
}
}
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
self.sprite.render(canvas, self.position);
}
}
impl Entity for Pacman<'_> {
fn is_colliding(&self, other: &dyn Entity) -> bool {
let (x, y) = self.position();
let (other_x, other_y) = other.position();
x == other_x && y == other_y
}
fn position(&self) -> (i32, i32) {
self.position
}
fn cell_position(&self) -> (u32, u32) {
let (x, y) = self.position();
(x as u32 / 24, y as u32 / 24)
}
}