add player + player movement
This commit is contained in:
parent
c6ba3f6d80
commit
889b175daf
3 changed files with 51 additions and 1 deletions
BIN
assets/sprites/grass.png
Normal file
BIN
assets/sprites/grass.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
BIN
assets/sprites/human.png
Normal file
BIN
assets/sprites/human.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
50
src/main.rs
50
src/main.rs
|
@ -3,5 +3,55 @@ use bevy::prelude::*;
|
|||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, spawn_player)
|
||||
.add_systems(Startup, spawn_camera)
|
||||
.add_systems(Main, move_player)
|
||||
.run();
|
||||
}
|
||||
|
||||
const PLAYER_SPEED: f32 = 500.0;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Player;
|
||||
|
||||
fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn((
|
||||
Player,
|
||||
SpriteBundle {
|
||||
texture: asset_server.load("sprites/human.png"),
|
||||
..Default::default()
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn move_player(
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mut q_player: Query<&mut Transform, With<Player>>,
|
||||
) {
|
||||
let mut direction = Vec3::ZERO;
|
||||
if key.pressed(KeyCode::KeyW) {
|
||||
direction.y += 1.0;
|
||||
}
|
||||
if key.pressed(KeyCode::KeyS) {
|
||||
direction.y -= 1.0;
|
||||
}
|
||||
if key.pressed(KeyCode::KeyD) {
|
||||
direction.x += 1.0;
|
||||
}
|
||||
if key.pressed(KeyCode::KeyA) {
|
||||
direction.x -= 1.0;
|
||||
}
|
||||
if direction != Vec3::ZERO {
|
||||
if let Ok(mut player_transform) = q_player.get_single_mut() {
|
||||
player_transform.translation +=
|
||||
direction.normalize() * PLAYER_SPEED * time.delta_seconds();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_camera(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle {
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue