style: run cargo fmt
This commit is contained in:
@@ -4,32 +4,29 @@
|
||||
//!
|
||||
//! Yeah I know, now it's a bit ugly
|
||||
|
||||
use bevy::{
|
||||
ecs::schedule::States,
|
||||
prelude::*,
|
||||
};
|
||||
use bevy::{ecs::schedule::States, prelude::*};
|
||||
use bevy_controls::{
|
||||
contract::{
|
||||
Action, ActionInner, GameState, GameStateInner, InputsContainer, InputsContainerInner,
|
||||
},
|
||||
plugin::ControlsPlugin,
|
||||
resource::{Binding, ButtonCombination, Controls, InputType, InputValue, PlayerInputs},
|
||||
contract::{
|
||||
Action, ActionInner, GameState, GameStateInner, InputsContainer, InputsContainerInner,
|
||||
},
|
||||
plugin::ControlsPlugin,
|
||||
resource::{Binding, ButtonCombination, Controls, InputType, InputValue, PlayerInputs},
|
||||
};
|
||||
use strum_macros::EnumIter;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, EnumIter, Clone, Copy, Debug, bevy_controls_derive::Action)]
|
||||
enum MyAction {
|
||||
Forward,
|
||||
Back,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Forward,
|
||||
Back,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
}
|
||||
|
||||
#[derive(States, PartialEq, Eq, Clone, Hash, Debug, Default)]
|
||||
enum MyGameState {
|
||||
#[default]
|
||||
InGame,
|
||||
#[default]
|
||||
InGame,
|
||||
}
|
||||
|
||||
impl GameState for MyGameState {}
|
||||
@@ -38,83 +35,85 @@ impl GameStateInner for MyGameState {}
|
||||
|
||||
#[derive(Resource, Default, Clone, Debug)]
|
||||
struct MyInputsContainer {
|
||||
// When the game does not provide multiplayer, one field is enough
|
||||
player_inputs: PlayerInputs<MyAction>,
|
||||
// When the game does not provide multiplayer, one field is enough
|
||||
player_inputs: PlayerInputs<MyAction>,
|
||||
}
|
||||
|
||||
impl InputsContainer<MyAction> for MyInputsContainer {}
|
||||
|
||||
impl InputsContainerInner<MyAction> for MyInputsContainer {
|
||||
fn iter_inputs<'a>(&'a self) -> Box<dyn Iterator<Item = &'a PlayerInputs<MyAction>> + 'a> {
|
||||
todo!()
|
||||
}
|
||||
fn iter_inputs<'a>(&'a self) -> Box<dyn Iterator<Item = &'a PlayerInputs<MyAction>> + 'a> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn me<'a>(&'a self) -> Option<&'a PlayerInputs<MyAction>> {
|
||||
Some(& self.player_inputs)
|
||||
}
|
||||
fn me<'a>(&'a self) -> Option<&'a PlayerInputs<MyAction>> {
|
||||
Some(&self.player_inputs)
|
||||
}
|
||||
|
||||
fn me_mut<'a>(&'a mut self) -> Option<&'a mut PlayerInputs<MyAction>> {
|
||||
Some(&mut self.player_inputs)
|
||||
}
|
||||
fn me_mut<'a>(&'a mut self) -> Option<&'a mut PlayerInputs<MyAction>> {
|
||||
Some(&mut self.player_inputs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Deref, DerefMut)]
|
||||
struct JumpsCount(i32);
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins,
|
||||
ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(),
|
||||
))
|
||||
.init_resource::<JumpsCount>()
|
||||
.add_systems(Startup, (add_bindings, setup))
|
||||
.add_systems(Update, text_update_system)
|
||||
.run();
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins,
|
||||
ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(),
|
||||
))
|
||||
.init_resource::<JumpsCount>()
|
||||
.add_systems(Startup, (add_bindings, setup))
|
||||
.add_systems(Update, text_update_system)
|
||||
.run();
|
||||
}
|
||||
|
||||
// TODO: default bingings...
|
||||
fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) {
|
||||
controls.force_push(
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::H))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::H))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Back,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::S))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Back,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::J))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Back,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::S))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Back,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::J))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Forward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::W))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Forward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::K))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Forward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::W))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Forward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::K))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Right,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::D))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Right,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::L))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Right,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::D))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Right,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::L))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Up,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::Space))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Up,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(
|
||||
KeyCode::Space,
|
||||
))),
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
@@ -123,55 +122,55 @@ struct TextBlock;
|
||||
const TEXT_SIZE: f32 = 30.;
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
|
||||
commands.spawn((
|
||||
TextBundle::from_sections([
|
||||
TextSection::new(
|
||||
"\nAction: ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Left (H/A) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Back (J/S) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Forward (K/W) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Right (L/D) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Up (Space)",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
]),
|
||||
TextBlock,
|
||||
));
|
||||
commands.spawn((
|
||||
TextBundle::from_sections([
|
||||
TextSection::new(
|
||||
"\nAction: ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Left (H/A) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Back (J/S) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Forward (K/W) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Right (L/D) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Up (Space)",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
]),
|
||||
TextBlock,
|
||||
));
|
||||
}
|
||||
|
||||
// text sections indexes
|
||||
@@ -182,37 +181,31 @@ const MOVE_RIGHT_INDEX: usize = 4;
|
||||
const JUMP_INDEX: usize = 5;
|
||||
|
||||
fn text_update_system(
|
||||
mut query: Query<&mut Text, With<TextBlock>>,
|
||||
inputs_container: Res<MyInputsContainer>,
|
||||
mut jumps_count: ResMut<JumpsCount>,
|
||||
mut query: Query<&mut Text, With<TextBlock>>,
|
||||
inputs_container: Res<MyInputsContainer>,
|
||||
mut jumps_count: ResMut<JumpsCount>,
|
||||
) {
|
||||
for text in query.iter_mut() {
|
||||
|
||||
let player_inputs = inputs_container.me().expect("This is bad");
|
||||
let mut text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::Left),
|
||||
MOVE_LEFT_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::Back),
|
||||
MOVE_BACK_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::Forward),
|
||||
MOVE_FORWARD_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::Right),
|
||||
MOVE_RIGHT_INDEX);
|
||||
if player_inputs.get_just_pressed(MyAction::Up).unwrap_or(false) {
|
||||
**jumps_count += 1;
|
||||
text.sections[JUMP_INDEX].value = format!("Up (Space) - {}", **jumps_count);
|
||||
}
|
||||
for text in query.iter_mut() {
|
||||
let player_inputs = inputs_container.me().expect("This is bad");
|
||||
let mut text = update_text(text, player_inputs.get(MyAction::Left), MOVE_LEFT_INDEX);
|
||||
text = update_text(text, player_inputs.get(MyAction::Back), MOVE_BACK_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::Forward),
|
||||
MOVE_FORWARD_INDEX,
|
||||
);
|
||||
text = update_text(text, player_inputs.get(MyAction::Right), MOVE_RIGHT_INDEX);
|
||||
if player_inputs
|
||||
.get_just_pressed(MyAction::Up)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
**jumps_count += 1;
|
||||
text.sections[JUMP_INDEX].value = format!("Up (Space) - {}", **jumps_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_text(mut text: Mut<Text>, input_value: InputValue, index: usize) -> Mut<Text>{
|
||||
fn update_text(mut text: Mut<Text>, input_value: InputValue, index: usize) -> Mut<Text> {
|
||||
if input_value.is_boolean() && input_value.to_boolean() {
|
||||
text.sections[index].style.color = Color::GOLD;
|
||||
} else {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn;
|
||||
use quote::quote;
|
||||
use bevy_controls::contract::{Action, ActionInner};
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn;
|
||||
|
||||
#[proc_macro_derive(Action)]
|
||||
pub fn action(input: TokenStream) -> TokenStream {
|
||||
|
||||
Reference in New Issue
Block a user