Hi,
I did a little mod of the Code: moving_dragon
so that the “dragon” can move UP/DOWN + RIGHT/LEFT (diagonal) at the same time
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, movement)
.run();
}
#[derive(Component)]
struct Dragon;
fn setup(//(1)
mut commands: Commands, //(2)
asset_server: Res<AssetServer>//(3)
) {
commands.spawn(Camera2d::default());//(4)
let dragon_image = asset_server.load("dragon_left.png");//(5)
commands
.spawn(Sprite::from_image(dragon_image))//(6)
.insert(Dragon);//(7)
}
fn movement(
keyboard: Res<ButtonInput<KeyCode>>,//(8)
mut dragon_query: Query<&mut Transform, With<Dragon>>,//(9)
) {
let delta = if keyboard.all_pressed([KeyCode::ArrowLeft, KeyCode::ArrowUp]) {//(10)
Vec2::new(-1.0, 1.0)
} else if keyboard.all_pressed([KeyCode::ArrowLeft, KeyCode::ArrowDown]) {
Vec2::new(-1.0, -1.0)
} else if keyboard.all_pressed([KeyCode::ArrowRight, KeyCode::ArrowUp]) {
Vec2::new(1.0, 1.0)
} else if keyboard.all_pressed([KeyCode::ArrowRight, KeyCode::ArrowDown]) {
Vec2::new(1.0, -1.0)
} else if keyboard.pressed(KeyCode::ArrowLeft) {
Vec2::new(-1.0, 0.0)
} else if keyboard.pressed(KeyCode::ArrowRight) {
Vec2::new(1.0, 0.0)
} else if keyboard.pressed(KeyCode::ArrowDown) {
Vec2::new(0.0, -1.0)
} else if keyboard.pressed(KeyCode::ArrowUp) {
Vec2::new(0.0, 1.0)
} else if keyboard.all_pressed([KeyCode::ArrowUp, KeyCode::ArrowLeft]) {
Vec2::new(1.0, 1.0)
} else {
Vec2::ZERO
};
dragon_query.iter_mut().for_each(|mut transform| {//(11)
transform.translation += delta.extend(0.0);//(12)
});
}
But how to change the Code, that the Image is changed when pressing ArrowRight key ?
That the dragon looks to the right when moving right.