mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-16 02:12:28 -06:00
refactor: fix unnecessary qualified imports
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
use crate::animation::{AtlasTexture, FrameDrawn};
|
use crate::animation::{AtlasTexture, FrameDrawn};
|
||||||
use crate::constants::{FruitType, MapTile, BOARD_HEIGHT, BOARD_WIDTH};
|
use crate::constants::{FruitType, MapTile, BOARD_HEIGHT, BOARD_WIDTH};
|
||||||
use crate::direction::Direction;
|
use crate::direction::Direction;
|
||||||
use crate::entity::{Entity, Renderable};
|
use crate::entity::{Entity, Renderable, StaticEntity};
|
||||||
use crate::map::Map;
|
use crate::map::Map;
|
||||||
use sdl2::{render::Canvas, video::Window};
|
use sdl2::{render::Canvas, video::Window};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@@ -16,33 +16,29 @@ pub enum EdibleKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Edible<'a> {
|
pub struct Edible<'a> {
|
||||||
pub base: crate::entity::StaticEntity,
|
pub base: StaticEntity,
|
||||||
pub kind: EdibleKind,
|
pub kind: EdibleKind,
|
||||||
pub sprite: std::rc::Rc<AtlasTexture<'a>>,
|
pub sprite: Rc<AtlasTexture<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Edible<'a> {
|
impl<'a> Edible<'a> {
|
||||||
pub fn new(
|
pub fn new(kind: EdibleKind, cell_position: (u32, u32), sprite: Rc<AtlasTexture<'a>>) -> Self {
|
||||||
kind: EdibleKind,
|
let pixel_position = Map::cell_to_pixel(cell_position);
|
||||||
cell_position: (u32, u32),
|
|
||||||
sprite: std::rc::Rc<AtlasTexture<'a>>,
|
|
||||||
) -> Self {
|
|
||||||
let pixel_position = crate::map::Map::cell_to_pixel(cell_position);
|
|
||||||
Edible {
|
Edible {
|
||||||
base: crate::entity::StaticEntity::new(pixel_position, cell_position),
|
base: StaticEntity::new(pixel_position, cell_position),
|
||||||
kind,
|
kind,
|
||||||
sprite,
|
sprite,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks collision with Pac-Man (or any entity)
|
/// Checks collision with Pac-Man (or any entity)
|
||||||
pub fn collide(&self, pacman: &dyn crate::entity::Entity) -> bool {
|
pub fn collide(&self, pacman: &dyn Entity) -> bool {
|
||||||
self.base.is_colliding(pacman)
|
self.base.is_colliding(pacman)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Entity for Edible<'a> {
|
impl<'a> Entity for Edible<'a> {
|
||||||
fn base(&self) -> &crate::entity::StaticEntity {
|
fn base(&self) -> &StaticEntity {
|
||||||
&self.base
|
&self.base
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,9 +53,9 @@ impl<'a> Renderable for Edible<'a> {
|
|||||||
/// Reconstruct all edibles from the original map layout
|
/// Reconstruct all edibles from the original map layout
|
||||||
pub fn reconstruct_edibles<'a>(
|
pub fn reconstruct_edibles<'a>(
|
||||||
map: Rc<RefCell<Map>>,
|
map: Rc<RefCell<Map>>,
|
||||||
pellet_sprite: std::rc::Rc<AtlasTexture<'a>>,
|
pellet_sprite: Rc<AtlasTexture<'a>>,
|
||||||
power_pellet_sprite: std::rc::Rc<AtlasTexture<'a>>,
|
power_pellet_sprite: Rc<AtlasTexture<'a>>,
|
||||||
fruit_sprite: std::rc::Rc<AtlasTexture<'a>>,
|
fruit_sprite: Rc<AtlasTexture<'a>>,
|
||||||
) -> Vec<Edible<'a>> {
|
) -> Vec<Edible<'a>> {
|
||||||
let mut edibles = Vec::new();
|
let mut edibles = Vec::new();
|
||||||
for x in 0..BOARD_WIDTH {
|
for x in 0..BOARD_WIDTH {
|
||||||
|
|||||||
25
src/game.rs
25
src/game.rs
@@ -13,16 +13,13 @@ use sdl2::{pixels::Color, render::Canvas, video::Window};
|
|||||||
use tracing::event;
|
use tracing::event;
|
||||||
|
|
||||||
use crate::audio::Audio;
|
use crate::audio::Audio;
|
||||||
use crate::{
|
use crate::animation::{AtlasTexture, FrameDrawn};
|
||||||
animation::{AtlasTexture, FrameDrawn},
|
use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
|
||||||
constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD},
|
use crate::direction::Direction;
|
||||||
direction::Direction,
|
use crate::entity::{Entity, Renderable};
|
||||||
entity::{Entity, Renderable},
|
use crate::ghosts::blinky::Blinky;
|
||||||
ghosts::blinky::Blinky,
|
use crate::map::Map;
|
||||||
map::Map,
|
use crate::pacman::Pacman;
|
||||||
pacman::Pacman,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::debug::{DebugMode, DebugRenderer};
|
use crate::debug::{DebugMode, DebugRenderer};
|
||||||
use crate::edible::{reconstruct_edibles, Edible, EdibleKind};
|
use crate::edible::{reconstruct_edibles, Edible, EdibleKind};
|
||||||
|
|
||||||
@@ -48,10 +45,10 @@ pub struct Game<'a> {
|
|||||||
power_pellet_texture: Rc<AtlasTexture<'a>>,
|
power_pellet_texture: Rc<AtlasTexture<'a>>,
|
||||||
font: Font<'a, 'static>,
|
font: Font<'a, 'static>,
|
||||||
pacman: Rc<RefCell<Pacman<'a>>>,
|
pacman: Rc<RefCell<Pacman<'a>>>,
|
||||||
map: Rc<std::cell::RefCell<Map>>,
|
map: Rc<RefCell<Map>>,
|
||||||
debug_mode: DebugMode,
|
debug_mode: DebugMode,
|
||||||
score: u32,
|
score: u32,
|
||||||
audio: crate::audio::Audio,
|
audio: Audio,
|
||||||
blinky: Blinky<'a>,
|
blinky: Blinky<'a>,
|
||||||
edibles: Vec<Edible<'a>>,
|
edibles: Vec<Edible<'a>>,
|
||||||
}
|
}
|
||||||
@@ -71,13 +68,13 @@ impl Game<'_> {
|
|||||||
ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
|
ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
|
||||||
_audio_subsystem: &'a sdl2::AudioSubsystem,
|
_audio_subsystem: &'a sdl2::AudioSubsystem,
|
||||||
) -> Game<'a> {
|
) -> Game<'a> {
|
||||||
let map = Rc::new(std::cell::RefCell::new(Map::new(RAW_BOARD)));
|
let map = Rc::new(RefCell::new(Map::new(RAW_BOARD)));
|
||||||
|
|
||||||
// Load Pacman texture from embedded data
|
// Load Pacman texture from embedded data
|
||||||
let pacman_atlas = texture_creator
|
let pacman_atlas = texture_creator
|
||||||
.load_texture_bytes(PACMAN_TEXTURE_DATA)
|
.load_texture_bytes(PACMAN_TEXTURE_DATA)
|
||||||
.expect("Could not load pacman texture from embedded data");
|
.expect("Could not load pacman texture from embedded data");
|
||||||
let pacman = Rc::new(std::cell::RefCell::new(Pacman::new(
|
let pacman = Rc::new(RefCell::new(Pacman::new(
|
||||||
(1, 1),
|
(1, 1),
|
||||||
pacman_atlas,
|
pacman_atlas,
|
||||||
Rc::clone(&map),
|
Rc::clone(&map),
|
||||||
|
|||||||
40
src/ghost.rs
40
src/ghost.rs
@@ -1,15 +1,17 @@
|
|||||||
use pathfinding::prelude::dijkstra;
|
use pathfinding::prelude::dijkstra;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::{
|
use crate::animation::{AnimatedAtlasTexture, FrameDrawn};
|
||||||
animation::{AnimatedAtlasTexture, FrameDrawn},
|
use crate::constants::{MapTile, BOARD_WIDTH};
|
||||||
constants::{MapTile, BOARD_WIDTH},
|
use crate::direction::Direction;
|
||||||
direction::Direction,
|
use crate::entity::{Entity, MovableEntity, Moving, Renderable, StaticEntity};
|
||||||
entity::{Entity, MovableEntity, Moving, Renderable, StaticEntity},
|
use crate::map::Map;
|
||||||
map::Map,
|
use crate::modulation::{SimpleTickModulator, TickModulator};
|
||||||
modulation::{SimpleTickModulator, TickModulator},
|
use crate::pacman::Pacman;
|
||||||
pacman::Pacman,
|
use sdl2::pixels::Color;
|
||||||
};
|
use sdl2::render::Texture;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
/// The different modes a ghost can be in
|
/// The different modes a ghost can be in
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
@@ -37,12 +39,12 @@ pub enum GhostType {
|
|||||||
|
|
||||||
impl GhostType {
|
impl GhostType {
|
||||||
/// Returns the color of the ghost.
|
/// Returns the color of the ghost.
|
||||||
pub fn color(&self) -> sdl2::pixels::Color {
|
pub fn color(&self) -> Color {
|
||||||
match self {
|
match self {
|
||||||
GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0),
|
GhostType::Blinky => Color::RGB(255, 0, 0),
|
||||||
GhostType::Pinky => sdl2::pixels::Color::RGB(255, 184, 255),
|
GhostType::Pinky => Color::RGB(255, 184, 255),
|
||||||
GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255),
|
GhostType::Inky => Color::RGB(0, 255, 255),
|
||||||
GhostType::Clyde => sdl2::pixels::Color::RGB(255, 184, 82),
|
GhostType::Clyde => Color::RGB(255, 184, 82),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,7 +58,7 @@ pub struct Ghost<'a> {
|
|||||||
/// The type/personality of this ghost
|
/// The type/personality of this ghost
|
||||||
pub ghost_type: GhostType,
|
pub ghost_type: GhostType,
|
||||||
/// Reference to Pac-Man for targeting
|
/// Reference to Pac-Man for targeting
|
||||||
pub pacman: std::rc::Rc<std::cell::RefCell<Pacman<'a>>>,
|
pub pacman: Rc<RefCell<Pacman<'a>>>,
|
||||||
pub body_sprite: AnimatedAtlasTexture<'a>,
|
pub body_sprite: AnimatedAtlasTexture<'a>,
|
||||||
pub eyes_sprite: AnimatedAtlasTexture<'a>,
|
pub eyes_sprite: AnimatedAtlasTexture<'a>,
|
||||||
}
|
}
|
||||||
@@ -66,10 +68,10 @@ impl Ghost<'_> {
|
|||||||
pub fn new<'a>(
|
pub fn new<'a>(
|
||||||
ghost_type: GhostType,
|
ghost_type: GhostType,
|
||||||
starting_position: (u32, u32),
|
starting_position: (u32, u32),
|
||||||
body_texture: sdl2::render::Texture<'a>,
|
body_texture: Texture<'a>,
|
||||||
eyes_texture: sdl2::render::Texture<'a>,
|
eyes_texture: Texture<'a>,
|
||||||
map: std::rc::Rc<std::cell::RefCell<Map>>,
|
map: Rc<RefCell<Map>>,
|
||||||
pacman: std::rc::Rc<std::cell::RefCell<Pacman<'a>>>,
|
pacman: Rc<RefCell<Pacman<'a>>>,
|
||||||
) -> Ghost<'a> {
|
) -> Ghost<'a> {
|
||||||
let color = ghost_type.color();
|
let color = ghost_type.color();
|
||||||
let mut body_sprite = AnimatedAtlasTexture::new(body_texture, 8, 2, 32, 32, Some((-4, -4)));
|
let mut body_sprite = AnimatedAtlasTexture::new(body_texture, 8, 2, 32, 32, Some((-4, -4)));
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ use std::rc::Rc;
|
|||||||
use sdl2::render::{Canvas, Texture};
|
use sdl2::render::{Canvas, Texture};
|
||||||
use sdl2::video::Window;
|
use sdl2::video::Window;
|
||||||
|
|
||||||
use crate::{
|
use crate::direction::Direction;
|
||||||
entity::{Entity, MovableEntity, Renderable},
|
use crate::entity::{Entity, MovableEntity, Moving, Renderable, StaticEntity};
|
||||||
ghost::{Ghost, GhostMode, GhostType},
|
use crate::ghost::{Ghost, GhostMode, GhostType};
|
||||||
map::Map,
|
use crate::map::Map;
|
||||||
pacman::Pacman,
|
use crate::pacman::Pacman;
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Blinky<'a> {
|
pub struct Blinky<'a> {
|
||||||
ghost: Ghost<'a>,
|
ghost: Ghost<'a>,
|
||||||
@@ -51,29 +50,29 @@ impl<'a> Blinky<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> crate::entity::Entity for Blinky<'a> {
|
impl<'a> Entity for Blinky<'a> {
|
||||||
fn base(&self) -> &crate::entity::StaticEntity {
|
fn base(&self) -> &StaticEntity {
|
||||||
self.ghost.base.base()
|
self.ghost.base.base()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> crate::entity::Renderable for Blinky<'a> {
|
impl<'a> Renderable for Blinky<'a> {
|
||||||
fn render(&self, canvas: &mut Canvas<Window>) {
|
fn render(&self, canvas: &mut Canvas<Window>) {
|
||||||
self.ghost.render(canvas);
|
self.ghost.render(canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> crate::entity::Moving for Blinky<'a> {
|
impl<'a> Moving for Blinky<'a> {
|
||||||
fn move_forward(&mut self) {
|
fn move_forward(&mut self) {
|
||||||
self.ghost.move_forward();
|
self.ghost.move_forward();
|
||||||
}
|
}
|
||||||
fn update_cell_position(&mut self) {
|
fn update_cell_position(&mut self) {
|
||||||
self.ghost.update_cell_position();
|
self.ghost.update_cell_position();
|
||||||
}
|
}
|
||||||
fn next_cell(&self, direction: Option<crate::direction::Direction>) -> (i32, i32) {
|
fn next_cell(&self, direction: Option<Direction>) -> (i32, i32) {
|
||||||
self.ghost.next_cell(direction)
|
self.ghost.next_cell(direction)
|
||||||
}
|
}
|
||||||
fn is_wall_ahead(&self, direction: Option<crate::direction::Direction>) -> bool {
|
fn is_wall_ahead(&self, direction: Option<Direction>) -> bool {
|
||||||
self.ghost.is_wall_ahead(direction)
|
self.ghost.is_wall_ahead(direction)
|
||||||
}
|
}
|
||||||
fn handle_tunnel(&mut self) -> bool {
|
fn handle_tunnel(&mut self) -> bool {
|
||||||
@@ -82,7 +81,7 @@ impl<'a> crate::entity::Moving for Blinky<'a> {
|
|||||||
fn is_grid_aligned(&self) -> bool {
|
fn is_grid_aligned(&self) -> bool {
|
||||||
self.ghost.is_grid_aligned()
|
self.ghost.is_grid_aligned()
|
||||||
}
|
}
|
||||||
fn set_direction_if_valid(&mut self, new_direction: crate::direction::Direction) -> bool {
|
fn set_direction_if_valid(&mut self, new_direction: Direction) -> bool {
|
||||||
self.ghost.set_direction_if_valid(new_direction)
|
self.ghost.set_direction_if_valid(new_direction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user