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.;
|
||||
}
|
||||
|
||||
|
|
|
|||
82
src/utils/input.rs
Normal file
82
src/utils/input.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
use bevy::prelude::*;
|
||||
use enumset::{EnumSet, EnumSetType};
|
||||
|
||||
pub struct GameInputPlugin;
|
||||
|
||||
impl Plugin for GameInputPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(InputPressStatus::default())
|
||||
.add_message::<InputMessage>()
|
||||
.add_systems(Update, process_input);
|
||||
}
|
||||
}
|
||||
|
||||
fn process_input(
|
||||
kb_input: Res<ButtonInput<KeyCode>>,
|
||||
mut ev_input: MessageWriter<InputMessage>,
|
||||
mut pressed: ResMut<InputPressStatus>,
|
||||
) {
|
||||
for m in DEFAULT_KEY_MAP {
|
||||
if kb_input.just_pressed(m.key) {
|
||||
ev_input.write(InputMessage::new(m.button, InputAction::Pressed));
|
||||
pressed.insert(m.button);
|
||||
}
|
||||
if kb_input.just_released(m.key) {
|
||||
ev_input.write(InputMessage::new(m.button, InputAction::Released));
|
||||
pressed.remove(m.button);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(EnumSetType)]
|
||||
pub enum InputButton {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Select,
|
||||
Back,
|
||||
Pause,
|
||||
}
|
||||
|
||||
pub enum InputAction {
|
||||
Pressed,
|
||||
Released,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
pub struct InputMessage {
|
||||
button: InputButton,
|
||||
action: InputAction,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Resource, Deref, DerefMut)]
|
||||
pub struct InputPressStatus(EnumSet<InputButton>);
|
||||
|
||||
pub struct KeyboardMapping {
|
||||
key: KeyCode,
|
||||
button: InputButton,
|
||||
}
|
||||
|
||||
const DEFAULT_KEY_MAP: [KeyboardMapping; 8] = [
|
||||
KeyboardMapping::new(KeyCode::KeyW, InputButton::Up),
|
||||
KeyboardMapping::new(KeyCode::ArrowUp, InputButton::Up),
|
||||
KeyboardMapping::new(KeyCode::KeyA, InputButton::Left),
|
||||
KeyboardMapping::new(KeyCode::KeyS, InputButton::Down),
|
||||
KeyboardMapping::new(KeyCode::KeyD, InputButton::Right),
|
||||
KeyboardMapping::new(KeyCode::KeyI, InputButton::Select),
|
||||
KeyboardMapping::new(KeyCode::KeyO, InputButton::Back),
|
||||
KeyboardMapping::new(KeyCode::Escape, InputButton::Pause),
|
||||
];
|
||||
|
||||
impl InputMessage {
|
||||
pub fn new(button: InputButton, action: InputAction) -> Self {
|
||||
Self { button, action }
|
||||
}
|
||||
}
|
||||
|
||||
impl KeyboardMapping {
|
||||
pub const fn new(key: KeyCode, button: InputButton) -> Self {
|
||||
Self { key, button }
|
||||
}
|
||||
}
|
||||
1
src/utils/mod.rs
Normal file
1
src/utils/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod input;
|
||||
Loading…
Add table
Add a link
Reference in a new issue