This commit is contained in:
Schrottkatze 2024-11-23 21:53:08 +01:00
parent 126325de8f
commit fa1f11c602
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
2 changed files with 59 additions and 27 deletions

View file

@ -1,6 +1,6 @@
use std::{hash::Hash, time::Duration};
use animation::AnimBundle;
use animation::{run_animations, AnimBundle, Animation};
use bevy::{ecs::system::SystemId, prelude::*, sprite::MaterialMesh2dBundle, utils::HashMap};
use bevy_rapier2d::prelude::*;
@ -34,8 +34,15 @@ impl FromWorld for PlayerSpawnOneshot {
}
pub(super) fn player_plugin(app: &mut App) {
app.add_systems(Update, (move_player,))
.init_resource::<PlayerSpawnOneshot>();
app.add_systems(
Update,
(
move_player,
debug_player_pos,
run_animations::<PlayerAnimations>,
),
)
.init_resource::<PlayerSpawnOneshot>();
}
#[derive(Component, Hash, PartialEq, Eq, Default)]
@ -47,7 +54,6 @@ enum PlayerAnimations {
fn debug_player_pos(query: Query<&Transform, With<Player>>) {
let trans = query.single();
dbg!(trans);
}
fn move_player(
kb_input: Res<ButtonInput<KeyCode>>,
@ -111,27 +117,11 @@ fn add_player(
let anims = animation::AnimationManager::default()
.insert(
PlayerAnimations::Idle,
animation::Animation::new(
TextureAtlas {
layout: layout_idle_handle,
index: 0,
},
2,
4,
tex_idle,
),
Animation::new(layout_idle_handle.clone(), 2, 4, tex_idle.clone()),
)
.insert(
PlayerAnimations::Walk,
animation::Animation::new(
TextureAtlas {
layout: layout_walk_handle,
index: 0,
},
2,
4,
tex_walk,
),
Animation::new(layout_walk_handle.clone(), 4, 10, tex_walk.clone()),
);
commands
@ -142,9 +132,18 @@ fn add_player(
mgr: anims,
},
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::splat(1.)),
..Default::default()
},
transform: (*player_coords).into(),
texture: tex_idle,
..Default::default()
},
TextureAtlas {
layout: layout_idle_handle,
index: 0,
},
))
.insert((
RigidBody::Dynamic,

View file

@ -5,6 +5,31 @@ use bevy::utils::HashMap;
use std::hash::Hash;
pub fn run_animations<Tag: Hash + Eq + Component>(
mut query: Query<(&Tag, &mut AnimationManager<Tag>, &mut TextureAtlas)>,
time: Res<Time>,
) {
for (tag, mut manager, mut atlas) in &mut query {
let anim = manager
.animations
.get_mut(tag)
.expect("animations should exist for all tags!");
anim.frame_timer.tick(time.delta());
dbg!(&anim.frame_timer);
if anim.frame_timer.just_finished() {
dbg!(atlas.index);
if atlas.index == anim.max_index {
atlas.index = 0;
} else {
atlas.index += 1;
}
anim.frame_timer = Animation::timer_from_fps(anim.fps);
}
}
}
#[derive(Bundle)]
pub struct AnimBundle<Tag: Hash + Eq + Component> {
pub tag: Tag,
@ -13,7 +38,7 @@ pub struct AnimBundle<Tag: Hash + Eq + Component> {
#[derive(Default, Component)]
pub(crate) struct AnimationManager<Tag: Hash + Eq> {
animations: HashMap<Tag, Animation>,
pub animations: HashMap<Tag, Animation>,
}
impl<Tag: Hash + Eq> AnimationManager<Tag> {
@ -24,7 +49,7 @@ impl<Tag: Hash + Eq> AnimationManager<Tag> {
}
pub(crate) struct Animation {
atlas: TextureAtlas,
atlas_layout_handle: Handle<TextureAtlasLayout>,
max_index: usize,
fps: u8,
frame_timer: Timer,
@ -32,9 +57,14 @@ pub(crate) struct Animation {
}
impl Animation {
pub fn new(atlas: TextureAtlas, length: usize, fps: u8, tex_handle: Handle<Image>) -> Self {
pub fn new(
atlas_layout_handle: Handle<TextureAtlasLayout>,
length: usize,
fps: u8,
tex_handle: Handle<Image>,
) -> Self {
Self {
atlas,
atlas_layout_handle,
max_index: length - 1,
fps,
frame_timer: Self::timer_from_fps(fps),
@ -44,6 +74,9 @@ impl Animation {
// stolen from https://github.com/bevyengine/bevy/blob/5c759a1be800209f537bea31d32b8ba7e966b0c1/examples/2d/sprite_animation.rs#L53-L55
pub(crate) fn timer_from_fps(fps: u8) -> Timer {
Timer::new(Duration::from_secs_f32(1.0 / (fps as f32)), TimerMode::Once)
Timer::new(
Duration::from_secs_f32(1.0 / (fps as f32)),
TimerMode::Repeating,
)
}
}