-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impr: move sprite sheets resource to ryot_sprites
- Loading branch information
Showing
9 changed files
with
123 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod sprite_sheets_test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |