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

9
Cargo.lock generated
View File

@@ -1902,6 +1902,7 @@ name = "l_system_plants"
version = "0.1.0"
dependencies = [
"bevy",
"serde",
]
[[package]]
@@ -2787,18 +2788,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.132"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9875c23cf305cd1fd7eb77234cbb705f21ea6a72c637a5c6db5fe4b8e7f008"
checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.132"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecc0db5cb2556c0e558887d9bbdcf6ac4471e83ff66cf696e5419024d1606276"
checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537"
dependencies = [
"proc-macro2",
"quote",

View File

@@ -7,3 +7,4 @@ edition = "2018"
[dependencies]
bevy = "0.5"
serde = "1.0.133"

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)>
)