merge
This commit is contained in:
parent
126325de8f
commit
fa1f11c602
2 changed files with 59 additions and 27 deletions
|
@ -1,6 +1,6 @@
|
||||||
use std::{hash::Hash, time::Duration};
|
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::{ecs::system::SystemId, prelude::*, sprite::MaterialMesh2dBundle, utils::HashMap};
|
||||||
|
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
|
@ -34,7 +34,14 @@ impl FromWorld for PlayerSpawnOneshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn player_plugin(app: &mut App) {
|
pub(super) fn player_plugin(app: &mut App) {
|
||||||
app.add_systems(Update, (move_player,))
|
app.add_systems(
|
||||||
|
Update,
|
||||||
|
(
|
||||||
|
move_player,
|
||||||
|
debug_player_pos,
|
||||||
|
run_animations::<PlayerAnimations>,
|
||||||
|
),
|
||||||
|
)
|
||||||
.init_resource::<PlayerSpawnOneshot>();
|
.init_resource::<PlayerSpawnOneshot>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +54,6 @@ enum PlayerAnimations {
|
||||||
|
|
||||||
fn debug_player_pos(query: Query<&Transform, With<Player>>) {
|
fn debug_player_pos(query: Query<&Transform, With<Player>>) {
|
||||||
let trans = query.single();
|
let trans = query.single();
|
||||||
dbg!(trans);
|
|
||||||
}
|
}
|
||||||
fn move_player(
|
fn move_player(
|
||||||
kb_input: Res<ButtonInput<KeyCode>>,
|
kb_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
@ -111,27 +117,11 @@ fn add_player(
|
||||||
let anims = animation::AnimationManager::default()
|
let anims = animation::AnimationManager::default()
|
||||||
.insert(
|
.insert(
|
||||||
PlayerAnimations::Idle,
|
PlayerAnimations::Idle,
|
||||||
animation::Animation::new(
|
Animation::new(layout_idle_handle.clone(), 2, 4, tex_idle.clone()),
|
||||||
TextureAtlas {
|
|
||||||
layout: layout_idle_handle,
|
|
||||||
index: 0,
|
|
||||||
},
|
|
||||||
2,
|
|
||||||
4,
|
|
||||||
tex_idle,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.insert(
|
.insert(
|
||||||
PlayerAnimations::Walk,
|
PlayerAnimations::Walk,
|
||||||
animation::Animation::new(
|
Animation::new(layout_walk_handle.clone(), 4, 10, tex_walk.clone()),
|
||||||
TextureAtlas {
|
|
||||||
layout: layout_walk_handle,
|
|
||||||
index: 0,
|
|
||||||
},
|
|
||||||
2,
|
|
||||||
4,
|
|
||||||
tex_walk,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
commands
|
commands
|
||||||
|
@ -142,9 +132,18 @@ fn add_player(
|
||||||
mgr: anims,
|
mgr: anims,
|
||||||
},
|
},
|
||||||
SpriteBundle {
|
SpriteBundle {
|
||||||
transform: (*player_coords).into(),
|
sprite: Sprite {
|
||||||
|
custom_size: Some(Vec2::splat(1.)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
transform: (*player_coords).into(),
|
||||||
|
texture: tex_idle,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
TextureAtlas {
|
||||||
|
layout: layout_idle_handle,
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
))
|
))
|
||||||
.insert((
|
.insert((
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
|
|
|
@ -5,6 +5,31 @@ use bevy::utils::HashMap;
|
||||||
|
|
||||||
use std::hash::Hash;
|
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)]
|
#[derive(Bundle)]
|
||||||
pub struct AnimBundle<Tag: Hash + Eq + Component> {
|
pub struct AnimBundle<Tag: Hash + Eq + Component> {
|
||||||
pub tag: Tag,
|
pub tag: Tag,
|
||||||
|
@ -13,7 +38,7 @@ pub struct AnimBundle<Tag: Hash + Eq + Component> {
|
||||||
|
|
||||||
#[derive(Default, Component)]
|
#[derive(Default, Component)]
|
||||||
pub(crate) struct AnimationManager<Tag: Hash + Eq> {
|
pub(crate) struct AnimationManager<Tag: Hash + Eq> {
|
||||||
animations: HashMap<Tag, Animation>,
|
pub animations: HashMap<Tag, Animation>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Tag: Hash + Eq> AnimationManager<Tag> {
|
impl<Tag: Hash + Eq> AnimationManager<Tag> {
|
||||||
|
@ -24,7 +49,7 @@ impl<Tag: Hash + Eq> AnimationManager<Tag> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Animation {
|
pub(crate) struct Animation {
|
||||||
atlas: TextureAtlas,
|
atlas_layout_handle: Handle<TextureAtlasLayout>,
|
||||||
max_index: usize,
|
max_index: usize,
|
||||||
fps: u8,
|
fps: u8,
|
||||||
frame_timer: Timer,
|
frame_timer: Timer,
|
||||||
|
@ -32,9 +57,14 @@ pub(crate) struct Animation {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Self {
|
||||||
atlas,
|
atlas_layout_handle,
|
||||||
max_index: length - 1,
|
max_index: length - 1,
|
||||||
fps,
|
fps,
|
||||||
frame_timer: Self::timer_from_fps(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
|
// 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 {
|
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,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue