mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-10 14:07:59 -06:00
feat: board reset, store original map matrix
This commit is contained in:
22
src/game.rs
22
src/game.rs
@@ -73,12 +73,34 @@ impl Game<'_> {
|
|||||||
if keycode == Keycode::Space {
|
if keycode == Keycode::Space {
|
||||||
self.debug = !self.debug;
|
self.debug = !self.debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset game
|
||||||
|
if keycode == Keycode::R {
|
||||||
|
self.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_score(&mut self, points: u32) {
|
pub fn add_score(&mut self, points: u32) {
|
||||||
self.score += points;
|
self.score += points;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
// Reset the map to restore all pellets
|
||||||
|
{
|
||||||
|
let mut map = self.map.borrow_mut();
|
||||||
|
map.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the score
|
||||||
|
self.score = 0;
|
||||||
|
|
||||||
|
// Reset Pacman position (you might want to customize this)
|
||||||
|
// For now, we'll keep Pacman where he is, but you could add:
|
||||||
|
// self.pacman.position = Map::cell_to_pixel((1, 1));
|
||||||
|
|
||||||
|
event!(tracing::Level::INFO, "Game reset - map and score cleared");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn tick(&mut self) {
|
pub fn tick(&mut self) {
|
||||||
self.pacman.tick();
|
self.pacman.tick();
|
||||||
self.check_pellet_eating();
|
self.check_pellet_eating();
|
||||||
|
|||||||
27
src/map.rs
27
src/map.rs
@@ -1,13 +1,14 @@
|
|||||||
use crate::constants::MapTile;
|
use crate::constants::MapTile;
|
||||||
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH};
|
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
|
||||||
|
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
inner: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
|
current: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
|
||||||
|
default: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
pub fn new(raw_board: [&str; BOARD_HEIGHT as usize]) -> Map {
|
pub fn new(raw_board: [&str; BOARD_HEIGHT as usize]) -> Map {
|
||||||
let mut inner = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize];
|
let mut map = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize];
|
||||||
|
|
||||||
for y in 0..BOARD_HEIGHT as usize {
|
for y in 0..BOARD_HEIGHT as usize {
|
||||||
let line = raw_board[y];
|
let line = raw_board[y];
|
||||||
@@ -35,11 +36,23 @@ impl Map {
|
|||||||
_ => panic!("Unknown character in board: {}", character),
|
_ => panic!("Unknown character in board: {}", character),
|
||||||
};
|
};
|
||||||
|
|
||||||
inner[x as usize][y as usize] = tile;
|
map[x as usize][y as usize] = tile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map { inner: inner }
|
Map {
|
||||||
|
current: map,
|
||||||
|
default: map.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
// Restore the map to its original state
|
||||||
|
for x in 0..BOARD_WIDTH as usize {
|
||||||
|
for y in 0..BOARD_HEIGHT as usize {
|
||||||
|
self.current[x][y] = self.default[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_tile(&self, cell: (i32, i32)) -> Option<MapTile> {
|
pub fn get_tile(&self, cell: (i32, i32)) -> Option<MapTile> {
|
||||||
@@ -50,7 +63,7 @@ impl Map {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(self.inner[x][y])
|
Some(self.current[x][y])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tile(&mut self, cell: (i32, i32), tile: MapTile) -> bool {
|
pub fn set_tile(&mut self, cell: (i32, i32), tile: MapTile) -> bool {
|
||||||
@@ -61,7 +74,7 @@ impl Map {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.inner[x][y] = tile;
|
self.current[x][y] = tile;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user