rust_fisher_game/src/player.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

2025-02-16 00:29:53 +03:00
use bevy::prelude::*;
2025-02-19 04:31:26 +03:00
use crate::{PlayerGUIStatus, CHARACTER_SPEED};
2025-02-16 00:29:53 +03:00
#[derive(Debug, Component)]
2025-02-19 04:31:26 +03:00
pub struct Player;
2025-02-16 00:29:53 +03:00
impl Player {
2025-02-16 03:35:13 +03:00
pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2025-02-16 00:29:53 +03:00
let image = asset_server.load::<Image>("character.png");
commands.spawn((
Sprite::from_image(image),
Transform::from_xyz(0.0, 0.0, 0.0),
2025-02-19 04:31:26 +03:00
Player,
2025-02-16 00:29:53 +03:00
));
}
2025-02-16 03:35:13 +03:00
pub fn r#move(
2025-02-16 00:29:53 +03:00
time: Res<Time>,
2025-02-16 03:35:13 +03:00
mut player_query: Query<&mut Transform, With<Player>>,
2025-02-16 00:29:53 +03:00
keyboard_input: Res<ButtonInput<KeyCode>>,
2025-02-19 04:31:26 +03:00
player_gui_status: Res<PlayerGUIStatus>,
2025-02-16 00:29:53 +03:00
) {
2025-02-19 04:31:26 +03:00
if !player_gui_status.looking_inventory {
let mut player = player_query.get_single_mut().unwrap();
2025-02-16 00:29:53 +03:00
2025-02-19 04:31:26 +03:00
let translation = CHARACTER_SPEED * time.delta_secs();
if keyboard_input.pressed(KeyCode::KeyW) {
player.translation.y += translation;
}
if keyboard_input.pressed(KeyCode::KeyS) {
player.translation.y -= translation;
}
if keyboard_input.pressed(KeyCode::KeyA) {
player.translation.x -= translation;
}
if keyboard_input.pressed(KeyCode::KeyD) {
player.translation.x += translation;
}
2025-02-16 00:29:53 +03:00
}
}
}