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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
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
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Sub Categories: