-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
82 additions
and
76 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <dog/player/player_controller.hpp> | ||
#include <algorithm> | ||
#include <cassert> | ||
|
||
namespace dog { | ||
|
||
PlayerController::PlayerController(bave::NotNull<bave::App const*> app) : m_app(app) {} | ||
|
||
void PlayerController::bind_throttle(std::string_view const key, Throttle const throttle) { | ||
if (throttle.hi == bave::Key::eUnknown) { return; } | ||
auto it = m_mappings.find(key); | ||
if (it == m_mappings.end()) { | ||
auto [i, _] = m_mappings.insert_or_assign(std::string{key}, std::vector<Mapping>{}); | ||
it = i; | ||
} | ||
assert(it != m_mappings.end()); | ||
it->second.push_back(throttle); | ||
} | ||
|
||
float PlayerController::get_state(std::string_view const key) const { | ||
auto const search = m_mappings.find(key); | ||
if (search == m_mappings.end()) { return 0.0f; } | ||
|
||
auto const& mappings = search->second; | ||
auto const is_pressed = [this](bave::Key const key) { return m_app->get_key_state().is_pressed(key); }; | ||
auto ret = 0.0f; | ||
for (auto const& mapping : mappings) { | ||
if (mapping.lo != bave::Key::eUnknown && is_pressed(mapping.lo)) { ret -= 1.0f; } | ||
if (is_pressed(mapping.hi)) { ret += 1.0f; } | ||
} | ||
return std::clamp(ret, -1.0f, 1.0f); | ||
} | ||
|
||
} // namespace dog |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
#include <bave/app.hpp> | ||
#include <bave/core/string_hash.hpp> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
namespace dog { | ||
class PlayerController { | ||
|
||
public: | ||
struct Throttle { | ||
bave::Key lo{}; | ||
bave::Key hi{}; | ||
}; | ||
|
||
explicit PlayerController(bave::NotNull<bave::App const*> app); | ||
|
||
void bind_throttle(std::string_view key, Throttle throttle); | ||
void bind_trigger(std::string_view key, bave::Key trigger) { bind_throttle(key, Throttle{.hi = trigger}); } | ||
|
||
float get_state(std::string_view key) const; | ||
|
||
private: | ||
bave::NotNull<bave::App const*> m_app; | ||
|
||
using Mapping = Throttle; // later to be a variant of KeyMapping / AxisMapping | ||
|
||
std::unordered_map<std::string, std::vector<Mapping>, bave::StringHash, std::equal_to<>> m_mappings{}; | ||
}; | ||
} // namespace dog |