added input subsystem
This commit is contained in:
parent
7ea28fd358
commit
92c8c288bc
5 changed files with 169 additions and 7 deletions
62
Cargo.lock
generated
62
Cargo.lock
generated
|
|
@ -1310,6 +1310,7 @@ name = "bevy_stock_photo"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"enumset",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -2013,6 +2014,40 @@ version = "1.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f"
|
checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling"
|
||||||
|
version = "0.21.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"darling_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_core"
|
||||||
|
version = "0.21.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"ident_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_macro"
|
||||||
|
version = "0.21.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dasp_sample"
|
name = "dasp_sample"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
|
|
@ -2146,6 +2181,27 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "enumset"
|
||||||
|
version = "1.1.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "25b07a8dfbbbfc0064c0a6bdf9edcf966de6b1c33ce344bdeca3b41615452634"
|
||||||
|
dependencies = [
|
||||||
|
"enumset_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "enumset_derive"
|
||||||
|
version = "0.14.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f43e744e4ea338060faee68ed933e46e722fb7f3617e722a5772d7e856d8b3ce"
|
||||||
|
dependencies = [
|
||||||
|
"darling",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "equivalent"
|
name = "equivalent"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
|
|
@ -2637,6 +2693,12 @@ version = "0.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
|
checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ident_case"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "image"
|
name = "image"
|
||||||
version = "0.25.9"
|
version = "0.25.9"
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,5 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.17.3"
|
bevy = "0.17.3"
|
||||||
|
enumset = "1.1.10"
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
|
|
|
||||||
30
src/main.rs
30
src/main.rs
|
|
@ -1,17 +1,33 @@
|
||||||
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
|
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
use crate::utils::input::{GameInputPlugin, InputButton, InputPressStatus};
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct PlayerVWBus;
|
pub struct PlayerVWBus;
|
||||||
|
|
||||||
|
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum GameState {
|
||||||
|
#[default]
|
||||||
|
Navi,
|
||||||
|
Pause,
|
||||||
|
Combat,
|
||||||
|
}
|
||||||
|
|
||||||
const PLAYER_SPEED: f32 = 100.;
|
const PLAYER_SPEED: f32 = 100.;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins((DefaultPlugins, GameInputPlugin))
|
||||||
|
.init_state::<GameState>()
|
||||||
.add_systems(Startup, (setup_scene, spawn_bus, spawn_enemy))
|
.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();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,23 +81,23 @@ fn move_player(
|
||||||
mut player: Single<&mut Transform, With<PlayerVWBus>>,
|
mut player: Single<&mut Transform, With<PlayerVWBus>>,
|
||||||
mut player_sprite: Single<&mut Sprite, With<PlayerVWBus>>,
|
mut player_sprite: Single<&mut Sprite, With<PlayerVWBus>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
kb_input: Res<ButtonInput<KeyCode>>,
|
buttons_pressed: Res<InputPressStatus>,
|
||||||
) {
|
) {
|
||||||
let mut direction = Vec2::ZERO;
|
let mut direction = Vec2::ZERO;
|
||||||
|
|
||||||
if kb_input.pressed(KeyCode::KeyW) {
|
if buttons_pressed.contains(InputButton::Up) {
|
||||||
direction.y += 1.;
|
direction.y += 1.;
|
||||||
}
|
}
|
||||||
|
|
||||||
if kb_input.pressed(KeyCode::KeyS) {
|
if buttons_pressed.contains(InputButton::Down) {
|
||||||
direction.y -= 1.;
|
direction.y -= 1.;
|
||||||
}
|
}
|
||||||
|
|
||||||
if kb_input.pressed(KeyCode::KeyA) {
|
if buttons_pressed.contains(InputButton::Left) {
|
||||||
direction.x -= 1.;
|
direction.x -= 1.;
|
||||||
}
|
}
|
||||||
|
|
||||||
if kb_input.pressed(KeyCode::KeyD) {
|
if buttons_pressed.contains(InputButton::Right) {
|
||||||
direction.x += 1.;
|
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