ANIMATION HELL 1999
This commit is contained in:
parent
fa1f11c602
commit
fb1b776e78
2 changed files with 27 additions and 7 deletions
|
@ -62,13 +62,16 @@ fn move_player(
|
||||||
&Transform,
|
&Transform,
|
||||||
&mut Player,
|
&mut Player,
|
||||||
&mut KinematicCharacterController,
|
&mut KinematicCharacterController,
|
||||||
|
&mut Sprite,
|
||||||
|
&mut PlayerAnimations,
|
||||||
)>,
|
)>,
|
||||||
mut camera_query: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
mut camera_query: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
||||||
phys: Query<&KinematicCharacterControllerOutput>,
|
phys: Query<&KinematicCharacterControllerOutput>,
|
||||||
player_coords: Res<PlayerCoords>,
|
player_coords: Res<PlayerCoords>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let (mut vel, p_transform, mut player, mut controller) = query.single_mut();
|
let (mut vel, p_transform, mut player, mut controller, mut sprite, mut anim_state) =
|
||||||
|
query.single_mut();
|
||||||
let (mut cam_transform) = camera_query.single_mut();
|
let (mut cam_transform) = camera_query.single_mut();
|
||||||
let (output) = phys.get_single();
|
let (output) = phys.get_single();
|
||||||
if player.move_cooldown.tick(time.delta()).finished() {
|
if player.move_cooldown.tick(time.delta()).finished() {
|
||||||
|
@ -76,7 +79,7 @@ fn move_player(
|
||||||
let mut move_x = 0;
|
let mut move_x = 0;
|
||||||
let mut move_y = 0;
|
let mut move_y = 0;
|
||||||
let mut jump = false;
|
let mut jump = false;
|
||||||
|
let grounded = output.is_ok_and(|x| x.grounded);
|
||||||
if kb_input.pressed(KeyCode::KeyA) {
|
if kb_input.pressed(KeyCode::KeyA) {
|
||||||
moved = true;
|
moved = true;
|
||||||
move_x -= 1;
|
move_x -= 1;
|
||||||
|
@ -85,7 +88,7 @@ fn move_player(
|
||||||
moved = true;
|
moved = true;
|
||||||
move_x += 1;
|
move_x += 1;
|
||||||
}
|
}
|
||||||
if kb_input.pressed(KeyCode::Space) && output.is_ok_and(|x| x.grounded) {
|
if kb_input.pressed(KeyCode::Space) && grounded {
|
||||||
moved = true;
|
moved = true;
|
||||||
move_y += 1;
|
move_y += 1;
|
||||||
}
|
}
|
||||||
|
@ -95,6 +98,13 @@ fn move_player(
|
||||||
|
|
||||||
controller.translation = Some(Vec2::new(move_x as f32 * 6., 0. - 0.01));
|
controller.translation = Some(Vec2::new(move_x as f32 * 6., 0. - 0.01));
|
||||||
vel.linvel += Vec2::new(0., move_y as f32 * 3. * METER);
|
vel.linvel += Vec2::new(0., move_y as f32 * 3. * METER);
|
||||||
|
|
||||||
|
if moved && move_x != 0 && grounded {
|
||||||
|
*anim_state = PlayerAnimations::Walk;
|
||||||
|
sprite.flip_x = move_x == -1;
|
||||||
|
} else if !moved {
|
||||||
|
*anim_state = PlayerAnimations::Idle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,21 +6,31 @@ use bevy::utils::HashMap;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
pub fn run_animations<Tag: Hash + Eq + Component>(
|
pub fn run_animations<Tag: Hash + Eq + Component>(
|
||||||
mut query: Query<(&Tag, &mut AnimationManager<Tag>, &mut TextureAtlas)>,
|
mut query: Query<(
|
||||||
|
&Tag,
|
||||||
|
&mut AnimationManager<Tag>,
|
||||||
|
&mut TextureAtlas,
|
||||||
|
&mut Handle<Image>,
|
||||||
|
)>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
for (tag, mut manager, mut atlas) in &mut query {
|
for (tag, mut manager, mut atlas, mut tex_handle) in &mut query {
|
||||||
let anim = manager
|
let anim = manager
|
||||||
.animations
|
.animations
|
||||||
.get_mut(tag)
|
.get_mut(tag)
|
||||||
.expect("animations should exist for all tags!");
|
.expect("animations should exist for all tags!");
|
||||||
|
|
||||||
|
*tex_handle = anim.tex.clone_weak();
|
||||||
|
atlas.layout = anim.atlas_layout_handle.clone_weak();
|
||||||
|
if atlas.index > anim.max_index {
|
||||||
|
atlas.index = 0
|
||||||
|
}
|
||||||
|
|
||||||
anim.frame_timer.tick(time.delta());
|
anim.frame_timer.tick(time.delta());
|
||||||
dbg!(&anim.frame_timer);
|
|
||||||
|
|
||||||
if anim.frame_timer.just_finished() {
|
if anim.frame_timer.just_finished() {
|
||||||
dbg!(atlas.index);
|
dbg!(atlas.index);
|
||||||
if atlas.index == anim.max_index {
|
if atlas.index >= anim.max_index {
|
||||||
atlas.index = 0;
|
atlas.index = 0;
|
||||||
} else {
|
} else {
|
||||||
atlas.index += 1;
|
atlas.index += 1;
|
||||||
|
|
Loading…
Add table
Reference in a new issue