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