Created
September 21, 2020 04:22
-
-
Save AlexCMueller/90833bab9cc89fb25f4a6a681df21f3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::prelude::*; | |
use std::collections::HashSet; | |
pub struct InputPlugin; | |
impl Plugin for InputPlugin { | |
fn build(&self, app: &mut AppBuilder) { | |
app | |
.add_startup_system(connection.system()) | |
.add_system(connection.system()) | |
.add_system(gamepad_axis.system()) | |
.add_resource(Lobby::default()) | |
.add_resource(InputState::default()); | |
} | |
} | |
#[derive(Default)] | |
struct Lobby { | |
gamepad: HashSet<Gamepad>, | |
gamepad_event_reader: EventReader<GamepadEvent>, | |
} | |
#[derive(Default)] | |
pub struct InputState { | |
pub movement_vector: Vec2, | |
} | |
fn connection(mut lobby: ResMut<Lobby>, gamepad_event: Res<Events<GamepadEvent>>) { | |
for event in lobby.gamepad_event_reader.iter(&gamepad_event) { | |
match &event { | |
GamepadEvent(gamepad, GamepadEventType::Connected) => { | |
lobby.gamepad.insert(*gamepad); | |
println!("Connected {:?}", gamepad); | |
} | |
GamepadEvent(gamepad, GamepadEventType::Disconnected) => { | |
lobby.gamepad.remove(gamepad); | |
println!("Disconnected {:?}", gamepad); | |
} | |
} | |
} | |
} | |
fn gamepad_axis( | |
lobby: Res<Lobby>, | |
axes: Res<Axis<GamepadAxis>>, | |
mut input: ResMut<InputState>, | |
) { | |
for gamepad in lobby.gamepad.iter() { | |
if let Some(value) = axes.get(&GamepadAxis(*gamepad, GamepadAxisType::LeftStickX)) { | |
*input.movement_vector.x_mut() = value; | |
} | |
if let Some(value) = axes.get(&GamepadAxis(*gamepad, GamepadAxisType::LeftStickY)) { | |
*input.movement_vector.y_mut() = value; | |
} | |
} | |
if input.movement_vector.length() > 1.0 { | |
input.movement_vector = input.movement_vector.normalize() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::{ | |
prelude::*, | |
render::pass::ClearColor, | |
}; | |
mod input; | |
struct Player; | |
fn main() { | |
App::build() | |
.add_resource(ClearColor(Color::rgb(0.5, 0.5, 0.5))) | |
.add_resource(WindowDescriptor { | |
title: String::from("vshrs"), | |
resizable: false, | |
..Default::default() | |
}) | |
.add_default_plugins() | |
.add_plugin(input::InputPlugin) | |
.add_startup_system(setup.system()) | |
.add_system(move_player.system()) | |
.run(); | |
} | |
fn setup( | |
mut commands: Commands, | |
mut materials: ResMut<Assets<ColorMaterial>>, | |
) { | |
commands | |
.spawn(Camera2dComponents::default()) | |
.spawn(SpriteComponents { | |
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()), | |
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)), | |
sprite: Sprite::new(Vec2::new(25.0, 25.0)), | |
..Default::default() | |
}) | |
.with(Player); | |
} | |
fn move_player( | |
time: Res<Time>, | |
input: Res<input::InputState>, | |
_player: &Player, | |
mut transform: Mut<Transform>, | |
) { | |
let translation = transform.translation_mut(); | |
*translation.x_mut() += input.movement_vector.x() * 500.0 * time.delta_seconds; | |
*translation.y_mut() += input.movement_vector.y() * 500.0 * time.delta_seconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment