-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
84 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,51 @@ | ||
#include "statemanager.hpp" | ||
|
||
#include "event.hpp" | ||
#include <unordered_map> | ||
|
||
using namespace framework; | ||
|
||
bool statemanager::is_keydown(const input::keyevent &event) const { | ||
const auto it = _keys.find(event); | ||
return it != _keys.end() && it->second; | ||
bool statemanager::on(int player, const std::variant<input::controller> &type) const noexcept { | ||
auto pit = _state.find(player); | ||
if (pit != _state.end()) { | ||
auto &map = pit->second; | ||
auto tit = map.find(type); | ||
if (tit != map.end()) { | ||
return tit->second; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
constexpr std::optional<input::controller> keytoctrl(const input::keyevent &event) { | ||
using input::controller; | ||
using input::keyevent; | ||
|
||
switch (event) { | ||
case keyevent::up: | ||
return controller::up; | ||
case keyevent::down: | ||
return controller::down; | ||
case keyevent::left: | ||
return controller::left; | ||
case keyevent::right: | ||
return controller::right; | ||
case keyevent::space: | ||
return controller::cross; | ||
default: | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
void statemanager::on_keydown(const input::keyevent &event) noexcept { | ||
_keys.emplace(event, true).first->second = true; | ||
if (auto ctrl = keytoctrl(event)) { | ||
_state[0][*ctrl] = true; | ||
} | ||
} | ||
|
||
void statemanager::on_keyup(const input::keyevent &event) noexcept { | ||
if (auto it = _keys.find(event); it != _keys.end()) { | ||
it->second = false; | ||
if (auto ctrl = keytoctrl(event)) { | ||
_state[0][*ctrl] = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters