Skip to content

Commit

Permalink
impr: move sprite sheets resource to ryot_sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrossi committed Apr 27, 2024
1 parent 93d8524 commit 212d141
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 113 deletions.
2 changes: 1 addition & 1 deletion crates/ryot_assets/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy_ecs::change_detection::ResMut;
use bevy_ecs::prelude::Res;
use bevy_reflect::TypePath;
use bevy_utils::tracing::debug;
use ryot_core::prelude::SpriteSheets;
use ryot_sprites::prelude::SpriteSheets;

pub trait CatalogAsset: crate::RyotAsset {
fn catalog_content(&self) -> &Handle<Catalog>;
Expand Down
40 changes: 0 additions & 40 deletions crates/ryot_core/src/content/sprite/sprite_sheet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::SpriteLayout;
use derive_more::{Deref, DerefMut};
use glam::UVec2;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -47,42 +46,3 @@ impl SpriteSheet {
UVec2::new(width, height)
}
}

/// This is a collection of sprite sheets.
/// It contains the sprite sheets and the sprite sheet config.
/// The sprite sheet config is used to calculate the position and size of a sprite in the sprite
/// sheet.
#[derive(Debug, Default, Clone, Deref, DerefMut)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Resource))]
pub struct SpriteSheets(Vec<SpriteSheet>);

impl<T> From<&[T]> for SpriteSheets
where
T: Into<Option<SpriteSheet>> + Clone,
{
fn from(content: &[T]) -> Self {
let sprite_sheets = content
.iter()
.filter_map(|content_type| content_type.clone().into())
.collect::<Vec<_>>();

Self(sprite_sheets)
}
}

impl<T> From<Vec<T>> for SpriteSheets
where
T: Into<Option<SpriteSheet>> + Clone,
{
fn from(content: Vec<T>) -> Self {
content.as_slice().into()
}
}

impl SpriteSheets {
/// Returns the sprite sheet that contains the given sprite id.
/// Returns None if the sprite id is not in any of the sprite sheets.
pub fn get_by_sprite_id(&self, sprite_id: u32) -> Option<&SpriteSheet> {
self.iter().find(|sheet| sheet.has_sprite(sprite_id))
}
}
8 changes: 4 additions & 4 deletions crates/ryot_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
pub mod content;
pub mod game;

#[cfg(test)]
mod tests;

pub mod prelude {
pub use crate::{
content::{
record::{Category, ContentRecord, Flags, VisualElement, VisualElements},
sprite::{
layout::{SpriteLayout, SpriteLayoutIter, TextureAtlasLayouts},
sprite_sheet::{SpriteSheet, SpriteSheets},
sprite_sheet::SpriteSheet,
Animation, FrameGroup, SpriteInfo,
},
ContentId, ContentType, RyotContentState,
Expand All @@ -27,3 +24,6 @@ pub mod prelude {
#[cfg(feature = "bevy")]
pub use crate::content::transition_to_ready;
}

#[cfg(test)]
mod tests;
67 changes: 0 additions & 67 deletions crates/ryot_core/src/tests/sprite_sheet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,73 +49,6 @@ fn sprite_sheet_fixture() -> SpriteSheet {
}
}

#[rstest]
fn test_from_content(#[from(sprite_sheet_set_fixture)] sprite_sheet_set: SpriteSheets) {
assert_eq!(2, sprite_sheet_set.len());
assert_eq!(100, sprite_sheet_set[0].first_sprite_id);
assert_eq!(200, sprite_sheet_set[0].last_sprite_id);
assert_eq!(300, sprite_sheet_set[1].first_sprite_id);
assert_eq!(400, sprite_sheet_set[1].last_sprite_id);
}

#[rstest]
fn test_set_get_by_sprite_id(#[from(sprite_sheet_set_fixture)] sprite_sheet_set: SpriteSheets) {
assert_eq!(
100,
sprite_sheet_set
.get_by_sprite_id(100)
.unwrap()
.first_sprite_id
);
assert_eq!(
200,
sprite_sheet_set
.get_by_sprite_id(200)
.unwrap()
.last_sprite_id
);
assert_eq!(
300,
sprite_sheet_set
.get_by_sprite_id(300)
.unwrap()
.first_sprite_id
);
assert_eq!(
400,
sprite_sheet_set
.get_by_sprite_id(400)
.unwrap()
.last_sprite_id
);
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(99));
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(201));
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(299));
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(401));
}

#[fixture]
fn sprite_sheet_set_fixture() -> SpriteSheets {
let vec = vec![
ContentRecord::SpriteSheet(SpriteSheet {
file: "spritesheet.png".to_string(),
layout: SpriteLayout::default(),
first_sprite_id: 100,
last_sprite_id: 200,
area: 64,
}),
ContentRecord::SpriteSheet(SpriteSheet {
file: "spritesheet2.png".to_string(),
layout: SpriteLayout::default(),
first_sprite_id: 300,
last_sprite_id: 400,
area: 64,
}),
];

vec.into()
}

