refactor: reorganize systems properly, move events to events.rs

This commit is contained in:
2025-08-15 09:48:16 -05:00
parent 2c65048fb0
commit b9bae99a4c
8 changed files with 27 additions and 30 deletions

View File

@@ -5,13 +5,15 @@ use bevy_ecs::{
use crate::{
error::GameError,
game::events::GameEvent,
input::commands::GameCommand,
systems::components::{GlobalState, PlayerControlled, Velocity},
events::GameEvent,
systems::{
components::{GlobalState, PlayerControlled, Velocity},
input::GameCommand,
},
};
// Handles
pub fn interact_system(
pub fn player_system(
mut events: EventReader<GameEvent>,
mut state: ResMut<GlobalState>,
mut players: Query<(&PlayerControlled, &mut Velocity)>,

69
src/systems/input.rs Normal file
View File

@@ -0,0 +1,69 @@
use std::collections::HashMap;
use bevy_ecs::{
event::EventWriter,
resource::Resource,
system::{NonSendMut, Res},
};
use sdl2::{event::Event, keyboard::Keycode, EventPump};
use crate::{entity::direction::Direction, events::GameEvent};
#[derive(Debug, Clone, Copy)]
pub enum GameCommand {
MovePlayer(Direction),
Exit,
TogglePause,
ToggleDebug,
MuteAudio,
ResetLevel,
}
#[derive(Debug, Clone, Resource)]
pub struct Bindings {
key_bindings: HashMap<Keycode, GameCommand>,
}
impl Default for Bindings {
fn default() -> Self {
let mut key_bindings = HashMap::new();
// Player movement
key_bindings.insert(Keycode::Up, GameCommand::MovePlayer(Direction::Up));
key_bindings.insert(Keycode::W, GameCommand::MovePlayer(Direction::Up));
key_bindings.insert(Keycode::Down, GameCommand::MovePlayer(Direction::Down));
key_bindings.insert(Keycode::S, GameCommand::MovePlayer(Direction::Down));
key_bindings.insert(Keycode::Left, GameCommand::MovePlayer(Direction::Left));
key_bindings.insert(Keycode::A, GameCommand::MovePlayer(Direction::Left));
key_bindings.insert(Keycode::Right, GameCommand::MovePlayer(Direction::Right));
key_bindings.insert(Keycode::D, GameCommand::MovePlayer(Direction::Right));
// Game actions
key_bindings.insert(Keycode::P, GameCommand::TogglePause);
key_bindings.insert(Keycode::Space, GameCommand::ToggleDebug);
key_bindings.insert(Keycode::M, GameCommand::MuteAudio);
key_bindings.insert(Keycode::R, GameCommand::ResetLevel);
key_bindings.insert(Keycode::Escape, GameCommand::Exit);
key_bindings.insert(Keycode::Q, GameCommand::Exit);
Self { key_bindings }
}
}
pub fn input_system(bindings: Res<Bindings>, mut writer: EventWriter<GameEvent>, mut pump: NonSendMut<&'static mut EventPump>) {
for event in pump.poll_iter() {
match event {
Event::Quit { .. } => {
writer.write(GameEvent::Command(GameCommand::Exit));
}
Event::KeyDown { keycode: Some(key), .. } => {
let command = bindings.key_bindings.get(&key).copied();
if let Some(command) = command {
tracing::info!("triggering command: {:?}", command);
writer.write(GameEvent::Command(command));
}
}
_ => {}
}
}
}

View File

@@ -4,6 +4,7 @@
//! and resources.
pub mod components;
pub mod interact;
pub mod control;
pub mod input;
pub mod movement;
pub mod render;