fixed input with keyboard and controller at the same time
This commit is contained in:
parent
d8fa53801b
commit
eabeb55115
11 changed files with 95 additions and 68 deletions
51
src/Game.cpp
51
src/Game.cpp
|
@ -25,14 +25,14 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer)
|
|||
|
||||
//TODO: read keybinds from config if it exists. otherwise initialze standard and write file
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_ESCAPE, new EndGameCommand(this));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_W, new MoveUpCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_A, new MoveLeftCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_S, new MoveDownCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_D, new MoveRightCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_UP, new MoveUpCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_LEFT, new MoveLeftCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_DOWN, new MoveDownCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_RIGHT, new MoveRightCommand(m_Player1DirectionInput, m_ButtonInput));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_W, new MoveUpCommand(m_Player1DirectionButtonStatus));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_A, new MoveLeftCommand(m_Player1DirectionButtonStatus));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_S, new MoveDownCommand(m_Player1DirectionButtonStatus));
|
||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_D, new MoveRightCommand(m_Player1DirectionButtonStatus));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_UP, new MoveUpCommand(m_Player1DirectionButtonStatus));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_LEFT, new MoveLeftCommand(m_Player1DirectionButtonStatus));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_DOWN, new MoveDownCommand(m_Player1DirectionButtonStatus));
|
||||
m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_RIGHT, new MoveRightCommand(m_Player1DirectionButtonStatus));
|
||||
m_Gamepad1AxisCommandMap.emplace(SDL_GAMEPAD_AXIS_LEFTX, new MoveXCommand(m_Player1DirectionInput));
|
||||
m_Gamepad1AxisCommandMap.emplace(SDL_GAMEPAD_AXIS_LEFTY, new MoveYCommand(m_Player1DirectionInput));
|
||||
|
||||
|
@ -41,14 +41,20 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer)
|
|||
void Game::Run()
|
||||
{
|
||||
float deltaTime = (SDL_GetTicks() - m_TickCount) / 1000.0f;
|
||||
uint64_t ticks = SDL_GetTicks() - m_TickCount;
|
||||
ticks = ticks > 0 ? ticks : 1;
|
||||
int fps = 1000/(ticks);
|
||||
m_TickCount = SDL_GetTicks();
|
||||
HandleMovement();
|
||||
m_Player1->Update(deltaTime);
|
||||
|
||||
SDL_SetRenderDrawColor(m_Renderer.get(), 0, 0, 0, 255);
|
||||
SDL_RenderClear(m_Renderer.get());
|
||||
|
||||
SDL_SetRenderDrawColor(m_Renderer.get(), 90, 90, 90, 255);
|
||||
|
||||
|
||||
SDL_SetRenderDrawColor(m_Renderer.get(), 255, 255, 255, 255);
|
||||
SDL_RenderDebugTextFormat(m_Renderer.get(), 10, 440, "%" SDL_PRIu64 " fps", fps);
|
||||
SDL_SetRenderDrawColor(m_Renderer.get(), 150, 0, 150, 255);
|
||||
SDL_FRect player = m_Player1->GetHitbox();
|
||||
SDL_RenderFillRect(m_Renderer.get(), &player);
|
||||
SDL_RenderPresent(m_Renderer.get());
|
||||
|
@ -57,24 +63,19 @@ void Game::Run()
|
|||
|
||||
void Game::HandleMovement()
|
||||
{
|
||||
//if both direction buttons are pressed, normalize the direction vector
|
||||
if((m_Player1DirectionInput.y == 1 || m_Player1DirectionInput.y == -1)
|
||||
&& (m_Player1DirectionInput.x == 1 || m_Player1DirectionInput.x == -1))
|
||||
NormalizedDirection direction;
|
||||
if(m_Player1DirectionButtonStatus.GetDirection(direction))
|
||||
{
|
||||
float x = m_Player1DirectionInput.x* c_NormalizedDiagonal;
|
||||
float y = m_Player1DirectionInput.y * c_NormalizedDiagonal;
|
||||
NormalizedDirection player1InputDirection(x, y, true);
|
||||
m_Player1->SetDirection(player1InputDirection);
|
||||
m_Player1->SetDirection(direction);
|
||||
return;
|
||||
}
|
||||
else if(abs(m_Player1DirectionInput.x) < c_GamepadAxisDeadzone && abs(m_Player1DirectionInput.y) < c_GamepadAxisDeadzone)
|
||||
{
|
||||
m_Player1->SetDirection(NormalizedDirection());
|
||||
}
|
||||
else
|
||||
else if(abs(m_Player1DirectionInput.x) > c_GamepadAxisDeadzone || abs(m_Player1DirectionInput.y) > c_GamepadAxisDeadzone)
|
||||
{
|
||||
NormalizedDirection player1InputDirection(m_Player1DirectionInput.x, m_Player1DirectionInput.y, true);
|
||||
m_Player1->SetDirection(player1InputDirection);
|
||||
return;
|
||||
}
|
||||
m_Player1->SetDirection(direction);
|
||||
}
|
||||
|
||||
void Game::HandleKeyboardInput(const SDL_Event &event)
|
||||
|
@ -118,11 +119,7 @@ void Game::HandleGamepadAxis(const SDL_Event &event)
|
|||
if(commandIterator != m_Gamepad1AxisCommandMap.end())
|
||||
{
|
||||
float value = (float)event.gaxis.value / std::numeric_limits<int16_t>::max();
|
||||
if(!m_ButtonInput || abs(value) > c_GamepadAxisDeadzone)
|
||||
{
|
||||
commandIterator->second->Execute(value);
|
||||
m_ButtonInput = false;
|
||||
}
|
||||
commandIterator->second->Execute(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue