mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-11 04:07:55 -06:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b623ffabe | ||
|
|
af81390e30 |
222
src/game.rs
222
src/game.rs
@@ -5,7 +5,7 @@ include!(concat!(env!("OUT_DIR"), "/atlas_data.rs"));
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::constants::{self, animation, MapTile, CANVAS_SIZE};
|
use crate::constants::{self, animation, MapTile, CANVAS_SIZE};
|
||||||
use crate::error::{GameError, GameResult, TextureError};
|
use crate::error::{GameError, GameResult};
|
||||||
use crate::events::GameEvent;
|
use crate::events::GameEvent;
|
||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::map::direction::Direction;
|
use crate::map::direction::Direction;
|
||||||
@@ -28,13 +28,13 @@ use crate::systems::{
|
|||||||
};
|
};
|
||||||
use crate::texture::animated::{DirectionalTiles, TileSequence};
|
use crate::texture::animated::{DirectionalTiles, TileSequence};
|
||||||
use crate::texture::sprite::AtlasTile;
|
use crate::texture::sprite::AtlasTile;
|
||||||
|
use crate::texture::sprites::{FrightenedColor, GameSprite, GhostSprite, MazeSprite, PacmanSprite};
|
||||||
use bevy_ecs::event::EventRegistry;
|
use bevy_ecs::event::EventRegistry;
|
||||||
use bevy_ecs::observer::Trigger;
|
use bevy_ecs::observer::Trigger;
|
||||||
use bevy_ecs::schedule::common_conditions::resource_changed;
|
use bevy_ecs::schedule::common_conditions::resource_changed;
|
||||||
use bevy_ecs::schedule::{Condition, IntoScheduleConfigs, Schedule, SystemSet};
|
use bevy_ecs::schedule::{Condition, IntoScheduleConfigs, Schedule, SystemSet};
|
||||||
use bevy_ecs::system::{Local, ResMut};
|
use bevy_ecs::system::{Local, ResMut};
|
||||||
use bevy_ecs::world::World;
|
use bevy_ecs::world::World;
|
||||||
use glam::UVec2;
|
|
||||||
use sdl2::event::EventType;
|
use sdl2::event::EventType;
|
||||||
use sdl2::image::LoadTexture;
|
use sdl2::image::LoadTexture;
|
||||||
use sdl2::render::{BlendMode, Canvas, ScaleMode, TextureCreator};
|
use sdl2::render::{BlendMode, Canvas, ScaleMode, TextureCreator};
|
||||||
@@ -200,8 +200,8 @@ impl Game {
|
|||||||
// Create map tiles
|
// Create map tiles
|
||||||
let mut map_tiles = Vec::with_capacity(35);
|
let mut map_tiles = Vec::with_capacity(35);
|
||||||
for i in 0..35 {
|
for i in 0..35 {
|
||||||
let tile_name = format!("maze/tiles/{}.png", i);
|
let tile_name = GameSprite::Maze(MazeSprite::Tile(i)).to_path();
|
||||||
let tile = atlas.get_tile(&tile_name).unwrap();
|
let tile = atlas.get_tile(&tile_name)?;
|
||||||
map_tiles.push(tile);
|
map_tiles.push(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,36 +216,42 @@ impl Game {
|
|||||||
|
|
||||||
// Create directional animated textures for Pac-Man
|
// Create directional animated textures for Pac-Man
|
||||||
let up_moving_tiles = [
|
let up_moving_tiles = [
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/up_a.png")
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Moving(Direction::Up, 0)).to_path())?,
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/up_a.png".to_string())))?,
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Moving(Direction::Up, 1)).to_path())?,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/up_b.png")
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Full).to_path())?,
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/up_b.png".to_string())))?,
|
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
|
||||||
];
|
];
|
||||||
let down_moving_tiles = [
|
let down_moving_tiles = [
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/down_a.png")
|
SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/down_a.png".to_string())))?,
|
&atlas,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/down_b.png")
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Down, 0)).to_path(),
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/down_b.png".to_string())))?,
|
)?,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
&atlas,
|
||||||
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Down, 1)).to_path(),
|
||||||
|
)?,
|
||||||
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Full).to_path())?,
|
||||||
];
|
];
|
||||||
let left_moving_tiles = [
|
let left_moving_tiles = [
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/left_a.png")
|
SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/left_a.png".to_string())))?,
|
&atlas,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/left_b.png")
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Left, 0)).to_path(),
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/left_b.png".to_string())))?,
|
)?,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
&atlas,
|
||||||
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Left, 1)).to_path(),
|
||||||
|
)?,
|
||||||
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Full).to_path())?,
|
||||||
];
|
];
|
||||||
let right_moving_tiles = [
|
let right_moving_tiles = [
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/right_a.png")
|
SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/right_a.png".to_string())))?,
|
&atlas,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/right_b.png")
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Right, 0)).to_path(),
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/right_b.png".to_string())))?,
|
)?,
|
||||||
SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
&atlas,
|
||||||
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Right, 1)).to_path(),
|
||||||
|
)?,
|
||||||
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Full).to_path())?,
|
||||||
];
|
];
|
||||||
|
|
||||||
let moving_tiles = DirectionalTiles::new(
|
let moving_tiles = DirectionalTiles::new(
|
||||||
@@ -255,14 +261,20 @@ impl Game {
|
|||||||
TileSequence::new(&right_moving_tiles),
|
TileSequence::new(&right_moving_tiles),
|
||||||
);
|
);
|
||||||
|
|
||||||
let up_stopped_tile = SpriteAtlas::get_tile(&atlas, "pacman/up_b.png")
|
let up_stopped_tile =
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/up_b.png".to_string())))?;
|
SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Moving(Direction::Up, 1)).to_path())?;
|
||||||
let down_stopped_tile = SpriteAtlas::get_tile(&atlas, "pacman/down_b.png")
|
let down_stopped_tile = SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/down_b.png".to_string())))?;
|
&atlas,
|
||||||
let left_stopped_tile = SpriteAtlas::get_tile(&atlas, "pacman/left_b.png")
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Down, 1)).to_path(),
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/left_b.png".to_string())))?;
|
)?;
|
||||||
let right_stopped_tile = SpriteAtlas::get_tile(&atlas, "pacman/right_b.png")
|
let left_stopped_tile = SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/right_b.png".to_string())))?;
|
&atlas,
|
||||||
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Left, 1)).to_path(),
|
||||||
|
)?;
|
||||||
|
let right_stopped_tile = SpriteAtlas::get_tile(
|
||||||
|
&atlas,
|
||||||
|
&GameSprite::Pacman(PacmanSprite::Moving(Direction::Right, 1)).to_path(),
|
||||||
|
)?;
|
||||||
|
|
||||||
let stopped_tiles = DirectionalTiles::new(
|
let stopped_tiles = DirectionalTiles::new(
|
||||||
TileSequence::new(&[up_stopped_tile]),
|
TileSequence::new(&[up_stopped_tile]),
|
||||||
@@ -283,8 +295,7 @@ impl Game {
|
|||||||
movement_modifiers: MovementModifiers::default(),
|
movement_modifiers: MovementModifiers::default(),
|
||||||
buffered_direction: BufferedDirection::None,
|
buffered_direction: BufferedDirection::None,
|
||||||
sprite: Renderable {
|
sprite: Renderable {
|
||||||
sprite: SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
sprite: SpriteAtlas::get_tile(&atlas, &GameSprite::Pacman(PacmanSprite::Full).to_path())?,
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
|
||||||
layer: 0,
|
layer: 0,
|
||||||
},
|
},
|
||||||
directional_animation: DirectionalAnimation::new(moving_tiles, stopped_tiles, 5),
|
directional_animation: DirectionalAnimation::new(moving_tiles, stopped_tiles, 5),
|
||||||
@@ -302,10 +313,7 @@ impl Game {
|
|||||||
EventRegistry::register_event::<GameEvent>(&mut world);
|
EventRegistry::register_event::<GameEvent>(&mut world);
|
||||||
EventRegistry::register_event::<AudioEvent>(&mut world);
|
EventRegistry::register_event::<AudioEvent>(&mut world);
|
||||||
|
|
||||||
let scale =
|
world.insert_resource(BatchedLinesResource::new(&map, constants::LARGE_SCALE));
|
||||||
(UVec2::from(canvas.output_size().unwrap()).as_vec2() / UVec2::from(canvas.logical_size()).as_vec2()).min_element();
|
|
||||||
|
|
||||||
world.insert_resource(BatchedLinesResource::new(&map, scale));
|
|
||||||
world.insert_resource(Self::create_ghost_animations(&atlas)?);
|
world.insert_resource(Self::create_ghost_animations(&atlas)?);
|
||||||
world.insert_resource(map);
|
world.insert_resource(map);
|
||||||
world.insert_resource(GlobalState { exit: false });
|
world.insert_resource(GlobalState { exit: false });
|
||||||
@@ -401,10 +409,14 @@ impl Game {
|
|||||||
// Spawn ghosts
|
// Spawn ghosts
|
||||||
Self::spawn_ghosts(&mut world)?;
|
Self::spawn_ghosts(&mut world)?;
|
||||||
|
|
||||||
let pellet_sprite = SpriteAtlas::get_tile(world.non_send_resource::<SpriteAtlas>(), "maze/pellet.png")
|
let pellet_sprite = SpriteAtlas::get_tile(
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("maze/pellet.png".to_string())))?;
|
world.non_send_resource::<SpriteAtlas>(),
|
||||||
let energizer_sprite = SpriteAtlas::get_tile(world.non_send_resource::<SpriteAtlas>(), "maze/energizer.png")
|
&GameSprite::Maze(MazeSprite::Pellet).to_path(),
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("maze/energizer.png".to_string())))?;
|
)?;
|
||||||
|
let energizer_sprite = SpriteAtlas::get_tile(
|
||||||
|
world.non_send_resource::<SpriteAtlas>(),
|
||||||
|
&GameSprite::Maze(MazeSprite::Energizer).to_path(),
|
||||||
|
)?;
|
||||||
|
|
||||||
// Build a list of item entities to spawn from the map
|
// Build a list of item entities to spawn from the map
|
||||||
let nodes: Vec<(NodeId, EntityType, AtlasTile, f32)> = world
|
let nodes: Vec<(NodeId, EntityType, AtlasTile, f32)> = world
|
||||||
@@ -464,6 +476,7 @@ impl Game {
|
|||||||
let ghost = {
|
let ghost = {
|
||||||
let animations = *world.resource::<GhostAnimations>().get_normal(&ghost_type).unwrap();
|
let animations = *world.resource::<GhostAnimations>().get_normal(&ghost_type).unwrap();
|
||||||
let atlas = world.non_send_resource::<SpriteAtlas>();
|
let atlas = world.non_send_resource::<SpriteAtlas>();
|
||||||
|
let sprite_path = GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Left, 0)).to_path();
|
||||||
|
|
||||||
GhostBundle {
|
GhostBundle {
|
||||||
ghost: ghost_type,
|
ghost: ghost_type,
|
||||||
@@ -473,14 +486,7 @@ impl Game {
|
|||||||
direction: Direction::Left,
|
direction: Direction::Left,
|
||||||
},
|
},
|
||||||
sprite: Renderable {
|
sprite: Renderable {
|
||||||
sprite: SpriteAtlas::get_tile(atlas, &format!("ghost/{}/left_a.png", ghost_type.as_str())).ok_or_else(
|
sprite: SpriteAtlas::get_tile(atlas, &sprite_path)?,
|
||||||
|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/left_a.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
},
|
|
||||||
)?,
|
|
||||||
layer: 0,
|
layer: 0,
|
||||||
},
|
},
|
||||||
directional_animation: animations,
|
directional_animation: animations,
|
||||||
@@ -502,18 +508,10 @@ impl Game {
|
|||||||
|
|
||||||
fn create_ghost_animations(atlas: &SpriteAtlas) -> GameResult<GhostAnimations> {
|
fn create_ghost_animations(atlas: &SpriteAtlas) -> GameResult<GhostAnimations> {
|
||||||
// Eaten (eyes) animations - single tile per direction
|
// Eaten (eyes) animations - single tile per direction
|
||||||
let up_eye = atlas
|
let up_eye = atlas.get_tile(&GameSprite::Ghost(GhostSprite::Eyes(Direction::Up)).to_path())?;
|
||||||
.get_tile("ghost/eyes/up.png")
|
let down_eye = atlas.get_tile(&GameSprite::Ghost(GhostSprite::Eyes(Direction::Down)).to_path())?;
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/eyes/up.png".to_string())))?;
|
let left_eye = atlas.get_tile(&GameSprite::Ghost(GhostSprite::Eyes(Direction::Left)).to_path())?;
|
||||||
let down_eye = atlas
|
let right_eye = atlas.get_tile(&GameSprite::Ghost(GhostSprite::Eyes(Direction::Right)).to_path())?;
|
||||||
.get_tile("ghost/eyes/down.png")
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/eyes/down.png".to_string())))?;
|
|
||||||
let left_eye = atlas
|
|
||||||
.get_tile("ghost/eyes/left.png")
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/eyes/left.png".to_string())))?;
|
|
||||||
let right_eye = atlas
|
|
||||||
.get_tile("ghost/eyes/right.png")
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/eyes/right.png".to_string())))?;
|
|
||||||
|
|
||||||
let eyes_tiles = DirectionalTiles::new(
|
let eyes_tiles = DirectionalTiles::new(
|
||||||
TileSequence::new(&[up_eye]),
|
TileSequence::new(&[up_eye]),
|
||||||
@@ -528,76 +526,20 @@ impl Game {
|
|||||||
for ghost_type in [Ghost::Blinky, Ghost::Pinky, Ghost::Inky, Ghost::Clyde] {
|
for ghost_type in [Ghost::Blinky, Ghost::Pinky, Ghost::Inky, Ghost::Clyde] {
|
||||||
// Normal animations - create directional tiles for each direction
|
// Normal animations - create directional tiles for each direction
|
||||||
let up_tiles = [
|
let up_tiles = [
|
||||||
atlas
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Up, 0)).to_path())?,
|
||||||
.get_tile(&format!("ghost/{}/up_a.png", ghost_type.as_str()))
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Up, 1)).to_path())?,
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/up_a.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
atlas
|
|
||||||
.get_tile(&format!("ghost/{}/up_b.png", ghost_type.as_str()))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/up_b.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
];
|
];
|
||||||
let down_tiles = [
|
let down_tiles = [
|
||||||
atlas
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Down, 0)).to_path())?,
|
||||||
.get_tile(&format!("ghost/{}/down_a.png", ghost_type.as_str()))
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Down, 1)).to_path())?,
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/down_a.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
atlas
|
|
||||||
.get_tile(&format!("ghost/{}/down_b.png", ghost_type.as_str()))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/down_b.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
];
|
];
|
||||||
let left_tiles = [
|
let left_tiles = [
|
||||||
atlas
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Left, 0)).to_path())?,
|
||||||
.get_tile(&format!("ghost/{}/left_a.png", ghost_type.as_str()))
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Left, 1)).to_path())?,
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/left_a.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
atlas
|
|
||||||
.get_tile(&format!("ghost/{}/left_b.png", ghost_type.as_str()))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/left_b.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
];
|
];
|
||||||
let right_tiles = [
|
let right_tiles = [
|
||||||
atlas
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Right, 0)).to_path())?,
|
||||||
.get_tile(&format!("ghost/{}/right_a.png", ghost_type.as_str()))
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Right, 1)).to_path())?,
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/right_a.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
atlas
|
|
||||||
.get_tile(&format!("ghost/{}/right_b.png", ghost_type.as_str()))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/right_b.png",
|
|
||||||
ghost_type.as_str()
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let normal_moving = DirectionalTiles::new(
|
let normal_moving = DirectionalTiles::new(
|
||||||
@@ -613,18 +555,14 @@ impl Game {
|
|||||||
|
|
||||||
let (frightened, frightened_flashing) = {
|
let (frightened, frightened_flashing) = {
|
||||||
// Load frightened animation tiles (same for all ghosts)
|
// Load frightened animation tiles (same for all ghosts)
|
||||||
let frightened_blue_a = atlas
|
let frightened_blue_a =
|
||||||
.get_tile("ghost/frightened/blue_a.png")
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Frightened(FrightenedColor::Blue, 0)).to_path())?;
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/frightened/blue_a.png".to_string())))?;
|
let frightened_blue_b =
|
||||||
let frightened_blue_b = atlas
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Frightened(FrightenedColor::Blue, 1)).to_path())?;
|
||||||
.get_tile("ghost/frightened/blue_b.png")
|
let frightened_white_a =
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/frightened/blue_b.png".to_string())))?;
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Frightened(FrightenedColor::White, 0)).to_path())?;
|
||||||
let frightened_white_a = atlas
|
let frightened_white_b =
|
||||||
.get_tile("ghost/frightened/white_a.png")
|
atlas.get_tile(&GameSprite::Ghost(GhostSprite::Frightened(FrightenedColor::White, 1)).to_path())?;
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/frightened/white_a.png".to_string())))?;
|
|
||||||
let frightened_white_b = atlas
|
|
||||||
.get_tile("ghost/frightened/white_b.png")
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("ghost/frightened/white_b.png".to_string())))?;
|
|
||||||
|
|
||||||
(
|
(
|
||||||
LinearAnimation::new(
|
LinearAnimation::new(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
pub mod animated;
|
pub mod animated;
|
||||||
pub mod blinking;
|
pub mod blinking;
|
||||||
pub mod sprite;
|
pub mod sprite;
|
||||||
|
pub mod sprites;
|
||||||
pub mod text;
|
pub mod text;
|
||||||
pub mod ttf;
|
pub mod ttf;
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ pub struct MapperFrame {
|
|||||||
pub size: U16Vec2,
|
pub size: U16Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
/// A single tile within a sprite atlas, defined by its position and size.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||||
pub struct AtlasTile {
|
pub struct AtlasTile {
|
||||||
pub pos: U16Vec2,
|
pub pos: U16Vec2,
|
||||||
pub size: U16Vec2,
|
pub size: U16Vec2,
|
||||||
@@ -89,9 +90,11 @@ pub struct SpriteAtlas {
|
|||||||
|
|
||||||
impl SpriteAtlas {
|
impl SpriteAtlas {
|
||||||
pub fn new(texture: Texture, mapper: AtlasMapper) -> Self {
|
pub fn new(texture: Texture, mapper: AtlasMapper) -> Self {
|
||||||
|
let tiles = mapper.frames.into_iter().collect();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
texture,
|
texture,
|
||||||
tiles: mapper.frames,
|
tiles,
|
||||||
default_color: None,
|
default_color: None,
|
||||||
last_modulation: None,
|
last_modulation: None,
|
||||||
}
|
}
|
||||||
@@ -103,11 +106,15 @@ impl SpriteAtlas {
|
|||||||
/// for the named sprite, or `None` if the sprite name is not found in the
|
/// for the named sprite, or `None` if the sprite name is not found in the
|
||||||
/// atlas. The returned tile can be used for immediate rendering or stored
|
/// atlas. The returned tile can be used for immediate rendering or stored
|
||||||
/// for repeated use in animations and entity sprites.
|
/// for repeated use in animations and entity sprites.
|
||||||
pub fn get_tile(&self, name: &str) -> Option<AtlasTile> {
|
pub fn get_tile(&self, name: &str) -> Result<AtlasTile, TextureError> {
|
||||||
self.tiles.get(name).map(|frame| AtlasTile {
|
let frame = self
|
||||||
|
.tiles
|
||||||
|
.get(name)
|
||||||
|
.ok_or_else(|| TextureError::AtlasTileNotFound(name.to_string()))?;
|
||||||
|
Ok(AtlasTile {
|
||||||
pos: frame.pos,
|
pos: frame.pos,
|
||||||
size: frame.size,
|
size: frame.size,
|
||||||
color: None,
|
color: self.default_color,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
104
src/texture/sprites.rs
Normal file
104
src/texture/sprites.rs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
//! A structured representation of all sprite assets in the game.
|
||||||
|
//!
|
||||||
|
//! This module provides a set of enums to represent every sprite, allowing for
|
||||||
|
//! type-safe access to asset paths and avoiding the use of raw strings.
|
||||||
|
//! The `GameSprite` enum is the main entry point, and its `to_path` method
|
||||||
|
//! generates the correct path for a given sprite in the texture atlas.
|
||||||
|
|
||||||
|
use crate::map::direction::Direction;
|
||||||
|
use crate::systems::components::Ghost;
|
||||||
|
|
||||||
|
/// Represents the different sprites for Pac-Man.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum PacmanSprite {
|
||||||
|
/// A moving Pac-Man sprite for a given direction and animation frame.
|
||||||
|
Moving(Direction, u8),
|
||||||
|
/// The full, closed-mouth Pac-Man sprite.
|
||||||
|
Full,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents the color of a frightened ghost.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum FrightenedColor {
|
||||||
|
Blue,
|
||||||
|
White,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents the different sprites for ghosts.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum GhostSprite {
|
||||||
|
/// The normal appearance of a ghost for a given type, direction, and animation frame.
|
||||||
|
Normal(Ghost, Direction, u8),
|
||||||
|
/// The frightened appearance of a ghost, with a specific color and animation frame.
|
||||||
|
Frightened(FrightenedColor, u8),
|
||||||
|
/// The "eyes only" appearance of a ghost after being eaten.
|
||||||
|
Eyes(Direction),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents the different sprites for the maze and collectibles.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum MazeSprite {
|
||||||
|
/// A specific tile of the maze.
|
||||||
|
Tile(u8),
|
||||||
|
/// A standard pellet.
|
||||||
|
Pellet,
|
||||||
|
/// An energizer/power pellet.
|
||||||
|
Energizer,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A top-level enum that encompasses all game sprites.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum GameSprite {
|
||||||
|
Pacman(PacmanSprite),
|
||||||
|
Ghost(GhostSprite),
|
||||||
|
Maze(MazeSprite),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameSprite {
|
||||||
|
/// Generates the asset path for the sprite.
|
||||||
|
///
|
||||||
|
/// This path corresponds to the filename in the texture atlas JSON file.
|
||||||
|
pub fn to_path(self) -> String {
|
||||||
|
match self {
|
||||||
|
GameSprite::Pacman(sprite) => match sprite {
|
||||||
|
PacmanSprite::Moving(dir, frame) => {
|
||||||
|
let frame_char = match frame {
|
||||||
|
0 => 'a',
|
||||||
|
1 => 'b',
|
||||||
|
_ => panic!("Invalid animation frame"),
|
||||||
|
};
|
||||||
|
format!("pacman/{}_{}.png", dir.as_ref().to_lowercase(), frame_char)
|
||||||
|
}
|
||||||
|
PacmanSprite::Full => "pacman/full.png".to_string(),
|
||||||
|
},
|
||||||
|
GameSprite::Ghost(sprite) => match sprite {
|
||||||
|
GhostSprite::Normal(ghost, dir, frame) => {
|
||||||
|
let frame_char = match frame {
|
||||||
|
0 => 'a',
|
||||||
|
1 => 'b',
|
||||||
|
_ => panic!("Invalid animation frame"),
|
||||||
|
};
|
||||||
|
format!("ghost/{}/{}_{}.png", ghost.as_str(), dir.as_ref().to_lowercase(), frame_char)
|
||||||
|
}
|
||||||
|
GhostSprite::Frightened(color, frame) => {
|
||||||
|
let frame_char = match frame {
|
||||||
|
0 => 'a',
|
||||||
|
1 => 'b',
|
||||||
|
_ => panic!("Invalid animation frame"),
|
||||||
|
};
|
||||||
|
let color_str = match color {
|
||||||
|
FrightenedColor::Blue => "blue",
|
||||||
|
FrightenedColor::White => "white",
|
||||||
|
};
|
||||||
|
format!("ghost/frightened/{}_{}.png", color_str, frame_char)
|
||||||
|
}
|
||||||
|
GhostSprite::Eyes(dir) => format!("ghost/eyes/{}.png", dir.as_ref().to_lowercase()),
|
||||||
|
},
|
||||||
|
GameSprite::Maze(sprite) => match sprite {
|
||||||
|
MazeSprite::Tile(index) => format!("maze/tiles/{}.png", index),
|
||||||
|
MazeSprite::Pellet => "maze/pellet.png".to_string(),
|
||||||
|
MazeSprite::Energizer => "maze/energizer.png".to_string(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -60,10 +60,7 @@ use sdl2::pixels::Color;
|
|||||||
use sdl2::render::{Canvas, RenderTarget};
|
use sdl2::render::{Canvas, RenderTarget};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
|
||||||
error::{GameError, TextureError},
|
|
||||||
texture::sprite::{AtlasTile, SpriteAtlas},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Converts a character to its tile name in the atlas.
|
/// Converts a character to its tile name in the atlas.
|
||||||
fn char_to_tile_name(c: char) -> Option<String> {
|
fn char_to_tile_name(c: char) -> Option<String> {
|
||||||
@@ -122,9 +119,7 @@ impl TextTexture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile_name) = char_to_tile_name(c) {
|
if let Some(tile_name) = char_to_tile_name(c) {
|
||||||
let tile = atlas
|
let tile = atlas.get_tile(&tile_name)?;
|
||||||
.get_tile(&tile_name)
|
|
||||||
.ok_or(GameError::Texture(TextureError::AtlasTileNotFound(tile_name)))?;
|
|
||||||
self.char_map.insert(c, tile);
|
self.char_map.insert(c, tile);
|
||||||
Ok(self.char_map.get(&c))
|
Ok(self.char_map.get(&c))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user