animatron
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
Usage
- Create an enum to define animation loops
- Implement AnimationLoop for the enum
- Create a struct to store current animation state
- And finally:
Option 1. use the handy macro here
Option 2. Or Implement AnimationTransition for the newly created struct yourself