Zadanie 2 WIP2

This commit is contained in:
Michał Leśniak 2021-12-06 19:50:38 +01:00
parent 5950c00e98
commit 5b30c6ca45
7 changed files with 95 additions and 7 deletions

View File

@ -169,7 +169,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Constants.h" /> <ClInclude Include="Constants.h" />
<ClInclude Include="Controllers.h" />
<ClInclude Include="GamePad.h" /> <ClInclude Include="GamePad.h" />
<ClInclude Include="KActionBind.h" />
<ClInclude Include="KCircle.h" /> <ClInclude Include="KCircle.h" />
<ClInclude Include="KGame.h" /> <ClInclude Include="KGame.h" />
<ClInclude Include="KInput.h" /> <ClInclude Include="KInput.h" />

View File

@ -80,5 +80,11 @@
<ClInclude Include="KRect.h"> <ClInclude Include="KRect.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="KActionBind.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Controllers.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,9 @@
#pragma once
namespace KapitanGame {
enum class Controllers : int {
Controller1,
Controller2,
Controller3,
Controller4
};
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "Controllers.h"
#include "KPlayerController.h"
namespace KapitanGame {;
enum class InputState : int {
Pressed,
Released,
Hold
};
struct KActionBind {
std::string Name;
InputState ExpectedInputState;
KPlayerController* ControllerObject;
KPlayerCommand Command;
Controllers Controller;
};
}

View File

@ -117,11 +117,54 @@ namespace KapitanGame {
break; break;
} }
} }
void KInput::BindAction(std::string name, InputState expectedInputState, KPlayerController* controller,
KPlayerCommand command) {
Actions.emplace_back(name, expectedInputState, controller, command);
}
bool KInput::CheckAction(const std::string& name, const InputState expectedInputState, Controllers controller) {
switch(expectedInputState) {
case InputState::Pressed:
const auto keyBind = KeyBinds.find(name);
if(keyBind!=KeyBinds.end()) {
if(IsKeyboardButtonPressed(keyBind->second)) {
return true;
}
}
const auto buttonBind = ButtonBinds[controller].find(name);
if(buttonBind!=ButtonBinds[controller].end()) {
if(IsControllerButtonPressed(controller, buttonBind)) {
return true;
}
}
return false;
case InputState::Released: break;
case InputState::Hold: break;
default: return false;
}
}
void KInput::ProcessActions() {
for(auto action : Actions) {
if(CheckAction(action.Name, action.ExpectedInputState, action.Controller)) {
//(action.ControllerObject->*action.Command)();
std::invoke(action.Command, action.ControllerObject);
}
}
}
bool KInput::IsControllerButtonPressed(const Controllers controllerId, const SDL_GameControllerButton button) { bool KInput::IsControllerButtonPressed(const Controllers controllerId, const SDL_GameControllerButton button) {
if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false; if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false;
return ControllerInputs[static_cast<int>(controllerId)].Buttons[button] && !LastControllerInputs[static_cast<int>(controllerId)].Buttons[button]; return ControllerInputs[static_cast<int>(controllerId)].Buttons[button] && !LastControllerInputs[static_cast<int>(controllerId)].Buttons[button];
} }
bool KInput::IsControllerButtonReleased(const Controllers controllerId, const SDL_GameControllerButton button) {
if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false;
return !ControllerInputs[static_cast<int>(controllerId)].Buttons[button] && LastControllerInputs[static_cast<int>(controllerId)].Buttons[button];
}
bool KInput::IsControllerButtonHeld(const Controllers controllerId, const SDL_GameControllerButton button) { bool KInput::IsControllerButtonHeld(const Controllers controllerId, const SDL_GameControllerButton button) {
if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false; if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false;
@ -144,6 +187,12 @@ namespace KapitanGame {
return KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode]; return KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode];
} }
bool KInput::IsKeyboardButtonReleased(const Uint8 scanCode) {
if (scanCode > KeyboardInputs.size() || scanCode > LastKeyboardInputs.size())
return false;
return !KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode];
}
bool KInput::IsKeyboardButtonHeld(const Uint8 scanCode) { bool KInput::IsKeyboardButtonHeld(const Uint8 scanCode) {
if (scanCode > KeyboardInputs.size()) if (scanCode > KeyboardInputs.size())
return false; return false;

View File

@ -1,23 +1,20 @@
#pragma once #pragma once
#include <SDL_events.h> #include <SDL_events.h>
#include <unordered_map>
#include <vector> #include <vector>
#include "GamePad.h" #include "GamePad.h"
#include "KActionBind.h"
namespace KapitanGame { namespace KapitanGame {
enum class Controllers : int {
Controller1,
Controller2,
Controller3,
Controller4
};
class KInput { class KInput {
std::vector<SDL_GameController*> ConnectedControllers; std::vector<SDL_GameController*> ConnectedControllers;
std::vector<GamePad> ControllerInputs; std::vector<GamePad> ControllerInputs;
std::vector<GamePad> LastControllerInputs; std::vector<GamePad> LastControllerInputs;
std::vector<Uint8> KeyboardInputs; std::vector<Uint8> KeyboardInputs;
std::vector<Uint8> LastKeyboardInputs; std::vector<Uint8> LastKeyboardInputs;
std::vector<KActionBind> Actions;
std::unordered_map<std::string, Uint8> KeyBinds;
int GamepadsCount; int GamepadsCount;
bool Initialized; bool Initialized;
public: public:
@ -27,11 +24,16 @@ namespace KapitanGame {
void HandleInputPreEvents(); void HandleInputPreEvents();
void HandleInputPostEvents(); void HandleInputPostEvents();
void HandleEvent(const SDL_Event& event); void HandleEvent(const SDL_Event& event);
void BindAction(std::string name, InputState expectedInputState, KPlayerController* controller, KPlayerCommand command);
bool CheckAction(const std::string& name, InputState expectedInputState, Controllers controller);
void ProcessActions();
bool IsControllerButtonPressed(Controllers controllerId, SDL_GameControllerButton button); bool IsControllerButtonPressed(Controllers controllerId, SDL_GameControllerButton button);
bool IsControllerButtonReleased(Controllers controllerId, SDL_GameControllerButton button);
bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button); bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button);
float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis); float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis);
bool IsKeyboardButtonHeld(Uint8 scanCode); bool IsKeyboardButtonHeld(Uint8 scanCode);
bool IsKeyboardButtonPressed(Uint8 scanCode); bool IsKeyboardButtonPressed(Uint8 scanCode);
bool IsKeyboardButtonReleased(Uint8 scanCode);
}; };
} }

View File

@ -9,6 +9,7 @@ namespace KapitanGame {
KVector2D Input; KVector2D Input;
KShape Shape; KShape Shape;
}; };
typedef void (KPlayerController::* KPlayerCommand)();
} }