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

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
AstonJ
Or looking forward to? :nerd_face:
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New

Sub Categories: