improve graphics and add camera controls
This commit is contained in:
parent
82a0fbdb50
commit
87fcc2b817
12 changed files with 367 additions and 93 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use bevy::{platform::collections::HashMap, prelude::*};
|
||||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{InputMap, InputMethod};
|
||||
|
|
@ -7,21 +7,37 @@ use crate::{InputMap, InputMethod};
|
|||
pub enum InputAction {
|
||||
ToggleDebugUI,
|
||||
TogglePause,
|
||||
CameraZoom,
|
||||
CameraYaw,
|
||||
CameraPanX,
|
||||
CameraPanY,
|
||||
CameraPanEnable,
|
||||
CameraOrbitEnable,
|
||||
}
|
||||
|
||||
impl Default for InputMap {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
map: HashMap::from_iter(vec![
|
||||
(
|
||||
InputMethod::KeyCode(KeyCode::F1),
|
||||
InputAction::ToggleDebugUI,
|
||||
),
|
||||
(
|
||||
InputMethod::KeyCode(KeyCode::Escape),
|
||||
InputAction::TogglePause,
|
||||
),
|
||||
]),
|
||||
}
|
||||
Self::from_iter(vec![
|
||||
(
|
||||
InputMethod::KeyCode(KeyCode::F1),
|
||||
InputAction::ToggleDebugUI,
|
||||
),
|
||||
(
|
||||
InputMethod::KeyCode(KeyCode::Escape),
|
||||
InputAction::TogglePause,
|
||||
),
|
||||
(InputMethod::MouseMovementX, InputAction::CameraYaw),
|
||||
(InputMethod::MouseScroll, InputAction::CameraZoom),
|
||||
(
|
||||
InputMethod::MouseButton(MouseButton::Middle),
|
||||
InputAction::CameraOrbitEnable,
|
||||
),
|
||||
(InputMethod::MouseMovementX, InputAction::CameraPanX),
|
||||
(InputMethod::MouseMovementY, InputAction::CameraPanY),
|
||||
(
|
||||
InputMethod::MouseButton(MouseButton::Left),
|
||||
InputAction::CameraPanEnable,
|
||||
),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ pub mod conditions;
|
|||
|
||||
use std::{fs, marker::PhantomData};
|
||||
|
||||
use bevy::{input::mouse::AccumulatedMouseMotion, platform::collections::HashMap, prelude::*};
|
||||
use bevy::{
|
||||
input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll},
|
||||
platform::collections::HashMap,
|
||||
prelude::*,
|
||||
};
|
||||
use ron::ser::PrettyConfig;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use state::GameSystemSet;
|
||||
|
|
@ -32,18 +36,31 @@ impl Plugin for InputPlugin {
|
|||
|
||||
#[derive(Hash, Clone, Copy, Debug, Reflect, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum InputMethod {
|
||||
Unbound,
|
||||
MouseButton(MouseButton),
|
||||
KeyCode(KeyCode),
|
||||
MouseScroll,
|
||||
MouseMovementX,
|
||||
MouseMovementY,
|
||||
}
|
||||
|
||||
#[derive(Resource, Serialize, Deserialize)]
|
||||
pub struct InputMap {
|
||||
pub map: HashMap<InputMethod, InputAction>,
|
||||
pub map: HashMap<InputMethod, Vec<InputAction>>,
|
||||
}
|
||||
|
||||
impl InputMap {
|
||||
pub fn from_iter<I>(iter: I) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = (InputMethod, InputAction)>,
|
||||
{
|
||||
let mut map: HashMap<InputMethod, Vec<InputAction>> = HashMap::new();
|
||||
for (method, action) in iter {
|
||||
map.entry(method).or_insert_with(Vec::new).push(action);
|
||||
}
|
||||
Self { map }
|
||||
}
|
||||
|
||||
fn save_to_file(&self, path: &str) -> Result<()> {
|
||||
let json = ron::ser::to_string_pretty(self, PrettyConfig::default())?;
|
||||
fs::write(path, json)?;
|
||||
|
|
@ -122,6 +139,7 @@ fn update_inputs_frame(
|
|||
input_map: Res<InputMap>,
|
||||
mouse_movement: Res<AccumulatedMouseMotion>,
|
||||
mouse_buttons: Res<ButtonInput<MouseButton>>,
|
||||
mouse_scroll: Res<AccumulatedMouseScroll>,
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
fn set_value(action: &InputAction, value: f32, map: &mut HashMap<InputAction, InputState>) {
|
||||
|
|
@ -143,37 +161,49 @@ fn update_inputs_frame(
|
|||
for input in frame_inputs.map.values_mut() {
|
||||
input.changed = false;
|
||||
}
|
||||
for (method, action) in input_map.map.iter() {
|
||||
match method {
|
||||
InputMethod::MouseButton(mouse_button) => {
|
||||
let value = if mouse_buttons.pressed(*mouse_button) {
|
||||
1.0
|
||||
} else {
|
||||
-1.0
|
||||
};
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
set_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::KeyCode(key_code) => {
|
||||
let value = if keyboard.pressed(*key_code) {
|
||||
1.0
|
||||
} else {
|
||||
-1.0
|
||||
};
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
set_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::MouseMovementX => {
|
||||
let value = mouse_movement.delta.x;
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
// want to get accumulated mouse movement in here if needed
|
||||
add_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::MouseMovementY => {
|
||||
let value = mouse_movement.delta.y;
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
// want to get accumulated mouse movement in here if needed
|
||||
add_value(action, value, &mut fixed_inputs.map);
|
||||
for (method, actions) in input_map.map.iter() {
|
||||
for action in actions.iter() {
|
||||
match method {
|
||||
InputMethod::Unbound => {
|
||||
set_value(action, 0., &mut frame_inputs.map);
|
||||
set_value(action, 0., &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::MouseButton(mouse_button) => {
|
||||
let value = if mouse_buttons.pressed(*mouse_button) {
|
||||
1.0
|
||||
} else {
|
||||
-1.0
|
||||
};
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
set_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::KeyCode(key_code) => {
|
||||
let value = if keyboard.pressed(*key_code) {
|
||||
1.0
|
||||
} else {
|
||||
-1.0
|
||||
};
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
set_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::MouseScroll => {
|
||||
let value = mouse_scroll.delta.y;
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
// want to get accumulated mouse movement in here if needed
|
||||
add_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::MouseMovementX => {
|
||||
let value = mouse_movement.delta.x;
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
// want to get accumulated mouse movement in here if needed
|
||||
add_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
InputMethod::MouseMovementY => {
|
||||
let value = mouse_movement.delta.y;
|
||||
set_value(action, value, &mut frame_inputs.map);
|
||||
// want to get accumulated mouse movement in here if needed
|
||||
add_value(action, value, &mut fixed_inputs.map);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -199,8 +229,15 @@ fn save_input_map(input_map: Res<InputMap>) {
|
|||
|
||||
fn setup_input_map(mut commands: Commands) {
|
||||
let input_map = match InputMap::load_from_file(INPUT_MAP_PATH) {
|
||||
Ok(map) => {
|
||||
Ok(mut map) => {
|
||||
info!("Loaded input map");
|
||||
|
||||
for (key, value) in InputMap::default().map {
|
||||
if !map.map.values().any(|v| *v == value) {
|
||||
info!("missing action {:?}, adding default", value);
|
||||
map.map.insert(key, value);
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
Err(e) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue