Skip to content

Commit 6cde42e

Browse files
committed
feat: enhance joystick support for player controls and menu interactions
1 parent 790de1a commit 6cde42e

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
@@ -181,7 +181,9 @@ namespace gameplay {
181181
moveDir -= joystickMoveDir;
182182
}
183183

184-
movement->isSprinting = keyboard.isPressed(GLFW_KEY_LEFT_SHIFT) && !movement->isSliding;
184+
bool sprintPressed =
185+
keyboard.isPressed(GLFW_KEY_LEFT_SHIFT) || joystick.isPressed(GLFW_GAMEPAD_BUTTON_LEFT_BUMPER);
186+
movement->isSprinting = sprintPressed && !movement->isSliding;
185187

186188
float sprintSpeed = movement->walkSpeed * movement->sprintMultiplier;
187189
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
@@ -131,10 +131,11 @@ class Playstate : public our::State {
131131
void onDraw(double deltaTime) override {
132132
float dt = static_cast<float>(deltaTime);
133133
our::Keyboard& keyboard = getApp()->getKeyboard();
134+
our::Joystick& joystick = getApp()->getJoystick();
134135

135136
if (overlay.handleActiveFrame(deltaTime)) return;
136137

137-
if (keyboard.justPressed(GLFW_KEY_ESCAPE)) {
138+
if (keyboard.justPressed(GLFW_KEY_ESCAPE) || joystick.justPressed(GLFW_GAMEPAD_BUTTON_START)) {
138139
overlay.openPause();
139140
overlay.renderCurrent(deltaTime);
140141
return;

0 commit comments

Comments
 (0)