mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 15:15:48 -06:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| da98b54216 |
@@ -15,4 +15,13 @@ impl Direction {
|
|||||||
Direction::Up => 270f64,
|
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
15
src/helper.rs
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ use sdl2::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
constants::{BOARD, MapTile},
|
||||||
animation::AnimatedTexture, constants::CELL_SIZE, direction::Direction, entity::Entity,
|
animation::AnimatedTexture, constants::CELL_SIZE, direction::Direction, entity::Entity,
|
||||||
modulation::SpeedModulator,
|
modulation::SpeedModulator,
|
||||||
};
|
};
|
||||||
@@ -13,6 +14,7 @@ pub struct Pacman<'a> {
|
|||||||
pub position: (i32, i32),
|
pub position: (i32, i32),
|
||||||
pub direction: Direction,
|
pub direction: Direction,
|
||||||
pub next_direction: Option<Direction>,
|
pub next_direction: Option<Direction>,
|
||||||
|
pub stopped: bool,
|
||||||
speed: u32,
|
speed: u32,
|
||||||
modulation: SpeedModulator,
|
modulation: SpeedModulator,
|
||||||
sprite: AnimatedTexture<'a>,
|
sprite: AnimatedTexture<'a>,
|
||||||
@@ -25,6 +27,7 @@ impl Pacman<'_> {
|
|||||||
direction: Direction::Right,
|
direction: Direction::Right,
|
||||||
next_direction: None,
|
next_direction: None,
|
||||||
speed: 2,
|
speed: 2,
|
||||||
|
stopped: false,
|
||||||
modulation: SpeedModulator::new(0.9333),
|
modulation: SpeedModulator::new(0.9333),
|
||||||
sprite: AnimatedTexture::new(atlas, 4, 3, 32, 32, Some((-4, -4))),
|
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>) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<'_> {
|
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;
|
let speed = self.speed as i32;
|
||||||
match self.direction {
|
match self.direction {
|
||||||
Direction::Right => {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user