added input subsystem
This commit is contained in:
parent
7ea28fd358
commit
92c8c288bc
5 changed files with 169 additions and 7 deletions
30
src/main.rs
30
src/main.rs
|
|
@ -1,17 +1,33 @@
|
|||
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
|
||||
use rand::Rng;
|
||||
|
||||
use crate::utils::input::{GameInputPlugin, InputButton, InputPressStatus};
|
||||
|
||||
mod utils;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct PlayerVWBus;
|
||||
|
||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum GameState {
|
||||
#[default]
|
||||
Navi,
|
||||
Pause,
|
||||
Combat,
|
||||
}
|
||||
|
||||
const PLAYER_SPEED: f32 = 100.;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins((DefaultPlugins, GameInputPlugin))
|
||||
.init_state::<GameState>()
|
||||
.add_systems(Startup, (setup_scene, spawn_bus, spawn_enemy))
|
||||
.add_systems(Update, (move_player, follow_player))
|
||||
.add_systems(
|
||||
Update,
|
||||
(move_player.run_if(in_state(GameState::Navi)), follow_player),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
|
|
@ -65,23 +81,23 @@ fn move_player(
|
|||
mut player: Single<&mut Transform, With<PlayerVWBus>>,
|
||||
mut player_sprite: Single<&mut Sprite, With<PlayerVWBus>>,
|
||||
time: Res<Time>,
|
||||
kb_input: Res<ButtonInput<KeyCode>>,
|
||||
buttons_pressed: Res<InputPressStatus>,
|
||||
) {
|
||||
let mut direction = Vec2::ZERO;
|
||||
|
||||
if kb_input.pressed(KeyCode::KeyW) {
|
||||
if buttons_pressed.contains(InputButton::Up) {
|
||||
direction.y += 1.;
|
||||
}
|
||||
|
||||
if kb_input.pressed(KeyCode::KeyS) {
|
||||
if buttons_pressed.contains(InputButton::Down) {
|
||||
direction.y -= 1.;
|
||||
}
|
||||
|
||||
if kb_input.pressed(KeyCode::KeyA) {
|
||||
if buttons_pressed.contains(InputButton::Left) {
|
||||
direction.x -= 1.;
|
||||
}
|
||||
|
||||
if kb_input.pressed(KeyCode::KeyD) {
|
||||
if buttons_pressed.contains(InputButton::Right) {
|
||||
direction.x += 1.;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue