kirby animation state
All checks were successful
Build Bevy Game (Linux) / build (push) Successful in 19m17s
All checks were successful
Build Bevy Game (Linux) / build (push) Successful in 19m17s
This commit is contained in:
parent
b3e1359c63
commit
0eb55dbe58
3 changed files with 36 additions and 20 deletions
|
|
@ -9,12 +9,19 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
#[derive(Component, Default)]
|
||||
pub struct AnimationIndices {
|
||||
pub first: usize,
|
||||
pub last: usize,
|
||||
}
|
||||
|
||||
impl AnimationIndices {
|
||||
pub fn change(&mut self, first: usize, second: usize) {
|
||||
self.first = first;
|
||||
self.last = second;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
pub struct AnimationTimer(pub Timer);
|
||||
|
||||
|
|
|
|||
43
src/kirby.rs
43
src/kirby.rs
|
|
@ -4,9 +4,10 @@ use crate::sphere_collider::SphereCollider;
|
|||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Sprite, PhysicsBody, SphereCollider)]
|
||||
#[require(Sprite, PhysicsBody, SphereCollider, AnimationIndices)]
|
||||
pub struct Kirby {
|
||||
speed_force: f32,
|
||||
sucking: bool,
|
||||
}
|
||||
|
||||
pub fn kirby_spawn(
|
||||
|
|
@ -21,7 +22,7 @@ pub fn kirby_spawn(
|
|||
let animation_indices = AnimationIndices { first: 3, last: 5 }; // idle
|
||||
|
||||
commands.spawn((
|
||||
Kirby { speed_force: 10000.0 },
|
||||
Kirby { speed_force: 10000.0, sucking: false },
|
||||
Sprite::from_atlas_image(
|
||||
texture,
|
||||
TextureAtlas { layout: texture_atlas_layout, index: animation_indices.first },
|
||||
|
|
@ -34,10 +35,6 @@ pub fn kirby_spawn(
|
|||
}
|
||||
|
||||
pub fn get_dir(keys: Res<ButtonInput<KeyCode>>) -> Vec2 {
|
||||
if keys.just_pressed(KeyCode::Space) {
|
||||
println!("SUCKING");
|
||||
}
|
||||
|
||||
let mut dir = Vec2::ZERO;
|
||||
|
||||
if keys.pressed(KeyCode::KeyW) {
|
||||
|
|
@ -56,22 +53,34 @@ pub fn get_dir(keys: Res<ButtonInput<KeyCode>>) -> Vec2 {
|
|||
dir.normalize_or_zero()
|
||||
}
|
||||
|
||||
pub fn kirby_player_move(
|
||||
pub fn kirby_actions(
|
||||
keys: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut PhysicsBody, &Kirby, &mut AnimationIndices), With<Kirby>>,
|
||||
mut query: Query<(&mut PhysicsBody, &mut Kirby, &mut AnimationIndices, &mut Transform, &mut Sprite), With<Kirby>>,
|
||||
) {
|
||||
if keys.pressed(KeyCode::Space) {
|
||||
println!("SUCKING");
|
||||
}
|
||||
let space_just_pressed = keys.just_pressed(KeyCode::Space);
|
||||
let space_just_released = keys.just_released(KeyCode::Space);
|
||||
let dir = get_dir(keys);
|
||||
for (mut body, kirby, mut anim_indices) in &mut query {
|
||||
for (mut body, mut kirby, mut anim_indices, mut transform, mut sprite) in &mut query {
|
||||
if space_just_released {
|
||||
kirby.sucking = false
|
||||
};
|
||||
|
||||
if space_just_pressed {
|
||||
kirby.sucking = true;
|
||||
anim_indices.change(0, 2);
|
||||
};
|
||||
|
||||
if kirby.sucking {
|
||||
continue;
|
||||
};
|
||||
if dir.x != 0.0 {
|
||||
sprite.flip_x = dir.x < 0.0;
|
||||
}
|
||||
body.force += dir * kirby.speed_force;
|
||||
if (dir == Vec2::ZERO) {
|
||||
anim_indices.first = 3;
|
||||
anim_indices.last = 5;
|
||||
if dir == Vec2::ZERO {
|
||||
anim_indices.change(3, 5);
|
||||
} else {
|
||||
anim_indices.first = 6;
|
||||
anim_indices.last = 8;
|
||||
anim_indices.change(6, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use density_grid::DensityGrid;
|
|||
use density_grid::density_grid_force;
|
||||
use density_grid::density_grid_update;
|
||||
use density_grid::init_density_object;
|
||||
use kirby::kirby_player_move;
|
||||
use kirby::kirby_actions;
|
||||
use kirby::kirby_spawn;
|
||||
use map::MapBounds;
|
||||
use physics_body::integrate;
|
||||
|
|
@ -33,7 +33,7 @@ fn main() {
|
|||
.add_systems(PostStartup, init_density_object)
|
||||
.add_systems(Update, animate_sprite)
|
||||
.add_systems(FixedUpdate, (density_grid_update, density_grid_force).chain())
|
||||
.add_systems(FixedUpdate, kirby_player_move)
|
||||
.add_systems(FixedUpdate, kirby_actions)
|
||||
.add_systems(FixedUpdate, bubble_move)
|
||||
.add_systems(FixedPostUpdate, integrate)
|
||||
.run();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue