-
Notifications
You must be signed in to change notification settings - Fork 0
add player controller. add debug window for testing. #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "PlayerController.hpp" | ||
|
||
namespace dog { | ||
|
||
PlayerController::PlayerController() { | ||
key_map.insert(std::make_pair("move_x", 0.f)); | ||
key_map.insert(std::make_pair("move_y", 0.f)); | ||
key_map.insert(std::make_pair("jump", 0.f)); | ||
} | ||
|
||
void PlayerController::tick(bave::Seconds dt, bave::App const& app) { | ||
|
||
auto const& key_state = app.get_key_state(); | ||
|
||
bool left = key_state.is_pressed(bave::Key::eA) || key_state.is_pressed(bave::Key::eLeft); | ||
bool right = key_state.is_pressed(bave::Key::eD) || key_state.is_pressed(bave::Key::eRight); | ||
bool up = key_state.is_pressed(bave::Key::eW) || key_state.is_pressed(bave::Key::eUp); | ||
bool down = key_state.is_pressed(bave::Key::eS) || key_state.is_pressed(bave::Key::eDown); | ||
|
||
key_map["move_x"] = 0.f; | ||
key_map["move_x"] = left && !right ? -1.f : key_map["move_x"]; | ||
key_map["move_x"] = right && !left ? 1.f : key_map["move_x"]; | ||
key_map["move_x"] = right && left ? 0.f : key_map["move_x"]; | ||
|
||
key_map["move_y"] = 0.f; | ||
key_map["move_y"] = down && !up ? -1.f : key_map["move_y"]; | ||
key_map["move_y"] = up && !down ? 1.f : key_map["move_y"]; | ||
key_map["move_y"] = up && down ? 0.f : key_map["move_y"]; | ||
|
||
key_map["jump"] = key_state.is_pressed(bave::Key::eW) || key_state.is_pressed(bave::Key::eUp) ? 1.f : 0.f; | ||
} | ||
|
||
|
||
std::optional<float> PlayerController::get_controller_state(std::string_view key) const { | ||
if (auto search = key_map.find(key); search != key_map.end()) { | ||
return search->second; | ||
} else { | ||
return -1.f; | ||
} | ||
} | ||
|
||
|
||
} // namespace dog |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
#include <bave/app.hpp> | ||
#include <bave/core/string_hash.hpp> | ||
#include <optional> | ||
|
||
namespace dog { | ||
class PlayerController { | ||
|
||
public: | ||
PlayerController(); | ||
|
||
void tick(bave::Seconds dt, bave::App const& app); | ||
std::optional<float> get_controller_state(std::string_view key) const; | ||
|
||
private: | ||
std::unordered_map<std::string, float, bave::StringHash, std::equal_to<>> key_map{}; | ||
|
||
}; | ||
} // namespace dog |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filename should be in snake_case like others.