Compare commits

...

2 commits
main ... bevy

Author SHA1 Message Date
edfb0be3c6 use unwrap 2024-07-10 17:27:22 +02:00
889b175daf add player + player movement 2024-07-10 17:18:15 +02:00
3 changed files with 49 additions and 1 deletions

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -3,5 +3,53 @@ 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 {
let mut player_transform = q_player.get_single_mut().unwrap();
player_transform.translation += direction.normalize() * PLAYER_SPEED * time.delta_seconds();
}
}
fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle {
..Default::default()
});
}