forgot to commit animation FILE LOL
This commit is contained in:
74
src/animations.rs
Normal file
74
src/animations.rs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
use bevy::ecs::prelude::{Component, Query, Res};
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use bevy::prelude::{Time, Timer};
|
||||||
|
use bevy::sprite::TextureAtlas;
|
||||||
|
use bevy_ecs_ldtk::ldtk::ldtk_fields::LdtkFields;
|
||||||
|
use bevy_ecs_ldtk::EntityInstance;
|
||||||
|
|
||||||
|
use crate::player::Player;
|
||||||
|
|
||||||
|
#[derive(Component, Default)]
|
||||||
|
pub struct AnimationIndices {
|
||||||
|
first: i32,
|
||||||
|
last: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Deref, DerefMut, Default)]
|
||||||
|
pub struct AnimationTimer(Timer);
|
||||||
|
|
||||||
|
impl AnimationTimer {
|
||||||
|
fn default() -> Self {
|
||||||
|
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<&EntityInstance> for AnimationTimer {
|
||||||
|
fn from(entity_instance: &EntityInstance) -> Self {
|
||||||
|
AnimationTimer(Timer::from_seconds(
|
||||||
|
entity_instance
|
||||||
|
.get_float_field("animation_timer")
|
||||||
|
.expect("items field should be correctly typed")
|
||||||
|
.clone(),
|
||||||
|
TimerMode::Repeating,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&EntityInstance> for AnimationIndices {
|
||||||
|
fn from(entity_instance: &EntityInstance) -> Self {
|
||||||
|
let indices: Vec<i32> = entity_instance
|
||||||
|
.iter_ints_field("animation_indices")
|
||||||
|
.expect("numbers field should be correctly typed")
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let first = indices.get(0).expect("fuck");
|
||||||
|
let last = indices.get(1).expect("fuck");
|
||||||
|
AnimationIndices {
|
||||||
|
first: *first,
|
||||||
|
last: *last,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn animate_player(
|
||||||
|
time: Res<Time>,
|
||||||
|
mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut TextureAtlas), With<Player>>,
|
||||||
|
) {
|
||||||
|
for (indices, mut timer, mut atlas) in &mut query {
|
||||||
|
timer.tick(time.delta());
|
||||||
|
if timer.just_finished() {
|
||||||
|
atlas.index = if atlas.index == indices.last as usize {
|
||||||
|
indices.first as usize
|
||||||
|
} else {
|
||||||
|
atlas.index + 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ShockingAnimationPlugin;
|
||||||
|
|
||||||
|
impl Plugin for ShockingAnimationPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(Update, animate_player);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user