ds2k5

ds2k5

Advanced Hands-on Rust: moving_dragon - little mod - diagonal movment

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Sub Categories: