mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-07 03:12:36 +01:00
Extract game input state handling from IngameState
Prevents input getting "stuck" when input is removed in the pause menu
This commit is contained in:
parent
40f18dabb0
commit
f0e0e6e747
@ -22,6 +22,9 @@ set(RWGAME_SOURCES
|
|||||||
MenuSystem.hpp
|
MenuSystem.hpp
|
||||||
MenuSystem.cpp
|
MenuSystem.cpp
|
||||||
|
|
||||||
|
GameInput.hpp
|
||||||
|
GameInput.cpp
|
||||||
|
|
||||||
states/LoadingState.hpp
|
states/LoadingState.hpp
|
||||||
states/LoadingState.cpp
|
states/LoadingState.cpp
|
||||||
states/IngameState.hpp
|
states/IngameState.hpp
|
||||||
|
80
rwgame/GameInput.cpp
Normal file
80
rwgame/GameInput.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "GameInput.hpp"
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
// Hardcoded Controls SDLK_* -> GameInputState::Control
|
||||||
|
const std::unordered_multimap<int, GameInputState::Control> kDefaultControls = {
|
||||||
|
/* On Foot */
|
||||||
|
{SDLK_LCTRL, GameInputState::FireWeapon},
|
||||||
|
{SDLK_KP_0, GameInputState::FireWeapon},
|
||||||
|
{SDLK_KP_ENTER, GameInputState::NextWeapon},
|
||||||
|
{SDLK_KP_PERIOD, GameInputState::LastWeapon},
|
||||||
|
{SDLK_w, GameInputState::GoForward},
|
||||||
|
{SDLK_UP, GameInputState::GoForward},
|
||||||
|
{SDLK_s, GameInputState::GoBackwards},
|
||||||
|
{SDLK_DOWN, GameInputState::GoBackwards},
|
||||||
|
{SDLK_a, GameInputState::GoLeft},
|
||||||
|
{SDLK_LEFT, GameInputState::GoLeft},
|
||||||
|
{SDLK_d, GameInputState::GoRight},
|
||||||
|
{SDLK_RIGHT, GameInputState::GoRight},
|
||||||
|
{SDLK_PAGEUP, GameInputState::ZoomIn},
|
||||||
|
{SDLK_z, GameInputState::ZoomIn},
|
||||||
|
{SDLK_PAGEDOWN, GameInputState::ZoomOut},
|
||||||
|
{SDLK_x, GameInputState::ZoomOut},
|
||||||
|
{SDLK_f, GameInputState::EnterExitVehicle},
|
||||||
|
{SDLK_RETURN, GameInputState::EnterExitVehicle},
|
||||||
|
{SDLK_c, GameInputState::ChangeCamera},
|
||||||
|
{SDLK_HOME, GameInputState::ChangeCamera},
|
||||||
|
{SDLK_RCTRL, GameInputState::Jump},
|
||||||
|
{SDLK_SPACE, GameInputState::Jump},
|
||||||
|
{SDLK_LSHIFT, GameInputState::Sprint},
|
||||||
|
{SDLK_RSHIFT, GameInputState::Sprint},
|
||||||
|
{SDLK_LALT, GameInputState::Walk},
|
||||||
|
{SDLK_DELETE, GameInputState::AimWeapon},
|
||||||
|
{SDLK_CAPSLOCK, GameInputState::LookBehind},
|
||||||
|
|
||||||
|
/* In Vehicle */
|
||||||
|
{SDLK_LCTRL, GameInputState::VehicleFireWeapon},
|
||||||
|
{SDLK_a, GameInputState::VehicleLeft},
|
||||||
|
{SDLK_LEFT, GameInputState::VehicleLeft},
|
||||||
|
{SDLK_d, GameInputState::VehicleRight},
|
||||||
|
{SDLK_RIGHT, GameInputState::VehicleRight},
|
||||||
|
{SDLK_w, GameInputState::VehicleAccelerate},
|
||||||
|
{SDLK_UP, GameInputState::VehicleAccelerate},
|
||||||
|
{SDLK_d, GameInputState::VehicleBrake},
|
||||||
|
{SDLK_DOWN, GameInputState::VehicleBrake},
|
||||||
|
{SDLK_INSERT, GameInputState::ChangeRadio},
|
||||||
|
{SDLK_r, GameInputState::ChangeRadio},
|
||||||
|
{SDLK_LSHIFT, GameInputState::Horn},
|
||||||
|
{SDLK_RSHIFT, GameInputState::Horn},
|
||||||
|
{SDLK_KP_PLUS, GameInputState::Submission},
|
||||||
|
{SDLK_CAPSLOCK, GameInputState::Submission},
|
||||||
|
{SDLK_RCTRL, GameInputState::Handbrake},
|
||||||
|
{SDLK_SPACE, GameInputState::Handbrake},
|
||||||
|
{SDLK_KP_9, GameInputState::VehicleAimUp},
|
||||||
|
{SDLK_KP_2, GameInputState::VehicleAimDown},
|
||||||
|
{SDLK_KP_4, GameInputState::VehicleAimLeft},
|
||||||
|
{SDLK_KP_6, GameInputState::VehicleAimRight},
|
||||||
|
{SDLK_KP_9, GameInputState::VehicleDown},
|
||||||
|
{SDLK_KP_2, GameInputState::VehicleUp},
|
||||||
|
{SDLK_KP_1, GameInputState::LookLeft},
|
||||||
|
{SDLK_q, GameInputState::LookLeft},
|
||||||
|
{SDLK_KP_2, GameInputState::LookRight},
|
||||||
|
{SDLK_e, GameInputState::LookRight},
|
||||||
|
};
|
||||||
|
|
||||||
|
void GameInput::updateGameInputState(GameInputState *state,
|
||||||
|
const SDL_Event &event) {
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
case SDL_KEYUP: {
|
||||||
|
auto sym = event.key.keysym.sym;
|
||||||
|
auto level = event.type == SDL_KEYDOWN ? 1.f : 0.f;
|
||||||
|
auto& levels = state->levels;
|
||||||
|
|
||||||
|
auto range = kDefaultControls.equal_range(sym);
|
||||||
|
for (auto it = range.first; it != range.second; ++it) {
|
||||||
|
levels[it->second] = level;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
}
|
10
rwgame/GameInput.hpp
Normal file
10
rwgame/GameInput.hpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef RWGAME_GAMEINPUT_HPP
|
||||||
|
#define RWGAME_GAMEINPUT_HPP
|
||||||
|
#include "engine/GameState.hpp"
|
||||||
|
#include "SDL2/SDL.h"
|
||||||
|
|
||||||
|
namespace GameInput {
|
||||||
|
void updateGameInputState(GameInputState* state, const SDL_Event& event);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,5 +1,6 @@
|
|||||||
#include "RWGame.hpp"
|
#include "RWGame.hpp"
|
||||||
#include "DrawUI.hpp"
|
#include "DrawUI.hpp"
|
||||||
|
#include "GameInput.hpp"
|
||||||
#include "State.hpp"
|
#include "State.hpp"
|
||||||
#include "states/BenchmarkState.hpp"
|
#include "states/BenchmarkState.hpp"
|
||||||
#include "states/IngameState.hpp"
|
#include "states/IngameState.hpp"
|
||||||
@ -394,6 +395,8 @@ int RWGame::run() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GameInput::updateGameInputState(&getState()->input[0], event);
|
||||||
|
|
||||||
RW_PROFILE_BEGIN("State");
|
RW_PROFILE_BEGIN("State");
|
||||||
state->handleEvent(event);
|
state->handleEvent(event);
|
||||||
RW_PROFILE_END()
|
RW_PROFILE_END()
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
#include <script/ScriptMachine.hpp>
|
#include <script/ScriptMachine.hpp>
|
||||||
|
|
||||||
#include <glm/gtc/constants.hpp>
|
#include <glm/gtc/constants.hpp>
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
constexpr float kAutoLookTime = 2.f;
|
constexpr float kAutoLookTime = 2.f;
|
||||||
constexpr float kAutolookMinVelocity = 0.2f;
|
constexpr float kAutolookMinVelocity = 0.2f;
|
||||||
@ -26,67 +25,6 @@ const float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
|
|||||||
const float kVehicleCameraPitch =
|
const float kVehicleCameraPitch =
|
||||||
glm::half_pi<float>() - glm::quarter_pi<float>() * 0.25f;
|
glm::half_pi<float>() - glm::quarter_pi<float>() * 0.25f;
|
||||||
|
|
||||||
// Hardcoded Controls SDLK_* -> GameInputState::Control
|
|
||||||
const std::unordered_multimap<int, GameInputState::Control> kDefaultControls = {
|
|
||||||
/* On Foot */
|
|
||||||
{SDLK_LCTRL, GameInputState::FireWeapon},
|
|
||||||
{SDLK_KP_0, GameInputState::FireWeapon},
|
|
||||||
{SDLK_KP_ENTER, GameInputState::NextWeapon},
|
|
||||||
{SDLK_KP_PERIOD, GameInputState::LastWeapon},
|
|
||||||
{SDLK_w, GameInputState::GoForward},
|
|
||||||
{SDLK_UP, GameInputState::GoForward},
|
|
||||||
{SDLK_s, GameInputState::GoBackwards},
|
|
||||||
{SDLK_DOWN, GameInputState::GoBackwards},
|
|
||||||
{SDLK_a, GameInputState::GoLeft},
|
|
||||||
{SDLK_LEFT, GameInputState::GoLeft},
|
|
||||||
{SDLK_d, GameInputState::GoRight},
|
|
||||||
{SDLK_RIGHT, GameInputState::GoRight},
|
|
||||||
{SDLK_PAGEUP, GameInputState::ZoomIn},
|
|
||||||
{SDLK_z, GameInputState::ZoomIn},
|
|
||||||
{SDLK_PAGEDOWN, GameInputState::ZoomOut},
|
|
||||||
{SDLK_x, GameInputState::ZoomOut},
|
|
||||||
{SDLK_f, GameInputState::EnterExitVehicle},
|
|
||||||
{SDLK_RETURN, GameInputState::EnterExitVehicle},
|
|
||||||
{SDLK_c, GameInputState::ChangeCamera},
|
|
||||||
{SDLK_HOME, GameInputState::ChangeCamera},
|
|
||||||
{SDLK_RCTRL, GameInputState::Jump},
|
|
||||||
{SDLK_SPACE, GameInputState::Jump},
|
|
||||||
{SDLK_LSHIFT, GameInputState::Sprint},
|
|
||||||
{SDLK_RSHIFT, GameInputState::Sprint},
|
|
||||||
{SDLK_LALT, GameInputState::Walk},
|
|
||||||
{SDLK_DELETE, GameInputState::AimWeapon},
|
|
||||||
{SDLK_CAPSLOCK, GameInputState::LookBehind},
|
|
||||||
|
|
||||||
/* In Vehicle */
|
|
||||||
{SDLK_LCTRL, GameInputState::VehicleFireWeapon},
|
|
||||||
{SDLK_a, GameInputState::VehicleLeft},
|
|
||||||
{SDLK_LEFT, GameInputState::VehicleLeft},
|
|
||||||
{SDLK_d, GameInputState::VehicleRight},
|
|
||||||
{SDLK_RIGHT, GameInputState::VehicleRight},
|
|
||||||
{SDLK_w, GameInputState::VehicleAccelerate},
|
|
||||||
{SDLK_UP, GameInputState::VehicleAccelerate},
|
|
||||||
{SDLK_d, GameInputState::VehicleBrake},
|
|
||||||
{SDLK_DOWN, GameInputState::VehicleBrake},
|
|
||||||
{SDLK_INSERT, GameInputState::ChangeRadio},
|
|
||||||
{SDLK_r, GameInputState::ChangeRadio},
|
|
||||||
{SDLK_LSHIFT, GameInputState::Horn},
|
|
||||||
{SDLK_RSHIFT, GameInputState::Horn},
|
|
||||||
{SDLK_KP_PLUS, GameInputState::Submission},
|
|
||||||
{SDLK_CAPSLOCK, GameInputState::Submission},
|
|
||||||
{SDLK_RCTRL, GameInputState::Handbrake},
|
|
||||||
{SDLK_SPACE, GameInputState::Handbrake},
|
|
||||||
{SDLK_KP_9, GameInputState::VehicleAimUp},
|
|
||||||
{SDLK_KP_2, GameInputState::VehicleAimDown},
|
|
||||||
{SDLK_KP_4, GameInputState::VehicleAimLeft},
|
|
||||||
{SDLK_KP_6, GameInputState::VehicleAimRight},
|
|
||||||
{SDLK_KP_9, GameInputState::VehicleDown},
|
|
||||||
{SDLK_KP_2, GameInputState::VehicleUp},
|
|
||||||
{SDLK_KP_1, GameInputState::LookLeft},
|
|
||||||
{SDLK_q, GameInputState::LookLeft},
|
|
||||||
{SDLK_KP_2, GameInputState::LookRight},
|
|
||||||
{SDLK_e, GameInputState::LookRight},
|
|
||||||
};
|
|
||||||
|
|
||||||
IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
|
IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
|
||||||
: State(game)
|
: State(game)
|
||||||
, started(false)
|
, started(false)
|
||||||
@ -445,8 +383,6 @@ void IngameState::handleEvent(const SDL_Event& event) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateInputState(event);
|
|
||||||
|
|
||||||
if (player && player->isInputEnabled()) {
|
if (player && player->isInputEnabled()) {
|
||||||
handlePlayerInput(event);
|
handlePlayerInput(event);
|
||||||
}
|
}
|
||||||
@ -499,22 +435,6 @@ void IngameState::handlePlayerInput(const SDL_Event& event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IngameState::updateInputState(const SDL_Event& event) {
|
|
||||||
switch (event.type) {
|
|
||||||
case SDL_KEYDOWN:
|
|
||||||
case SDL_KEYUP: {
|
|
||||||
auto sym = event.key.keysym.sym;
|
|
||||||
auto level = event.type == SDL_KEYDOWN ? 1.f : 0.f;
|
|
||||||
auto& levels = getWorld()->state->input[0].levels;
|
|
||||||
|
|
||||||
auto range = kDefaultControls.equal_range(sym);
|
|
||||||
for (auto it = range.first; it != range.second; ++it) {
|
|
||||||
levels[it->second] = level;
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IngameState::shouldWorldUpdate() {
|
bool IngameState::shouldWorldUpdate() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,6 @@ public:
|
|||||||
|
|
||||||
virtual void handleEvent(const SDL_Event& event);
|
virtual void handleEvent(const SDL_Event& event);
|
||||||
virtual void handlePlayerInput(const SDL_Event& event);
|
virtual void handlePlayerInput(const SDL_Event& event);
|
||||||
void updateInputState(const SDL_Event& event);
|
|
||||||
|
|
||||||
virtual bool shouldWorldUpdate();
|
virtual bool shouldWorldUpdate();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user