Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reflection to types and register resources #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/animation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::fmt;

use bevy::reflect::prelude::*;

use crate::{clip::ClipId, easing::Easing};

/// An opaque identifier that references an [Animation].
///
/// Returned by [AnimationLibrary::register_animation](crate::prelude::AnimationLibrary::register_animation).
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub struct AnimationId {
pub(crate) value: usize,
}
Expand All @@ -19,7 +22,8 @@ impl fmt::Display for AnimationId {
/// Specifies the duration of an [Animation].
///
/// Defaults to `PerFrame(100)`.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Reflect)]
#[reflect(Debug)]
pub enum AnimationDuration {
/// Specifies the duration of each frame in milliseconds
PerFrame(u32),
Expand All @@ -36,7 +40,8 @@ impl Default for AnimationDuration {
/// Specifies how many times an [Animation] repeats.
///
/// Defaults to `AnimationRepeat::Loop`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum AnimationRepeat {
/// Loops indefinitely
Loop,
Expand All @@ -53,7 +58,8 @@ impl Default for AnimationRepeat {
/// Specifies the direction of an [Animation].
///
/// Defaults to `AnimationDirection::Forwards`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum AnimationDirection {
/// Frames play from left to right
Forwards,
Expand Down Expand Up @@ -98,7 +104,8 @@ impl Default for AnimationDirection {
///
/// let animation_id = library.register_animation(animation);
/// ```
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Reflect)]
#[reflect(Debug)]
pub struct Animation {
/// The IDs of the [Clip](crate::prelude::Clip)s that compose this animation
clip_ids: Vec<ClipId>,
Expand Down
7 changes: 6 additions & 1 deletion src/animator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use bevy::{
ecs::{
entity::Entity,
event::EventWriter,
reflect::*,
system::{Query, Resource},
},
reflect::prelude::*,
sprite::TextureAtlas,
time::Time,
};
Expand All @@ -22,6 +24,8 @@ use crate::{

use self::{iterator::AnimationIterator, iterator::IteratorFrame};

#[derive(Debug, Reflect)]
#[reflect(Debug)]
/// An instance of an animation that is currently being played
struct AnimationInstance {
animation_id: AnimationId,
Expand All @@ -35,7 +39,8 @@ struct AnimationInstance {
}

/// The animator is responsible for playing animations as time advances.
#[derive(Resource, Default)]
#[derive(Resource, Debug, Default, Reflect)]
#[reflect(Resource, Debug, Default)]
pub struct Animator {
/// Instances of animations currently being played.
/// Each animation instance is associated to an entity with a [SpritesheetAnimation] component.
Expand Down
10 changes: 7 additions & 3 deletions src/animator/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::log::warn;
use bevy::{log::warn, reflect::prelude::*};

use crate::{
animation::{AnimationDirection, AnimationDuration, AnimationId, AnimationRepeat},
Expand All @@ -10,7 +10,8 @@ use crate::{
};

/// A pre-computed frame of animation, ready to be played back.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Reflect)]
#[reflect(Debug)]
pub struct CacheFrame {
pub atlas_index: usize,
pub duration: u32,
Expand All @@ -27,7 +28,8 @@ pub struct CacheFrame {
/// The iterator & animator will also generate extra events that cannot be cached:
/// - ClipRepetitionEnd, ClipEnd, AnimationRepetitionEnd on the first frame of the repetitions
/// - AnimationEnd after the last frame
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum AnimationCacheEvent {
MarkerHit {
marker_id: AnimationMarkerId,
Expand All @@ -43,6 +45,8 @@ pub enum AnimationCacheEvent {
},
}

#[derive(Debug, Reflect)]
#[reflect(Debug)]
/// The [AnimationCache] contains pre-computed frames for an animation.
///
/// The idea is to cache for each frame its atlas index, duration and emitted events
Expand Down
10 changes: 7 additions & 3 deletions src/animator/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use bevy::log::warn;
use bevy::{log::warn, reflect::prelude::*};

use crate::{
animation::AnimationDirection, clip::ClipId,
Expand All @@ -10,7 +10,8 @@ use crate::{
use super::cache::{AnimationCache, AnimationCacheEvent, CacheFrame};

/// Same as [CacheFrame] but with `animation_repetition`
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Reflect)]
#[reflect(Debug)]
pub struct IteratorFrame {
pub atlas_index: usize,
pub duration: u32,
Expand All @@ -23,7 +24,8 @@ pub struct IteratorFrame {
/// A partial version of AnimationEvent.
///
/// The animation will promote them to regular AnimationEvents and add the information available at its level.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum AnimationIteratorEvent {
MarkerHit {
marker_id: AnimationMarkerId,
Expand All @@ -43,6 +45,8 @@ pub enum AnimationIteratorEvent {
},
}

#[derive(Debug, Reflect)]
#[reflect(Debug)]
/// An iterator that advances an animation frame by frame.
///
/// `next()` will produce frames until the end of the animation.
Expand Down
8 changes: 6 additions & 2 deletions src/clip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{collections::HashMap, fmt};

use bevy::reflect::prelude::*;

use crate::{
animation::{AnimationDirection, AnimationDuration},
easing::Easing,
Expand All @@ -9,7 +11,8 @@ use crate::{
/// An opaque identifier that references a [Clip].
///
/// Returned by [AnimationLibrary::register_clip](crate::prelude::AnimationLibrary::register_clip).
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub struct ClipId {
pub(crate) value: usize,
}
Expand Down Expand Up @@ -72,7 +75,8 @@ impl fmt::Display for ClipId {
///
/// let composite_animation = Animation::from_clips([slow_clip_id, fast_clip_id]);
/// ```
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Reflect)]
#[reflect(Debug)]
pub struct Clip {
/// Indices into the layout of a TextureAtlas component
atlas_indices: Vec<usize>,
Expand Down
6 changes: 4 additions & 2 deletions src/components/sprite3d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bevy::{
asset::Handle,
color::Color,
ecs::{bundle::Bundle, component::Component},
ecs::prelude::*,
math::{Rect, Vec2},
reflect::prelude::*,
render::{
texture::Image,
view::{InheritedVisibility, ViewVisibility, Visibility},
Expand All @@ -16,7 +17,8 @@ use bevy::{
/// This contains similar fields as Bevy's [Sprite](bevy::sprite::Sprite).
///
/// This is commonly used as a component within [Sprite3dBundle].
#[derive(Component)]
#[derive(Component, Debug, Reflect)]
#[reflect(Component, Debug)]
pub struct Sprite3d {
/// A color to tint the sprite with.
///
Expand Down
8 changes: 5 additions & 3 deletions src/components/spritesheet_animation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bevy::ecs::component::Component;
use bevy::{ecs::prelude::*, reflect::prelude::*};

use crate::animation::AnimationId;

// The progress of an animation being played.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, Default, PartialEq, Hash)]
pub struct AnimationProgress {
/// The index of the active frame of the animation
///
Expand Down Expand Up @@ -61,7 +62,8 @@ pub struct AnimationProgress {
/// ));
/// }
/// ```
#[derive(Component, Debug, Clone)]
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Debug)]
pub struct SpritesheetAnimation {
/// The ID of the animation to play
///
Expand Down
8 changes: 6 additions & 2 deletions src/easing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::f32::consts::PI;

use bevy::reflect::prelude::*;

/// Variety to associate with [Easing]s to tune the acceleration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum EasingVariety {
Quadratic,
Cubic,
Expand Down Expand Up @@ -39,7 +42,8 @@ pub enum EasingVariety {
///
/// - <https://easings.net/>
/// - <http://robertpenner.com/easing/penner_chapter7_tweening.pdf>
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, Default, PartialEq, Hash)]
pub enum Easing {
/// Linear interpolation
#[default]
Expand Down
8 changes: 6 additions & 2 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::fmt;

use bevy::ecs::{entity::Entity, event::Event};
use bevy::{
ecs::{entity::Entity, event::Event},
reflect::prelude::*,
};

use crate::{animation::AnimationId, clip::ClipId};

/// An opaque identifier that references an animation marker.
///
/// Returned by [AnimationLibrary::new_marker](crate::prelude::AnimationLibrary::new_marker).
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub struct AnimationMarkerId {
pub(crate) value: usize,
}
Expand Down
5 changes: 3 additions & 2 deletions src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
sync::Arc,
};

use bevy::prelude::Resource;
use bevy::{ecs::reflect::*, prelude::Resource, reflect::prelude::*};

use crate::{
animator::cache::AnimationCache,
Expand Down Expand Up @@ -51,7 +51,8 @@ pub enum LibraryError {
/// // ... Assign the animation to a SpritesheetAnimation component ...
/// }
/// ```
#[derive(Resource, Default)]
#[derive(Resource, Default, Reflect)]
#[reflect(Resource, Default)]
pub struct AnimationLibrary {
/// All the clips
clips: HashMap<ClipId, Clip>,
Expand Down
3 changes: 3 additions & 0 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ impl Plugin for SpritesheetAnimationPlugin {
app
// The animation library, for creating clips, animations and markers
.init_resource::<AnimationLibrary>()
.register_type::<AnimationLibrary>()
// The animator responsible for running animations
.init_resource::<Animator>()
.register_type::<Animator>()
// Cache for 3D sprites
.init_resource::<sprite3d::Cache>()
.register_type::<sprite3d::Cache>()
// Animations events
.add_event::<AnimationEvent>()
// Systems
Expand Down
9 changes: 6 additions & 3 deletions src/systems/sprite3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use bevy::{
use crate::components::sprite3d::Sprite3d;

/// Cached data for the 3D sprites
#[derive(Resource, Default)]
#[derive(Resource, Debug, Default, Reflect)]
#[reflect(Resource, Debug, Default)]
pub struct Cache {
/// Materials used by 3D sprites.
///
Expand All @@ -35,7 +36,8 @@ pub struct Cache {
}

/// Uniquely identifies a sprite material
#[derive(Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq, Reflect)]
#[reflect(Debug, Hash, PartialEq)]
struct MaterialId {
image: Handle<Image>,
color: u32,
Expand All @@ -51,7 +53,8 @@ impl MaterialId {
}

/// Uniquely identifies a sprite mesh
#[derive(Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq, Reflect)]
#[reflect(Debug, Hash, PartialEq)]
struct MeshId {
sprite_custom_size: [u32; 2],
sprite_anchor: [u32; 2],
Expand Down