Skip to content
Snippets Groups Projects
Commit fc3c33e2 authored by Per Lindgren's avatar Per Lindgren
Browse files

refactor

parent 25dbc8c2
No related branches found
No related tags found
No related merge requests found
use bevy::prelude::*;
pub struct LevelUpEvent(Entity);
// fn player_level_up(mut ev_levelup: EventWriter<LevelUpEvent>, query: Query<(Entity, &PlayerXp)>) {
// for (entity, xp) in query.iter() {
// if xp.0 > 1000 {
// ev_levelup.send(LevelUpEvent(entity));
// }
// }
// }
// fn debug_levelups(mut ev_levelup: EventReader<LevelUpEvent>) {
// for ev in ev_levelup.iter() {
// eprintln!("Entity {:?} leveled up!", ev.0);
// }
// }
use bevy::{
core::FixedTimestep,
prelude::*,
render::{camera::Camera, texture::FilterMode},
};
use crate::events::*;
// Gamepad related
/// Simple resource to store the ID of the connected gamepad.
/// We need to know which gamepad to use for player input.
pub struct MyGamepad(Gamepad);
pub fn gamepad_connections(
mut commands: Commands,
my_gamepad: Option<Res<MyGamepad>>,
mut gamepad_evr: EventReader<GamepadEvent>,
) {
for GamepadEvent(id, kind) in gamepad_evr.iter() {
match kind {
GamepadEventType::Connected => {
println!("New gamepad connected with ID: {:?}", id);
// if we don't have any gamepad yet, use this one
if my_gamepad.is_none() {
commands.insert_resource(MyGamepad(*id));
}
}
GamepadEventType::Disconnected => {
println!("Lost gamepad connection with ID: {:?}", id);
// if it's the one we previously associated with the player,
// disassociate it:
if let Some(MyGamepad(old_id)) = my_gamepad.as_deref() {
if old_id == id {
commands.remove_resource::<MyGamepad>();
}
}
}
// other events are irrelevant
_ => {}
}
}
}
pub fn gamepad_input(
axes: Res<Axis<GamepadAxis>>,
buttons: Res<Input<GamepadButton>>,
my_gamepad: Option<Res<MyGamepad>>,
mut ev_levelup: EventWriter<LevelUpEvent>,
) {
let gamepad = if let Some(gp) = my_gamepad {
// a gamepad is connected, we have the id
gp.0
} else {
// no gamepad is connected
return;
};
// The joysticks are represented using a separate axis for X and Y
let axis_lx = GamepadAxis(gamepad, GamepadAxisType::LeftStickX);
let axis_ly = GamepadAxis(gamepad, GamepadAxisType::LeftStickY);
if let (Some(x), Some(y)) = (axes.get(axis_lx), axes.get(axis_ly)) {
// combine X and Y into one vector
let left_stick_pos = Vec2::new(x, y);
// implement a dead-zone to ignore small inputs
if left_stick_pos.length() > 0.1 {
// do something with the position of the left stick
}
}
let jump_button = GamepadButton(gamepad, GamepadButtonType::South);
let heal_button = GamepadButton(gamepad, GamepadButtonType::East);
if buttons.just_pressed(jump_button) {
// button pressed: make the player jump
println!("jump");
}
if buttons.pressed(heal_button) {
// button being held down: heal the player
println!("heal");
}
}
......@@ -7,6 +7,12 @@ use bevy::{
use bevy_ecs_tilemap::{prelude::*, MapQuery};
use std::f32::consts::PI;
mod game_pad;
use game_pad::*;
mod events;
use events::*;
const PLAYER_SHEET: &str = "character_zombie_sheet.png";
fn main() {
env_logger::Builder::from_default_env()
......@@ -168,28 +174,7 @@ impl Default for Player {
fn player_movement(
mut query: Query<(&mut Player, &mut Transform, &mut TextureAtlasSprite)>,
keyboard_input: Res<Input<KeyCode>>,
// mut gilrs_input: ResMut<Gamepad>,
) {
// while let Some(event) = gilrs_input.gilrs.next_event() {
// match event {
// Event {
// id,
// event: EventType::ButtonPressed(Button::South, _),
// ..
// } => {
// println!("Player {}: jump!", id)
// }
// Event {
// id,
// event: EventType::Disconnected,
// ..
// } => {
// println!("We lost player {}", id)
// }
// _ => (),
// };
// }
// println!("gilrs_input {}", gilrs_input.gilrs);
if let Ok((mut player, mut transform, mut sprite)) = query.single_mut() {
let (index, state, dir) = if player.player_jump {
// already in jump
......@@ -299,83 +284,3 @@ pub fn camera(
transform.translation.x = player.player_x * (1. - layer.settings.parallax_x) / MAP_SCALE;
}
}
// Gamepad related
/// Simple resource to store the ID of the connected gamepad.
/// We need to know which gamepad to use for player input.
struct MyGamepad(Gamepad);
fn gamepad_connections(
mut commands: Commands,
my_gamepad: Option<Res<MyGamepad>>,
mut gamepad_evr: EventReader<GamepadEvent>,
) {
for GamepadEvent(id, kind) in gamepad_evr.iter() {
match kind {
GamepadEventType::Connected => {
println!("New gamepad connected with ID: {:?}", id);
// if we don't have any gamepad yet, use this one
if my_gamepad.is_none() {
commands.insert_resource(MyGamepad(*id));
}
}
GamepadEventType::Disconnected => {
println!("Lost gamepad connection with ID: {:?}", id);
// if it's the one we previously associated with the player,
// disassociate it:
if let Some(MyGamepad(old_id)) = my_gamepad.as_deref() {
if old_id == id {
commands.remove_resource::<MyGamepad>();
}
}
}
// other events are irrelevant
_ => {}
}
}
}
fn gamepad_input(
axes: Res<Axis<GamepadAxis>>,
buttons: Res<Input<GamepadButton>>,
my_gamepad: Option<Res<MyGamepad>>,
) {
let gamepad = if let Some(gp) = my_gamepad {
// a gamepad is connected, we have the id
gp.0
} else {
// no gamepad is connected
return;
};
// The joysticks are represented using a separate axis for X and Y
let axis_lx = GamepadAxis(gamepad, GamepadAxisType::LeftStickX);
let axis_ly = GamepadAxis(gamepad, GamepadAxisType::LeftStickY);
if let (Some(x), Some(y)) = (axes.get(axis_lx), axes.get(axis_ly)) {
// combine X and Y into one vector
let left_stick_pos = Vec2::new(x, y);
// implement a dead-zone to ignore small inputs
if left_stick_pos.length() > 0.1 {
// do something with the position of the left stick
}
}
let jump_button = GamepadButton(gamepad, GamepadButtonType::South);
let heal_button = GamepadButton(gamepad, GamepadButtonType::East);
if buttons.just_pressed(jump_button) {
// button pressed: make the player jump
println!("jump");
}
if buttons.pressed(heal_button) {
// button being held down: heal the player
println!("heal");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment