Compare commits

...

1 Commits

Author SHA1 Message Date
da98b54216 feat: wall collisions 2025-06-17 11:51:49 -05:00
3 changed files with 39 additions and 1 deletions

View File

@@ -15,4 +15,13 @@ impl Direction {
Direction::Up => 270f64,
}
}
pub fn offset(&self) -> (i32, i32) {
match self {
Direction::Right => (1, 0),
Direction::Down => (0, 1),
Direction::Left => (-1, 0),
Direction::Up => (0, -1),
}
}
}

15
src/helper.rs Normal file
View File

@@ -0,0 +1,15 @@
pub fn is_adjacent(a: (u32, u32), b: (u32, u32), diagonal: bool) -> bool {
let (ax, ay) = a;
let (bx, by) = b;
if diagonal {
(ax == bx && (ay == by + 1 || ay == by - 1))
|| (ay == by && (ax == bx + 1 || ax == bx - 1))
|| (ax == bx + 1 && ay == by + 1)
|| (ax == bx + 1 && ay == by - 1)
|| (ax == bx - 1 && ay == by + 1)
|| (ax == bx - 1 && ay == by - 1)
} else {
(ax == bx && (ay == by + 1 || ay == by - 1))
|| (ay == by && (ax == bx + 1 || ax == bx - 1))
}
}

View File

@@ -4,6 +4,7 @@ use sdl2::{
};
use crate::{
constants::{BOARD, MapTile},
animation::AnimatedTexture, constants::CELL_SIZE, direction::Direction, entity::Entity,
modulation::SpeedModulator,
};
@@ -13,6 +14,7 @@ pub struct Pacman<'a> {
pub position: (i32, i32),
pub direction: Direction,
pub next_direction: Option<Direction>,
pub stopped: bool,
speed: u32,
modulation: SpeedModulator,
sprite: AnimatedTexture<'a>,
@@ -25,6 +27,7 @@ impl Pacman<'_> {
direction: Direction::Right,
next_direction: None,
speed: 2,
stopped: false,
modulation: SpeedModulator::new(0.9333),
sprite: AnimatedTexture::new(atlas, 4, 3, 32, 32, Some((-4, -4))),
}
@@ -33,6 +36,12 @@ impl Pacman<'_> {
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
self.sprite.render(canvas, self.position, self.direction);
}
fn next_cell(&self) -> (i32, i32) {
let (x, y) = self.direction.offset();
let cell = self.cell_position();
(cell.0 as i32 + x, cell.1 as i32 + y)
}
}
impl Entity for Pacman<'_> {
@@ -65,7 +74,7 @@ impl Entity for Pacman<'_> {
}
}
if self.modulation.next() {
if !self.stopped && self.modulation.next() {
let speed = self.speed as i32;
match self.direction {
Direction::Right => {
@@ -82,5 +91,10 @@ impl Entity for Pacman<'_> {
}
}
}
let next = self.next_cell();
if BOARD[next.1 as usize][next.0 as usize] == MapTile::Wall {
self.stopped = true;
}
}
}