Skip to content

Commit c6b6a2b

Browse files
committed
feat: enhance joystick support for player controls and menu interactions
1 parent 566b0ce commit c6b6a2b

5 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/game/systems/player-movement-system.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ namespace gameplay {
210210
moveDir -= joystickMoveDir;
211211
}
212212

213-
movement->isSprinting = keyboard.isPressed(GLFW_KEY_LEFT_SHIFT) && !movement->isSliding;
213+
bool sprintPressed =
214+
keyboard.isPressed(GLFW_KEY_LEFT_SHIFT) || joystick.isPressed(GLFW_GAMEPAD_BUTTON_LEFT_BUMPER);
215+
movement->isSprinting = sprintPressed && !movement->isSliding;
214216

215217
float sprintSpeed = movement->walkSpeed * movement->sprintMultiplier;
216218
float slideStartSpeed = sprintSpeed * movement->slideSpeedMultiplier;

src/game/systems/weapon-switcher-system.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ namespace gameplay {
5252
(playerComp->currentActiveWeaponIndex - 1 + weapons.size()) % weapons.size();
5353
}
5454

55+
our::Joystick& joystick = app->getJoystick();
56+
if (joystick.justPressed(GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER)) {
57+
playerComp->currentActiveWeaponIndex = (playerComp->currentActiveWeaponIndex + 1) % weapons.size();
58+
}
59+
5560
weapons[currentIndex]->isActive = false;
5661
weapons[playerComp->currentActiveWeaponIndex]->isActive = true;
5762

src/game/ui/play-overlay.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,19 @@ namespace gameplay {
300300
if (!app) return false;
301301

302302
our::Keyboard& keyboard = app->getKeyboard();
303+
our::Joystick& joystick = app->getJoystick();
303304
if (screen == Screen::Pause) {
304-
if (keyboard.justPressed(GLFW_KEY_ESCAPE)) {
305+
if (keyboard.justPressed(GLFW_KEY_ESCAPE) || joystick.justPressed(GLFW_GAMEPAD_BUTTON_START) ||
306+
joystick.justPressed(GLFW_GAMEPAD_BUTTON_A)) {
305307
closePause();
306308
return true;
307309
}
308310

311+
if (joystick.justPressed(GLFW_GAMEPAD_BUTTON_B)) {
312+
app->changeState("menu");
313+
return true;
314+
}
315+
309316
glm::ivec2 size = app->getFrameBufferSize();
310317
menu_ui::PixelRect menuRect = menu_ui::getMenuRect(size);
311318
glm::vec2 mousePosition = menu_ui::getMousePositionInTarget(app, size);
@@ -317,7 +324,15 @@ namespace gameplay {
317324
}
318325

319326
if (screen == Screen::GameOver) {
320-
if (keyboard.justPressed(GLFW_KEY_ESCAPE)) app->changeState("menu");
327+
if (keyboard.justPressed(GLFW_KEY_ESCAPE) || joystick.justPressed(GLFW_GAMEPAD_BUTTON_B)) {
328+
app->changeState("menu");
329+
return true;
330+
}
331+
332+
if (joystick.justPressed(GLFW_GAMEPAD_BUTTON_A)) {
333+
app->changeState("loading-play");
334+
return true;
335+
}
321336

322337
glm::ivec2 size = app->getFrameBufferSize();
323338
menu_ui::PixelRect menuRect = menu_ui::getMenuRect(size);

src/states/menu-state.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ class Menustate : public our::State {
6767

6868
void onDraw(double deltaTime) override {
6969
auto& keyboard = getApp()->getKeyboard();
70-
if (keyboard.justPressed(GLFW_KEY_SPACE))
70+
auto& joystick = getApp()->getJoystick();
71+
if (keyboard.justPressed(GLFW_KEY_SPACE) || joystick.justPressed(GLFW_GAMEPAD_BUTTON_A))
7172
getApp()->changeState("loading-play");
72-
else if (keyboard.justPressed(GLFW_KEY_ESCAPE))
73+
else if (keyboard.justPressed(GLFW_KEY_ESCAPE) || joystick.justPressed(GLFW_GAMEPAD_BUTTON_B))
7374
getApp()->close();
7475

7576
glm::ivec2 size = getApp()->getFrameBufferSize();

src/states/play-state.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class Playstate : public our::State {
149149
void onDraw(double deltaTime) override {
150150
float dt = static_cast<float>(deltaTime);
151151
our::Keyboard& keyboard = getApp()->getKeyboard();
152+
our::Joystick& joystick = getApp()->getJoystick();
152153

153154
if (waitFirstFrame) {
154155
waitFirstFrame = false;
@@ -158,7 +159,7 @@ class Playstate : public our::State {
158159

159160
if (overlay.handleActiveFrame(deltaTime)) return;
160161

161-
if (keyboard.justPressed(GLFW_KEY_ESCAPE)) {
162+
if (keyboard.justPressed(GLFW_KEY_ESCAPE) || joystick.justPressed(GLFW_GAMEPAD_BUTTON_START)) {
162163
overlay.openPause();
163164
overlay.renderCurrent(deltaTime);
164165
return;

0 commit comments

Comments
 (0)