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

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
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
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1033 17470 383
New
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
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
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
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
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1143 25883 760
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
New

Sub Categories: