Zadanie 2 WIP2
This commit is contained in:
parent
5950c00e98
commit
5b30c6ca45
@ -169,7 +169,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Constants.h" />
|
||||
<ClInclude Include="Controllers.h" />
|
||||
<ClInclude Include="GamePad.h" />
|
||||
<ClInclude Include="KActionBind.h" />
|
||||
<ClInclude Include="KCircle.h" />
|
||||
<ClInclude Include="KGame.h" />
|
||||
<ClInclude Include="KInput.h" />
|
||||
|
@ -80,5 +80,11 @@
|
||||
<ClInclude Include="KRect.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KActionBind.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Controllers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
9
2dgk_7/2dgk_7/Controllers.h
Normal file
9
2dgk_7/2dgk_7/Controllers.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
namespace KapitanGame {
|
||||
enum class Controllers : int {
|
||||
Controller1,
|
||||
Controller2,
|
||||
Controller3,
|
||||
Controller4
|
||||
};
|
||||
}
|
19
2dgk_7/2dgk_7/KActionBind.h
Normal file
19
2dgk_7/2dgk_7/KActionBind.h
Normal 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;
|
||||
};
|
||||
}
|
||||
|
@ -117,11 +117,54 @@ namespace KapitanGame {
|
||||
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) {
|
||||
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::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) {
|
||||
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];
|
||||
}
|
||||
|
||||
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) {
|
||||
if (scanCode > KeyboardInputs.size())
|
||||
return false;
|
||||
|
@ -1,23 +1,20 @@
|
||||
#pragma once
|
||||
#include <SDL_events.h>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "GamePad.h"
|
||||
#include "KActionBind.h"
|
||||
|
||||
namespace KapitanGame {
|
||||
enum class Controllers : int {
|
||||
Controller1,
|
||||
Controller2,
|
||||
Controller3,
|
||||
Controller4
|
||||
};
|
||||
|
||||
class KInput {
|
||||
std::vector<SDL_GameController*> ConnectedControllers;
|
||||
std::vector<GamePad> ControllerInputs;
|
||||
std::vector<GamePad> LastControllerInputs;
|
||||
std::vector<Uint8> KeyboardInputs;
|
||||
std::vector<Uint8> LastKeyboardInputs;
|
||||
std::vector<KActionBind> Actions;
|
||||
std::unordered_map<std::string, Uint8> KeyBinds;
|
||||
int GamepadsCount;
|
||||
bool Initialized;
|
||||
public:
|
||||
@ -27,11 +24,16 @@ namespace KapitanGame {
|
||||
void HandleInputPreEvents();
|
||||
void HandleInputPostEvents();
|
||||
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 IsControllerButtonReleased(Controllers controllerId, SDL_GameControllerButton button);
|
||||
bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button);
|
||||
float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis);
|
||||
bool IsKeyboardButtonHeld(Uint8 scanCode);
|
||||
bool IsKeyboardButtonPressed(Uint8 scanCode);
|
||||
bool IsKeyboardButtonReleased(Uint8 scanCode);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ namespace KapitanGame {
|
||||
KVector2D Input;
|
||||
KShape Shape;
|
||||
};
|
||||
typedef void (KPlayerController::* KPlayerCommand)();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user