#[rstest]
#[case(ContentRecord::Unknown, r#"{"type":"unknown"}"#)]
#[case(
Expand Down
5 changes: 5 additions & 0 deletions crates/ryot_sprites/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_ecs::prelude::SystemSet;
pub mod animation;
pub mod loading;
pub mod material;
pub mod sheets;
pub mod update;

pub static SPRITE_SHEET_FOLDER: &str = "sprite-sheets";
Expand Down Expand Up @@ -49,10 +50,14 @@ pub mod prelude {
params::{SpriteOutline, SpriteParams},
SpriteMaterial,
},
sheets::SpriteSheets,
update::update_sprite_system,
SpriteSystems, SPRITE_SHEET_FOLDER,
};

#[cfg(feature = "debug")]
pub use crate::loading::debug::debug_sprites;
}

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion crates/ryot_sprites/src/loading/loaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_render::prelude::Image;
use bevy_utils::HashMap;
use derive_more::{Deref, DerefMut};
use ryot_core::content::{ContentId, ContentType};
use ryot_core::prelude::{FrameGroup, SpriteSheet, SpriteSheets};
use ryot_core::prelude::{FrameGroup, SpriteSheet};

pub struct LoadedAppearance {
pub sprites: Vec<LoadedSprite>,
Expand Down
41 changes: 41 additions & 0 deletions crates/ryot_sprites/src/sheets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use bevy_ecs::prelude::Resource;
use derive_more::{Deref, DerefMut};
use ryot_core::prelude::SpriteSheet;

/// This is a collection of sprite sheets.
/// It contains the sprite sheets and the sprite sheet config.
/// The sprite sheet config is used to calculate the position and size of a sprite in the sprite
/// sheet.
#[derive(Debug, Default, Clone, Deref, DerefMut, Resource)]
pub struct SpriteSheets(Vec<SpriteSheet>);

impl<T> From<&[T]> for SpriteSheets
where
T: Into<Option<SpriteSheet>> + Clone,
{
fn from(content: &[T]) -> Self {
let sprite_sheets = content
.iter()
.filter_map(|content_type| content_type.clone().into())
.collect::<Vec<_>>();

Self(sprite_sheets)
}
}

impl<T> From<Vec<T>> for SpriteSheets
where
T: Into<Option<SpriteSheet>> + Clone,
{
fn from(content: Vec<T>) -> Self {
content.as_slice().into()
}
}

impl SpriteSheets {
/// Returns the sprite sheet that contains the given sprite id.
/// Returns None if the sprite id is not in any of the sprite sheets.
pub fn get_by_sprite_id(&self, sprite_id: u32) -> Option<&SpriteSheet> {
self.iter().find(|sheet| sheet.has_sprite(sprite_id))
}
}
1 change: 1 addition & 0 deletions crates/ryot_sprites/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod sprite_sheets_test;
70 changes: 70 additions & 0 deletions crates/ryot_sprites/src/tests/sprite_sheets_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::prelude::SpriteSheets;
use rstest::{fixture, rstest};
use ryot_core::prelude::{ContentRecord, SpriteLayout, SpriteSheet};

#[rstest]
fn test_from_content(#[from(sprite_sheet_set_fixture)] sprite_sheet_set: SpriteSheets) {
assert_eq!(2, sprite_sheet_set.len());
assert_eq!(100, sprite_sheet_set[0].first_sprite_id);
assert_eq!(200, sprite_sheet_set[0].last_sprite_id);
assert_eq!(300, sprite_sheet_set[1].first_sprite_id);
assert_eq!(400, sprite_sheet_set[1].last_sprite_id);
}

#[rstest]
fn test_set_get_by_sprite_id(#[from(sprite_sheet_set_fixture)] sprite_sheet_set: SpriteSheets) {
assert_eq!(
100,
sprite_sheet_set
.get_by_sprite_id(100)
.unwrap()
.first_sprite_id
);
assert_eq!(
200,
sprite_sheet_set
.get_by_sprite_id(200)
.unwrap()
.last_sprite_id
);
assert_eq!(
300,
sprite_sheet_set
.get_by_sprite_id(300)
.unwrap()
.first_sprite_id
);
assert_eq!(
400,
sprite_sheet_set
.get_by_sprite_id(400)
.unwrap()
.last_sprite_id
);
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(99));
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(201));
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(299));
assert_eq!(None, sprite_sheet_set.get_by_sprite_id(401));
}

#[fixture]
fn sprite_sheet_set_fixture() -> SpriteSheets {
let vec = vec![
ContentRecord::SpriteSheet(SpriteSheet {
file: "spritesheet.png".to_string(),
layout: SpriteLayout::default(),
first_sprite_id: 100,
last_sprite_id: 200,
area: 64,
}),
ContentRecord::SpriteSheet(SpriteSheet {
file: "spritesheet2.png".to_string(),
layout: SpriteLayout::default(),
first_sprite_id: 300,
last_sprite_id: 400,
area: 64,
}),
];

vec.into()
}

0 comments on commit 212d141

Please sign in to comment.