docs: simplest example
This commit is contained in:
+79
-63
@@ -14,22 +14,19 @@ use bevy_controls::{
|
||||
Action, ActionInner, GameState, GameStateInner, InputsContainer, InputsContainerInner,
|
||||
},
|
||||
plugin::ControlsPlugin,
|
||||
resource::{Binding, ButtonCombination, Controls, InputType, Inputs, PlayerInputs},
|
||||
resource::{Binding, ButtonCombination, Controls, InputType, InputValue, Inputs, PlayerInputs},
|
||||
};
|
||||
use strum::IntoEnumIterator;
|
||||
use strum_macros::EnumIter;
|
||||
const FIRA_CODE_PATH: &str = "font/FiraCode/FiraCode-VariableFont_wght.ttf";
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, EnumIter, Clone, Copy)]
|
||||
#[derive(PartialEq, Eq, Hash, EnumIter, Clone, Copy, Debug)]
|
||||
enum MyAction {
|
||||
MoveForward,
|
||||
MoveBack,
|
||||
MoveLeft,
|
||||
MoveRight,
|
||||
Jump,
|
||||
Interact,
|
||||
Shoot,
|
||||
Scope,
|
||||
}
|
||||
|
||||
impl Action for MyAction {}
|
||||
@@ -46,7 +43,7 @@ impl GameState for MyGameState {}
|
||||
|
||||
impl GameStateInner for MyGameState {}
|
||||
|
||||
#[derive(Resource, Default, Clone)]
|
||||
#[derive(Resource, Default, Clone, Debug)]
|
||||
struct MyInputsContainer {
|
||||
// When the game does not provide multiplayer, one field is enough
|
||||
player_inputs: PlayerInputs<MyAction>,
|
||||
@@ -78,58 +75,49 @@ fn main() {
|
||||
ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(),
|
||||
))
|
||||
.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.push(
|
||||
controls.force_push(
|
||||
MyAction::MoveLeft,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::MoveBack,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::S))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::MoveForward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::W))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::MoveRight,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::D))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Jump,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::Space))),
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct TextBlock;
|
||||
|
||||
const TEXT_SIZE: f32 = 30.;
|
||||
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
|
||||
commands.spawn((
|
||||
TextBundle::from_sections([
|
||||
TextSection::new(
|
||||
"Input: ",
|
||||
"Inputs: A S W D Space",
|
||||
TextStyle {
|
||||
font: asset_server.load(FIRA_CODE_PATH),
|
||||
font_size: 60.0,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"A ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"S ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"W ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"D ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
@@ -137,35 +125,42 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
"\nAction: ",
|
||||
TextStyle {
|
||||
font: asset_server.load(FIRA_CODE_PATH),
|
||||
font_size: 60.0,
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveLeft",
|
||||
"MoveLeft ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveBack",
|
||||
"MoveBack ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveForward",
|
||||
"MoveForward ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveRight",
|
||||
"MoveRight ",
|
||||
TextStyle {
|
||||
font_size: 60.0,
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Jump",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
@@ -175,31 +170,52 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
}
|
||||
|
||||
// text sections indexes
|
||||
const A_INDEX: usize = 2;
|
||||
const S_INDEX: usize = 3;
|
||||
const W_INDEX: usize = 4;
|
||||
const D_INDEX: usize = 5;
|
||||
|
||||
const MOVE_LEFT_INDEX: usize = 6;
|
||||
const MOVE_BACK_INDEX: usize = 7;
|
||||
const MOVE_FORWARD_INDEX: usize = 8;
|
||||
const MOVE_RIGHT_INDEX: usize = 9;
|
||||
const MOVE_LEFT_INDEX: usize = 2;
|
||||
const MOVE_BACK_INDEX: usize = 3;
|
||||
const MOVE_FORWARD_INDEX: usize = 4;
|
||||
const MOVE_RIGHT_INDEX: usize = 5;
|
||||
const JUMP_INDEX: usize = 6;
|
||||
|
||||
fn text_update_system(
|
||||
mut query: Query<&mut Text, With<TextBlock>>,
|
||||
inputs_container: MyInputsContainer,
|
||||
inputs_container: Res<MyInputsContainer>,
|
||||
) {
|
||||
for mut text in &mut query {
|
||||
for text in query.iter_mut() {
|
||||
|
||||
let input_value = inputs_container.me().expect("This is bad").get(MyAction::MoveLeft);
|
||||
if input_value.is_boolean() && input_value.to_boolean() {
|
||||
text.sections[MOVE_LEFT_INDEX].style.color = Color::GOLD;
|
||||
} else {
|
||||
text.sections[MOVE_LEFT_INDEX].style.color = Color::GOLD;
|
||||
}
|
||||
let player_inputs = inputs_container.me().expect("This is bad");
|
||||
let mut text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveLeft),
|
||||
MOVE_LEFT_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveBack),
|
||||
MOVE_BACK_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveForward),
|
||||
MOVE_FORWARD_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveRight),
|
||||
MOVE_RIGHT_INDEX);
|
||||
_ = update_text(
|
||||
text,
|
||||
InputValue::Boolean(player_inputs.get_just_pressed(MyAction::Jump).unwrap_or(false)),
|
||||
JUMP_INDEX);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
text.sections[index].style.color = Color::WHITE;
|
||||
}
|
||||
|
||||
text
|
||||
}
|
||||
|
||||
// fn text_update_system(
|
||||
// diagnostics: Res<DiagnosticsStore>,
|
||||
// mut query: Query<&mut Text, With<FpsText>>,
|
||||
|
||||
Reference in New Issue
Block a user