| Crates.io | animatron |
| lib.rs | animatron |
| version | 0.2.2 |
| created_at | 2024-12-14 21:20:54.234729+00 |
| updated_at | 2024-12-14 21:37:33.252749+00 |
| description | Animation state management. |
| homepage | |
| repository | https://github.com/tauseefk/animatron |
| max_upload_size | |
| id | 1483431 |
| size | 11,989 |
Companion macro here
Fancy way to describe animation loops in a single array coz cache friendly. This is a lie, all it does is loop over array indices. You can probably use it for other things.
DECLARING THE ENUM
╔═══════╦═══════╦═══════╦═══════╦═══════╦═══════╗
║ ║ ║ ║ ║ ║ ║▐▌
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║▐▌
╚═══════╩═══════╩═══════╩═══════╩═══════╩═══════╝▐▌
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘
[usize; 6]
pub enum Variant { pub trait AnimationLoop {
Idle, implements
Rising, ━━━━━━━━━━━━━━━━━━━━━▶ fn page() -> (usize, usize);
Falling,
} }
MAPPING TO idx/offset
╔═══════╦═══════╦═══════╦═══════╦═══════╦═══════╗
║ ║ ║ ║ ║ ║ ║▐▌
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║▐▌
╚═══════╩═══════╩═══════╩═══╤═══╩═══════╩═══════╝▐▌
▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘
└───────┬───────┘
╵
Idle::pages(); // (offset: 1, size: 3)
╔═══════╦═══════╦═══════╦═══════╦═══════╦═══════╗
║ ║ ║ ║ ║ ║ ║▐▌
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║▐▌
╚═══════╩═══════╩═══════╩═══╤═══╩═══╤═══╩═══════╝▐▌
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▘
└───┬───┘
╵
Rising::pages(); // (offset: 3, size: 2)
FLIPPING THROUGH SPRITES
┌───────┐
│ │
╔═══════╦═══════╦═══════╦═══╧═══╦═══▼═══╦═══════╗
║ ║ ║ ║ ║ ║ ║▐▌
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║▐▌
╚═══════╩═══════╩═══════╩═══════╩═══════╩═══════╝▐▌
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘
animation_state.variant; // Variant::Rising
animation_state.idx; // 3
animation_state.wrapping_next_idx(); // 4
VARIANT TRANSITION
┏━━━━━━━━━━━┓
┃ ┃
┌───┸───┐ ┃
╔═══════╦═══════╦═══════╦═══╧═══╦═══╧═══╦═══▼═══╗
║ ║ ║ ║ ║ ║ ║▐▌
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║▐▌
╚═══════╩═══════╩═══════╩═══════╩═══════╩═══════╝▐▌
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘
animation_state.variant; // Variant::Rising
animation_state.transition_variant(); // ()
animation_state.variant; // Variant::Falling
animation_state.idx; // 5
#[derive(Clone, PartialEq)]
pub enum PlayerAnimationVariant {
Idle,
Rising,
Falling,
}
impl AnimationLoop for PlayerAnimationVariant {
fn page(&self) -> (usize, usize) {
match self {
// return values (idx_offset, loop_size) describe the animation loop
PlayerAnimationVariant::Idle => (0, 3),
PlayerAnimationVariant::Rising => (2, 2),
PlayerAnimationVariant::Falling => (4, 4),
}
}
}
pub struct PlayerAnimationState {
pub variant: PlayerAnimationVariant,
pub idx: usize,
}
Option 1. use the handy macro here
#[derive(AnimationTransitionMacro)]
pub struct PlayerAnimationState {
/// This is needed to tell the compiler the type of your variant enum
#[variant]
pub variant: PlayerAnimationVariant,
pub idx: usize,
}
Option 2. Or Implement AnimationTransition for the newly created struct yourself
impl AnimationTransition<PlayerAnimationVariant> for PlayerAnimationState {
fn wrapping_next_idx(&mut self) -> usize {
// This is example implementation, you can choose to update the page however you like
let current_idx = self.idx;
let (offset, size) = self.variant.page();
self.idx = offset + (current_idx + 1) % size;
self.idx
}
fn transition_variant(&mut self, to: PlayerAnimationVariant) {
let (offset, _) = to.page();
self.variant = to;
self.idx = offset;
}
}