Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
merwaaan committed Sep 2, 2024
1 parent b335821 commit 24bab78
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 192 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ jobs:
run: cargo doc --no-deps

- name: Run tests
# Enable expose-internals-to-tests so that tests/doctests get access to some private constructors
run: cargo test --features expose-internals-to-tests
run: cargo test
8 changes: 0 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ exclude = [

[dependencies]
bevy = { version = "0.14.0", default-features = false, features = ["bevy_pbr", "bevy_sprite"] }
itertools = "0.12.1"
visibility = "0.1.0"

[dev-dependencies]
approx = "0.5.1"
Expand All @@ -28,12 +26,6 @@ rand = "0.8.5"
[profile.test]
inherits = "release"

[features]
# Enable this feature so that the tests/doctests get access to some private constructors.
#
# > cargo test --features expose-internals-to-tests
expose-internals-to-tests = []

[[bench]]
name = "basic"
harness = false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn spawn_character(mut commands: Commands, mut library: ResMut<AnimationLibrary>

commands.spawn((
Sprite3dBuilder::from_image(texture.clone())
.with_atlas(atlas_layout_handle)
.with_atlas(atlas_layout)
.with_anchor(Anchor::BottomRight)
.build(),
SpritesheetAnimation::from_id(animation_id)
Expand Down
15 changes: 7 additions & 8 deletions examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use rand::{seq::SliceRandom, Rng};

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_plugins(SpritesheetAnimationPlugin)
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, (update_on_keypress, orbit, draw_gizmos))
.run();
Expand Down Expand Up @@ -45,7 +47,7 @@ fn setup(

let texture = assets.load("character.png");

let atlas_layout_handle = atlas_layouts.add(spritesheet.atlas_layout(96, 96));
let atlas_layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

// Spawn 3D sprites

Expand All @@ -70,10 +72,7 @@ fn setup(

for (i, builder) in sprite_builders.iter().enumerate() {
commands.spawn((
builder
.clone()
.with_atlas(atlas_layout_handle.clone())
.build(),
builder.clone().with_atlas(atlas_layout.clone()).build(),
SpritesheetAnimation::from_id(animation_id),
Orbit {
start_angle: i as f32 * std::f32::consts::TAU / sprite_builders.len() as f32,
Expand All @@ -85,7 +84,7 @@ fn setup(

commands.spawn(
Sprite3dBuilder::from_image(texture.clone())
.with_atlas(atlas_layout_handle.clone())
.with_atlas(atlas_layout.clone())
.with_color(Color::linear_rgb(1.0, 0.0, 0.0))
.build(),
);
Expand Down
6 changes: 4 additions & 2 deletions examples/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use bevy_spritesheet_animation::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SpritesheetAnimationPlugin)
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, control_character)
.run();
Expand Down
6 changes: 4 additions & 2 deletions examples/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use bevy_spritesheet_animation::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SpritesheetAnimationPlugin)
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, setup)
.run();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ use bevy_spritesheet_animation::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SpritesheetAnimationPlugin)
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, show_triggered_events)
.add_systems(Update, spawn_visual_effects)
Expand Down
6 changes: 4 additions & 2 deletions examples/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use common::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SpritesheetAnimationPlugin)
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, setup)
.run();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use bevy_spritesheet_animation::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SpritesheetAnimationPlugin)
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, keyboard)
.run();
Expand Down
2 changes: 1 addition & 1 deletion src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Default for AnimationDirection {
///
/// ```
/// # use bevy_spritesheet_animation::prelude::*;
/// # let mut library = AnimationLibrary::new();
/// # let mut library = AnimationLibrary::default();
/// let some_clip = Clip::from_frames([1, 2, 3])
/// .with_duration(AnimationDuration::PerRepetition(2000));
///
Expand Down
121 changes: 58 additions & 63 deletions src/animator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,14 @@ struct AnimationInstance {
}

/// The animator is responsible for playing animations as time advances.
#[derive(Resource)]
pub(crate) struct Animator {
#[derive(Resource, Default)]
pub struct Animator {
/// Instances of animations currently being played.
/// Each animation instance is associated to an entity with a [SpritesheetAnimation] component.
animation_instances: HashMap<Entity, AnimationInstance>,
}

impl Animator {
pub fn new() -> Self {
Self {
animation_instances: HashMap::new(),
}
}

/// Plays the animations
pub fn update(
&mut self,
Expand Down Expand Up @@ -137,7 +131,7 @@ impl Animator {
entity_animation.progress = animation_instance
.current_frame
.as_ref()
.map(|(_, progress)| progress.clone())
.map(|(_, progress)| *progress)
.unwrap_or_default()
}
}
Expand All @@ -155,14 +149,14 @@ impl Animator {
animation_instance.accumulated_time +=
(time.delta_seconds() * entity_animation.speed_factor * 1000.0) as u32;

while let Some(frame) = animation_instance
while let Some(current_frame) = animation_instance
.current_frame
.as_ref()
.filter(|frame| animation_instance.accumulated_time > frame.0.duration)
{
// Consume the elapsed time

animation_instance.accumulated_time -= frame.0.duration;
animation_instance.accumulated_time -= current_frame.0.duration;

// Fetch the next frame

Expand All @@ -176,25 +170,25 @@ impl Animator {
.or_else(|| {
// The animation is over

// Emit all the end events if the animation just ended
// Emit the end events if the animation just ended

event_writer.send(AnimationEvent::ClipRepetitionEnd {
entity,
animation_id: animation_instance.animation_id,
clip_id: frame.0.clip_id,
clip_repetition: frame.0.clip_repetition,
clip_id: current_frame.0.clip_id,
clip_repetition: current_frame.0.clip_repetition,
});

event_writer.send(AnimationEvent::ClipEnd {
entity,
animation_id: animation_instance.animation_id,
clip_id: frame.0.clip_id,
clip_id: current_frame.0.clip_id,
});

event_writer.send(AnimationEvent::AnimationRepetitionEnd {
entity,
animation_id: animation_instance.animation_id,
animation_repetition: frame.0.animation_repetition,
animation_repetition: current_frame.0.animation_repetition,
});

event_writer.send(AnimationEvent::AnimationEnd {
Expand All @@ -220,66 +214,67 @@ impl Animator {
if let Some((frame, progress)) = &maybe_frame {
// Update the sprite

atlas.index = frame.atlas_index;
if atlas.index != frame.atlas_index {
// prevents needless "Changed" events
atlas.index = frame.atlas_index;
}

animation.progress = *progress;

// Emit events

let events = Animator::promote_events(&frame.events, animation.animation_id, entity);

for event in events {
event_writer.send(event);
}
Animator::emit_events(&frame.events, animation.animation_id, entity, event_writer);
}

maybe_frame
}

/// Promotes AnimationIteratorEvents to regular AnimationEvents
fn promote_events(
fn emit_events(
animation_events: &[AnimationIteratorEvent],
animation_id: AnimationId,
entity: &Entity,
) -> Vec<AnimationEvent> {
animation_events
.iter()
.map(|event| match event {
AnimationIteratorEvent::MarkerHit {
marker_id,
animation_repetition,
clip_id,
clip_repetition,
} => AnimationEvent::MarkerHit {
entity: *entity,
marker_id: *marker_id,
animation_id,
animation_repetition: *animation_repetition,
clip_id: *clip_id,
clip_repetition: *clip_repetition,
},
AnimationIteratorEvent::ClipRepetitionEnd {
clip_id,
clip_repetition,
} => AnimationEvent::ClipRepetitionEnd {
entity: *entity,
animation_id,
clip_id: *clip_id,
clip_repetition: *clip_repetition,
},
AnimationIteratorEvent::ClipEnd { clip_id } => AnimationEvent::ClipEnd {
entity: *entity,
animation_id,
clip_id: *clip_id,
},
AnimationIteratorEvent::AnimationRepetitionEnd {
animation_repetition,
} => AnimationEvent::AnimationRepetitionEnd {
entity: *entity,
animation_id,
animation_repetition: *animation_repetition,
event_writer: &mut EventWriter<AnimationEvent>,
) {
animation_events.iter().for_each(|event| {
event_writer.send(
// Promote AnimationIteratorEvents to regular AnimationEvents
match event {
AnimationIteratorEvent::MarkerHit {
marker_id,
animation_repetition,
clip_id,
clip_repetition,
} => AnimationEvent::MarkerHit {
entity: *entity,
marker_id: *marker_id,
animation_id,
animation_repetition: *animation_repetition,
clip_id: *clip_id,
clip_repetition: *clip_repetition,
},
AnimationIteratorEvent::ClipRepetitionEnd {
clip_id,
clip_repetition,
} => AnimationEvent::ClipRepetitionEnd {
entity: *entity,
animation_id,
clip_id: *clip_id,
clip_repetition: *clip_repetition,
},
AnimationIteratorEvent::ClipEnd { clip_id } => AnimationEvent::ClipEnd {
entity: *entity,
animation_id,
clip_id: *clip_id,
},
AnimationIteratorEvent::AnimationRepetitionEnd {
animation_repetition,
} => AnimationEvent::AnimationRepetitionEnd {
entity: *entity,
animation_id,
animation_repetition: *animation_repetition,
},
},
})
.collect()
);
});
}
}
Loading

0 comments on commit 24bab78

Please sign in to comment.