From b7f343b524ed5d9eefd7beaa9ca38839463ca184 Mon Sep 17 00:00:00 2001 From: Julian Date: Mon, 3 Jan 2022 18:39:06 +0100 Subject: [PATCH] Actionsystem --- Cargo.lock | 9 ++++--- Cargo.toml | 1 + src/main.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 347c4a3..e7cd3b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 2667b0d..ccba9db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" [dependencies] bevy = "0.5" +serde = "1.0.133" diff --git a/src/main.rs b/src/main.rs index 3057b7c..8d50aa9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, +} + +#[derive(Copy, Clone)] +enum Action +{ + Forward, + Backward, + Left, + Right, + Up, + Down +} + +struct Actions { + actions: VecDeque, +} 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>, + binding_input: Res, + mut actions: ResMut, +) +{ + 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