1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 11:52:40 +01:00

Use input state for player movement

This commit is contained in:
Daniel Evans 2016-06-29 00:59:50 +01:00
parent ae55b888e0
commit 91428fbff1
3 changed files with 47 additions and 74 deletions

View File

@ -3,6 +3,8 @@
struct GameInputState
{
static constexpr float kButtonOnThreshold = 0.1f;
/// Inputs that can be controlled
/// @todo find any sensible values
enum Control {
@ -55,6 +57,16 @@ struct GameInputState
* For buttons, this will result in either 0 or 1.
*/
float currentLevels[_MaxControls] = { };
float operator[] (Control c) const
{
return currentLevels[c];
}
bool pressed(Control c) const
{
return currentLevels[c] > kButtonOnThreshold;
}
};
#endif

View File

@ -283,21 +283,52 @@ void IngameState::tick(float dt)
// Calculate the yaw to look at the target.
float angleYaw = glm::atan(lookdir.y, lookdir.x);
angle = glm::quat( glm::vec3(0.f, 0.f, angleYaw) );
glm::vec3 movement;
// Update player input
const auto& input = getWorld()->state->input;
movement.x = input[GameInputState::GoForward] - input[GameInputState::GoBackwards];
movement.y = input[GameInputState::GoLeft] - input[GameInputState::GoRight];
// Update player with camera yaw
if( player->isInputEnabled() )
{
player->setRunning(!input.pressed(GameInputState::Walk));
/// @todo find the correct behaviour for entering & exiting
if (input.pressed(GameInputState::EnterExitVehicle)) {
/// @todo move me
if (player->getCharacter()->getCurrentVehicle()) {
player->exitVehicle();
}
else if (!player->isCurrentActivity(
Activities::EnterVehicle::ActivityName)) {
player->enterNearestVehicle();
}
}
else if (glm::length2(movement) > 0.001f) {
if (player->isCurrentActivity(
Activities::EnterVehicle::ActivityName)) {
// Give up entering a vehicle if we're alreadying doing so
player->skipActivity();
}
}
if (player->getCharacter()->getCurrentVehicle())
{
player->setMoveDirection(_movement);
auto vehicle = player->getCharacter()->getCurrentVehicle();
vehicle->setHandbraking(input.pressed(GameInputState::Handbrake));
player->setMoveDirection(movement);
}
else
{
float length = glm::length(_movement);
if (input.pressed(GameInputState::Jump)) {
player->jump();
}
float length = glm::length(movement);
float movementAngle = angleYaw - M_PI/2.f;
if (length > 0.1f)
{
glm::vec3 direction = glm::normalize(_movement);
glm::vec3 direction = glm::normalize(movement);
movementAngle += atan2(direction.y, direction.x);
player->setMoveDirection(glm::vec3(1.f, 0.f, 0.f));
}
@ -370,32 +401,6 @@ void IngameState::handleEvent(const SDL_Event& event)
case SDLK_c:
camMode = CameraMode((camMode+(CameraMode)1)%CAMERA_MAX);
break;
case SDLK_w:
_movement.x = 1.f;
break;
case SDLK_s:
_movement.x =-1.f;
break;
case SDLK_a:
_movement.y = 1.f;
break;
case SDLK_d:
_movement.y =-1.f;
break;
default: break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_w:
case SDLK_s:
_movement.x = 0.f;
break;
case SDLK_a:
case SDLK_d:
_movement.y = 0.f;
break;
default: break;
}
break;
@ -415,48 +420,6 @@ void IngameState::handlePlayerInput(const SDL_Event& event)
{
auto player = game->getPlayer();
switch(event.type) {
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_SPACE:
if( player->getCharacter()->getCurrentVehicle() ) {
player->getCharacter()->getCurrentVehicle()->setHandbraking(true);
}
else
{
player->jump();
}
break;
case SDLK_f:
if( player->getCharacter()->getCurrentVehicle()) {
player->exitVehicle();
}
else
if (player->isCurrentActivity(
Activities::EnterVehicle::ActivityName))
{
// Give up entering a vehicle if we're alreadying doing so
player->skipActivity();
}
else {
player->enterNearestVehicle();
}
break;
case SDLK_LSHIFT:
player->setRunning(true);
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_LSHIFT:
player->setRunning(false);
break;
default: break;
}
break;
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button) {

View File

@ -22,8 +22,6 @@ class IngameState : public State
std::string save;
bool newgame;
ViewCamera _look;
/** Player input */
glm::vec3 _movement;
glm::vec3 cameraPosition;
/** Timer to hold user camera position */
float autolookTimer;