-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support basic spritesheet animations
- Loading branch information
1 parent
02a1cb9
commit de58fd8
Showing
5 changed files
with
88 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,66 @@ | ||
use bevy::prelude::*; | ||
use bevy_asset_loader::prelude::*; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
use crate::core::UpdateSet; | ||
use crate::game::step::on_step; | ||
use crate::game::step::Step; | ||
use crate::util::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) {} | ||
pub(super) fn plugin(app: &mut App) { | ||
app.configure::<SpriteAnimation>(); | ||
} | ||
|
||
#[derive(Reflect, Serialize, Deserialize, Copy, Clone)] | ||
pub struct SpriteAnimationFrame { | ||
pub index: usize, | ||
pub steps: usize, | ||
} | ||
|
||
#[derive(Component, Reflect, Serialize, Deserialize, Clone)] | ||
#[reflect(Component)] | ||
pub struct SpriteAnimation { | ||
pub frames: Vec<SpriteAnimationFrame>, | ||
#[serde(skip)] | ||
pub total_steps: usize, | ||
} | ||
|
||
impl Configure for SpriteAnimation { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
app.add_systems( | ||
Update, | ||
update_sprite_animation | ||
.in_set(UpdateSet::Update) | ||
.run_if(on_step(1)), | ||
); | ||
} | ||
} | ||
|
||
impl SpriteAnimation { | ||
// TODO: Does serde support syncing a value after deserialization? | ||
pub fn calculate_total_steps(&mut self) { | ||
self.total_steps = self.frames.iter().map(|x| x.steps).sum(); | ||
} | ||
|
||
/// Calculate the texture atlas index of the animation after `steps` steps. | ||
fn index(&self, steps: usize) -> usize { | ||
let mut steps = steps % self.total_steps; | ||
let mut i = 0; | ||
while steps >= self.frames[i].steps { | ||
steps -= self.frames[i].steps; | ||
i += 1; | ||
} | ||
|
||
struct SpriteAnimationFrame { | ||
index: usize, | ||
steps: usize, | ||
self.frames[i].index | ||
} | ||
} | ||
|
||
struct SpriteAnimation { | ||
texture: Handle<Image>, | ||
layout: Handle<TextureAtlasLayout>, | ||
frames: Vec<SpriteAnimationFrame>, | ||
fn update_sprite_animation( | ||
step: Res<Step>, | ||
mut anim_query: Query<(&SpriteAnimation, &mut TextureAtlas)>, | ||
) { | ||
for (anim, mut atlas) in &mut anim_query { | ||
atlas.index = anim.index(step.total); | ||
} | ||
} |
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