Skip to content
Snippets Groups Projects
Select Git revision
  • fc3c33e2b3f723fba9528c85d564435dfad213d5
  • master default protected
  • refactor
3 results

game_pad.rs

Blame
  • game_pad.rs 2.64 KiB
    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");
        }
    }