Actionsystem

This commit is contained in:
Julian
2022-01-03 18:39:06 +01:00
parent 5d32243dce
commit b7f343b524
3 changed files with 71 additions and 17 deletions

View File

@@ -1,25 +1,78 @@
use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use std::collections::HashMap;
use std::collections::VecDeque;
mod bundles;
use bundles::camera::*;
struct ActionBindings
{
bindings: HashMap<KeyCode, Action>,
}
#[derive(Copy, Clone)]
enum Action
{
Forward,
Backward,
Left,
Right,
Up,
Down
}
struct Actions {
actions: VecDeque<Action>,
}
fn main() {
let mut binds = ActionBindings {
bindings: HashMap::new(),
};
binds.bindings.insert(KeyCode::W, Action::Forward);
binds.bindings.insert(KeyCode::A, Action::Left);
binds.bindings.insert(KeyCode::S, Action::Backward);
binds.bindings.insert(KeyCode::D, Action::Right);
binds.bindings.insert(KeyCode::Space, Action::Up);
binds.bindings.insert(KeyCode::C, Action::Down);
App::build()
.insert_resource(Msaa {samples: 4})
.insert_resource(binds)
.insert_resource(Actions { actions: VecDeque::new() })
.add_plugins(DefaultPlugins)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_startup_system(startup.system())
.add_startup_system(initial_grab_cursor.system())
.add_system(camera_movement_system.system())
.add_system(camera_update_system.system())
.add_system(cursor_grab.system())
.add_system(binding_action_converter_system.system())
.run();
}
fn binding_action_converter_system(
keyboard_input: Res<Input<KeyCode>>,
binding_input: Res<ActionBindings>,
mut actions: ResMut<Actions>,
)
{
for key in keyboard_input.get_pressed()
{
match binding_input.bindings.get(key) {
Some(action) => actions.actions.push_back(action.clone()),
None => (),
}
}
}
fn camera_movement_system(
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
mut action_input: ResMut<Actions>,
windows: Res<Windows>,
mut mouse_input: EventReader<MouseMotion>,
mut query: Query<(&mut Transform, &mut FPSCamera)>)
@@ -50,22 +103,21 @@ fn camera_movement_system(
let forward = -Vec3::new(local_z.x, 0., local_z.z);
let right = Vec3::new(local_z.z, 0., -local_z.x);
for key in keyboard_input.get_pressed()
{
match key {
KeyCode::W => transform.translation += forward * time.delta_seconds() * walking_speed,
KeyCode::A => transform.translation += -right * time.delta_seconds() * walking_speed,
KeyCode::S => transform.translation += -forward * time.delta_seconds() * walking_speed,
KeyCode::D => transform.translation += right * time.delta_seconds() * walking_speed,
KeyCode::Space => transform.translation += Vec3::Y * time.delta_seconds() * walking_speed,
KeyCode::C => transform.translation -= Vec3::Y * time.delta_seconds() * walking_speed,
_ => (),
action_input.actions.retain(|action| {
match action {
Action::Forward => transform.translation += forward * time.delta_seconds() * walking_speed,
Action::Backward => transform.translation += -forward * time.delta_seconds() * walking_speed,
Action::Left => transform.translation += -right * time.delta_seconds() * walking_speed,
Action::Right => transform.translation += right * time.delta_seconds() * walking_speed,
Action::Up => transform.translation += Vec3::Y * time.delta_seconds() * walking_speed,
Action::Down => transform.translation -= Vec3::Y * time.delta_seconds() * walking_speed,
_ => return true,
}
}
false
});
}
}
fn camera_update_system(
mut query: Query<(&mut Transform, &FPSCamera)>
)