feat: speed property to PacMan

This commit is contained in:
2023-09-09 02:38:46 -05:00
parent 841943e121
commit 08e85ad377

View File

@@ -1,11 +1,15 @@
use sdl2::{render::{Canvas, Texture}, video::Window}; use sdl2::{
render::{Canvas, Texture},
video::Window,
};
use crate::{direction::Direction, entity::Entity, animation::AnimatedTexture}; use crate::{animation::AnimatedTexture, direction::Direction, entity::Entity};
pub struct Pacman<'a> { pub struct Pacman<'a> {
// Absolute position on the board (precise) // Absolute position on the board (precise)
pub position: (i32, i32), pub position: (i32, i32),
pub direction: Direction, pub direction: Direction,
speed: u32,
sprite: AnimatedTexture<'a>, sprite: AnimatedTexture<'a>,
} }
@@ -14,11 +18,12 @@ impl Pacman<'_> {
Pacman { Pacman {
position: starting_position.unwrap_or((0i32, 0i32)), position: starting_position.unwrap_or((0i32, 0i32)),
direction: Direction::Right, direction: Direction::Right,
sprite: AnimatedTexture::new(atlas, 4, 3, 32,32), speed: 2,
sprite: AnimatedTexture::new(atlas, 4, 3, 32, 32),
} }
} }
pub fn render(&mut self, canvas: &mut Canvas<Window>) { pub fn render(&mut self, canvas: &mut Canvas<Window>) {
self.sprite.render(canvas, self.position, self.direction); self.sprite.render(canvas, self.position, self.direction);
} }
} }
@@ -40,19 +45,20 @@ impl Entity for Pacman<'_> {
} }
fn tick(&mut self) { fn tick(&mut self) {
let speed = self.speed as i32;
match self.direction { match self.direction {
Direction::Right => { Direction::Right => {
self.position.0 += 1; self.position.0 += speed;
} }
Direction::Left => { Direction::Left => {
self.position.0 -= 1; self.position.0 -= speed;
} }
Direction::Up => { Direction::Up => {
self.position.1 -= 1; self.position.1 -= speed;
} }
Direction::Down => { Direction::Down => {
self.position.1 += 1; self.position.1 += speed;
} }
} }
} }
} }