feat: ✨ player movement
This commit is contained in:
parent
f88b5a1e6e
commit
dd0d1e8643
5 changed files with 4927 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
4862
Cargo.lock
generated
Normal file
4862
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "fisher"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bevy = "0.15.2"
|
BIN
assets/character.png
Normal file
BIN
assets/character.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 231 B |
57
src/main.rs
Normal file
57
src/main.rs
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
use bevy::{
|
||||||
|
app::{App, Startup, Update},
|
||||||
|
asset::AssetServer,
|
||||||
|
image::Image,
|
||||||
|
input::ButtonInput,
|
||||||
|
prelude::{Camera2d, Commands, Component, KeyCode, Query, Res, Transform, With},
|
||||||
|
sprite::Sprite,
|
||||||
|
time::Time,
|
||||||
|
DefaultPlugins,
|
||||||
|
};
|
||||||
|
|
||||||
|
const CHARACTER_SPEED: f32 = 300.0;
|
||||||
|
|
||||||
|
#[derive(Debug, Component)]
|
||||||
|
struct Player {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
|
||||||
|
App::new()
|
||||||
|
.add_plugins(DefaultPlugins)
|
||||||
|
.add_systems(Startup, load_character)
|
||||||
|
.add_systems(Update, move_character)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_character(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
|
commands.spawn(Camera2d::default());
|
||||||
|
let image = asset_server.load::<Image>("character.png");
|
||||||
|
commands.spawn((
|
||||||
|
Sprite::from_image(image),
|
||||||
|
Transform::from_xyz(0.0, 0.0, 0.0),
|
||||||
|
Player {},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_character(
|
||||||
|
time: Res<Time>,
|
||||||
|
mut character_transform: Query<&mut Transform, With<Player>>,
|
||||||
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
) {
|
||||||
|
let mut character_transform = character_transform.get_single_mut().unwrap();
|
||||||
|
|
||||||
|
let translation = CHARACTER_SPEED * time.delta_secs();
|
||||||
|
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||||
|
character_transform.translation.y += translation;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||||
|
character_transform.translation.y -= translation;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||||
|
character_transform.translation.x -= translation;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||||
|
character_transform.translation.x += translation;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue