diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000000000000000000000000000000000000..f65b33b164e5a58ffdf1aedb73858881a61c6a9c --- /dev/null +++ b/src/events.rs @@ -0,0 +1,17 @@ +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); +// } +// } diff --git a/src/game_pad.rs b/src/game_pad.rs new file mode 100644 index 0000000000000000000000000000000000000000..bbbc95ce4a09f19bb0e972a695ba8d686b833fc2 --- /dev/null +++ b/src/game_pad.rs @@ -0,0 +1,88 @@ +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"); + } +} diff --git a/src/main.rs b/src/main.rs index 2fccf00a85dd55c65cbcce9d9bc6b1faa4ee457f..c0a201a5bde7e7e61b0a3af8efe1bcce58cb5f86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); - } -}