use wasm4fun_core::{
BUTTON_1, BUTTON_2, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_UP, GAMEPAD1, GAMEPAD2,
GAMEPAD3, GAMEPAD4,
};
pub struct GamePad(u8);
impl GamePad {
pub fn open(index: usize) -> &'static Self {
match index {
1 => unsafe { &*(GAMEPAD1 as *const Self) },
2 => unsafe { &*(GAMEPAD2 as *const Self) },
3 => unsafe { &*(GAMEPAD3 as *const Self) },
4 => unsafe { &*(GAMEPAD4 as *const Self) },
_ => panic!("Invalid gamepad"),
}
}
pub fn x(&self) -> bool {
self.0 & BUTTON_1 != 0
}
pub fn z(&self) -> bool {
self.0 & BUTTON_2 != 0
}
pub fn left(&self) -> bool {
self.0 & BUTTON_LEFT != 0
}
pub fn right(&self) -> bool {
self.0 & BUTTON_RIGHT != 0
}
pub fn up(&self) -> bool {
self.0 & BUTTON_UP != 0
}
pub fn down(&self) -> bool {
self.0 & BUTTON_DOWN != 0
}
}