style: run cargo fmt

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