♻️ refactor: extract VirtualGamepad

main
_ 2022-01-09 13:49:44 +00:00
parent e7854f9021
commit d570ee4cc8
2 changed files with 69 additions and 12 deletions

View File

@ -19,6 +19,7 @@ use opengl_rust::{
}; };
mod graphics; mod graphics;
mod virtual_gamepad;
use graphics::Graphics; use graphics::Graphics;
@ -178,12 +179,12 @@ async fn main () -> Result <()> {
game_state.reset_level (&level); game_state.reset_level (&level);
game_state.phys_tris = phys_tris; game_state.phys_tris = phys_tris;
let mut next_mpf_print = 60; let mut next_upf_print = 60;
let mut last_mpf_instant = Instant::now (); let mut last_upf_instant = Instant::now ();
'running: loop { 'running: loop {
let _frames_to_do = time_step.step (); let _frames_to_do = time_step.step ();
let mut player_wants_to_jump = false; let mut player_gamepad = virtual_gamepad::VirtualGamepad::default ();
for event in event_pump.poll_iter () { for event in event_pump.poll_iter () {
match event { match event {
@ -194,29 +195,60 @@ async fn main () -> Result <()> {
Event::KeyDown { keycode: Some (Keycode::R), .. } => { Event::KeyDown { keycode: Some (Keycode::R), .. } => {
game_state.reset_level (&level); game_state.reset_level (&level);
}, },
Event::KeyDown { keycode: Some (Keycode::Space), repeat: false, .. } => { Event::KeyDown { scancode: Some (Scancode::Space), repeat: false, .. } => {
debug! ("Space down"); player_gamepad.jump.pressed = true;
player_wants_to_jump = true; },
Event::KeyDown { scancode: Some (Scancode::Left), repeat: false, .. } => {
player_gamepad.d_left.pressed = true;
},
Event::KeyDown { scancode: Some (Scancode::Right), repeat: false, .. } => {
player_gamepad.d_right.pressed = true;
},
Event::KeyDown { scancode: Some (Scancode::Up), repeat: false, .. } => {
player_gamepad.d_up.pressed = true;
},
Event::KeyDown { scancode: Some (Scancode::Down), repeat: false, .. } => {
player_gamepad.d_down.pressed = true;
}, },
_ => (), _ => (),
} }
} }
let kb_state = event_pump.keyboard_state (); {
let kb_state = event_pump.keyboard_state ();
if kb_state.is_scancode_pressed (Scancode::Space) {
player_gamepad.jump.held = true;
}
if kb_state.is_scancode_pressed (Scancode::Left) {
player_gamepad.d_left.held = true;
}
if kb_state.is_scancode_pressed (Scancode::Right) {
player_gamepad.d_right.held = true;
}
if kb_state.is_scancode_pressed (Scancode::Up) {
player_gamepad.d_up.held = true;
}
if kb_state.is_scancode_pressed (Scancode::Down) {
player_gamepad.d_down.held = true;
}
}
let p_gp = player_gamepad;
let mut wanted_dir = Vec3::default (); let mut wanted_dir = Vec3::default ();
if kb_state.is_scancode_pressed (Scancode::Left) { if p_gp.d_left.any_press () {
wanted_dir.x -= 1.0; wanted_dir.x -= 1.0;
} }
if kb_state.is_scancode_pressed (Scancode::Right) { if p_gp.d_right.any_press () {
wanted_dir.x += 1.0; wanted_dir.x += 1.0;
} }
if kb_state.is_scancode_pressed (Scancode::Up) { if p_gp.d_up.any_press () {
wanted_dir.y += 1.0; wanted_dir.y += 1.0;
} }
if kb_state.is_scancode_pressed (Scancode::Down) { if p_gp.d_down.any_press () {
wanted_dir.y -= 1.0; wanted_dir.y -= 1.0;
} }
@ -267,7 +299,7 @@ async fn main () -> Result <()> {
// dbg! (game_state.player.vel); // dbg! (game_state.player.vel);
if player_wants_to_jump { if p_gp.jump.pressed {
if let Some (normal) = player_jump_vec.clone () { if let Some (normal) = player_jump_vec.clone () {
game_state.player.vel.z = 0.0; game_state.player.vel.z = 0.0;
game_state.player.vel += normal * player_jump_speed; game_state.player.vel += normal * player_jump_speed;

View File

@ -0,0 +1,25 @@
#[derive (Clone, Copy, Default)]
pub struct VirtualGamepad {
pub jump: VirtualButton,
pub d_left: VirtualButton,
pub d_right: VirtualButton,
pub d_up: VirtualButton,
pub d_down: VirtualButton,
}
#[derive (Clone, Copy, Default)]
pub struct VirtualButton {
/// True iff the button is down now
pub held: bool,
/// True if the button was pressed at any point since the last frame,
/// even if it's up now.
/// This is needed to make really fast clicks air-tight.
pub pressed: bool,
}
impl VirtualButton {
pub fn any_press (self) -> bool {
self.held || self.pressed
}
}