Skip to content

Commit

Permalink
Better input
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvaka committed Mar 3, 2024
1 parent ebc756a commit ae3f2e5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
5 changes: 0 additions & 5 deletions engine/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#include <functional>

//int player_to_joystick[Input::kMaxPlayers] = { nullptr };

KeyStates Input::action_states[Input::kMaxPlayers][magic_enum::enum_count<GameKeys>()] = { { RELEASED } };
float Input::action_times[Input::kMaxPlayers][magic_enum::enum_count<GameKeys>()] = { { 0 } };
vec Input::analog_states[Input::kMaxPlayers][magic_enum::enum_count<AnalogInput>()];

bool ignoreInput = false;

void Input::IgnoreInput(bool enable) {
Expand Down
20 changes: 16 additions & 4 deletions engine/input.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#pragma once

#include <functional>
#include <SDL_assert.h>

#include "magic_enum.h"

#include "raw_input.h"
#include "../src/input_conf.h"

// Action-based input
struct Input {

static const int kMaxPlayers = 2;
static const int kMaxPlayers;

static vec GetAnalog(int player, AnalogInput k) {
SDL_assert(player < kMaxPlayers);
return analog_states[player][int(k)];
}

static bool IsPressed(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == PRESSED || action_states[player][int(k)] == JUST_PRESSED);
}

Expand All @@ -33,37 +38,44 @@ struct Input {
}

static bool IsReleased(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == RELEASED || action_states[player][int(k)] == JUST_RELEASED);
}

// True for one frame after the action is pressed
static bool IsJustPressed(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == JUST_PRESSED);
}

// True for `interval` time after the key is pressed
static bool IsJustPressed(int player, GameKeys k, float interval) {
SDL_assert(player < kMaxPlayers);
return action_states[player][int(k)] == JUST_PRESSED || (action_states[player][int(k)] == PRESSED && action_times[player][int(k)] < interval);
}

// True for one frame after the action is pressed
static bool IsJustReleased(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == JUST_RELEASED);
}

// True for `interval` time after the key is pressed
static bool IsJustReleased(int player, GameKeys k, float interval) {
SDL_assert(player < kMaxPlayers);
return action_states[player][int(k)] == JUST_RELEASED || (action_states[player][int(k)] == RELEASED && action_times[player][int(k)] < interval);
}

// Consume a just pressed event so the next call to IsJustPressed for that action returns false
static void ConsumeJustPressed(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
action_states[player][int(k)] = PRESSED;
action_times[player][int(k)] += 1000.f;
}

// Consume a just released event so the next call to IsJustReleased for that action returns false
static void ConsumeJustReleased(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
action_states[player][int(k)] = RELEASED;
action_times[player][int(k)] += 1000.f;
}
Expand All @@ -77,9 +89,9 @@ struct Input {
private:
static void MapGameKeys();
static std::function<bool(int)> action_mapping[magic_enum::enum_count<GameKeys>()];
static KeyStates action_states[Input::kMaxPlayers][magic_enum::enum_count<GameKeys>()];
static float action_times[Input::kMaxPlayers][magic_enum::enum_count<GameKeys>()];
static std::function<vec(int)> analog_mapping[magic_enum::enum_count<AnalogInput>()];
static vec analog_states[Input::kMaxPlayers][magic_enum::enum_count<AnalogInput>()];
static KeyStates action_states[][magic_enum::enum_count<GameKeys>()];
static float action_times[][magic_enum::enum_count<GameKeys>()];
static vec analog_states[][magic_enum::enum_count<AnalogInput>()];
};

2 changes: 1 addition & 1 deletion engine/mates.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <math.h>
#include <limits>
#include <string>
#include "SDL_assert.h"
#include <SDL_assert.h>

namespace Mates
{
Expand Down
2 changes: 1 addition & 1 deletion engine/scene_manager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "scene.h"
#include "SDL_assert.h"
#include <SDL_assert.h>

struct SceneManager
{
Expand Down
2 changes: 1 addition & 1 deletion engine/singleinstance.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "SDL_assert.h"
#include <SDL_assert.h>

template <typename T>
class SingleInstance
Expand Down
31 changes: 18 additions & 13 deletions src/input_conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
#include "input.h"
#include "player.h"

#include "magic_enum.h"
#include <functional>

const int Input::kMaxPlayers = 2;

static int keyboard_player_id = 0; // Keyboard controls player one
static bool aimingWithMouse = true;
static vec lastAnalogAim;

std::function<bool(int)> Input::action_mapping[magic_enum::enum_count<GameKeys>()];
std::function<vec(int)> Input::analog_mapping[magic_enum::enum_count<AnalogInput>()];

static bool aimingWithMouse = true;
static vec lastAnalogAim;
KeyStates Input::action_states[Input::kMaxPlayers][magic_enum::enum_count<GameKeys>()] = { { RELEASED } };
float Input::action_times[Input::kMaxPlayers][magic_enum::enum_count<GameKeys>()] = { { 0 } };
vec Input::analog_states[Input::kMaxPlayers][magic_enum::enum_count<AnalogInput>()];

void Input::MapGameKeys()
{
Expand Down Expand Up @@ -135,7 +138,6 @@ void Input::MapGameKeys()
};



// Analog

analog_mapping[(int)AnalogInput::MOVE] = [](int p)
Expand Down Expand Up @@ -165,18 +167,21 @@ void Input::MapGameKeys()
analog_mapping[(int)AnalogInput::AIM] = [](int p)
{
vec joystick = GamePad::AnalogStick::Right.get(p);
if (joystick != vec::Zero) {
aimingWithMouse = false;
}
if (Mouse::IsPressed()) {
aimingWithMouse = true;
}
if (p == keyboard_player_id && aimingWithMouse) {
return Mouse::GetPositionInWorld();
if (p == keyboard_player_id) {
if (joystick != vec::Zero) {
aimingWithMouse = false;
}
if (Mouse::IsPressed()) {
aimingWithMouse = true;
}
if (aimingWithMouse) {
return Mouse::GetPositionInWorld();
}
}
if (joystick != vec::Zero) {
lastAnalogAim = joystick;
}
// For 2 players, lastAnalogAim needs to be turned into an array.
return Player::instance()->pos + lastAnalogAim;
};
}
Expand Down

0 comments on commit ae3f2e5

Please sign in to comment.