40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
![]() |
use bevy::prelude::*;
|
||
|
|
||
|
use crate::CHARACTER_SPEED;
|
||
|
|
||
|
#[derive(Debug, Component)]
|
||
|
pub struct Player;
|
||
|
|
||
|
impl Player {
|
||
|
pub fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||
|
let image = asset_server.load::<Image>("character.png");
|
||
|
commands.spawn((
|
||
|
Sprite::from_image(image),
|
||
|
Transform::from_xyz(0.0, 0.0, 0.0),
|
||
|
Player,
|
||
|
));
|
||
|
}
|
||
|
|
||
|
pub fn move_player(
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|