-
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 1 commit
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 |
---|---|---|
|
@@ -8,13 +8,9 @@ Player::Player(bave::App& app, glm::vec2 const world_space) : m_app(app), m_worl | |
void Player::tick(bave::Seconds const dt) { | ||
|
||
m_physics.tick(dt); | ||
m_player_controller.tick(dt, m_app); | ||
|
||
auto const& key_state = m_app.get_key_state(); | ||
auto direction = glm::vec2{}; | ||
if (key_state.is_pressed(bave::Key::eW) || key_state.is_pressed(bave::Key::eUp)) { direction.y += 1.0f; } | ||
if (key_state.is_pressed(bave::Key::eS) || key_state.is_pressed(bave::Key::eDown)) { direction.y -= 1.0f; } | ||
if (key_state.is_pressed(bave::Key::eA) || key_state.is_pressed(bave::Key::eLeft)) { direction.x -= 1.0f; } | ||
if (key_state.is_pressed(bave::Key::eD) || key_state.is_pressed(bave::Key::eRight)) { direction.x += 1.0f; } | ||
|
||
if (direction.x != 0.0f || direction.y != 0.0f) { | ||
direction = glm::normalize(direction); | ||
|
@@ -27,6 +23,12 @@ void Player::tick(bave::Seconds const dt) { | |
|
||
void Player::draw(bave::Shader& shader) const { m_sprite.draw(shader); } | ||
|
||
float const Player::get_controller_state(std::string key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should I just make it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep |
||
try { | ||
return m_player_controller.key_map[key]; | ||
} catch (std::out_of_range const& oor) { return -1.f; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to risk exceptional paths in hot code like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what should i do instead? just return the element? or use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First of all the member function should be |
||
} | ||
|
||
void Player::handle_wall_collision() { | ||
auto& position = m_physics.position; | ||
// bounce_rect represents the play area for the sprite, ie the limits for its centre. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#include <bave/app.hpp> | ||
#include <bave/graphics/sprite.hpp> | ||
#include "components/physics.hpp" | ||
#include "player/PlayerController.hpp" | ||
|
||
namespace dog { | ||
class Player { | ||
|
@@ -15,6 +16,7 @@ class Player { | |
bave::Sprite m_sprite{}; | ||
|
||
component::Physics m_physics{}; | ||
player::PlayerController m_player_controller{}; | ||
|
||
void handle_wall_collision(); | ||
|
||
|
@@ -23,5 +25,7 @@ class Player { | |
|
||
void tick(bave::Seconds dt); | ||
void draw(bave::Shader& shader) const; | ||
|
||
float const get_controller_state(std::string key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! |
||
}; | ||
} // namespace dog |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filename should be in snake_case like others. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "PlayerController.hpp" | ||
#include <dog/player/PlayerController.hpp> | ||
|
||
namespace dog::player { | ||
|
||
PlayerController::PlayerController() { key_map.insert(std::make_pair("test", 0.f)); } | ||
|
||
void PlayerController::tick(bave::Seconds dt, const bave::App& app) { | ||
auto const& key_state = app.get_key_state(); | ||
key_map["test"] = key_state.is_pressed(bave::Key::eT) ? 1.f : 0.f; | ||
} | ||
|
||
} // namespace dog::player |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
#include <bave/app.hpp> | ||
|
||
namespace dog::player { | ||
class PlayerController { | ||
|
||
public: | ||
|
||
PlayerController(); | ||
|
||
void tick(bave::Seconds dt, const bave::App& app); | ||
|
||
std::unordered_map<std::string, float> key_map{}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be public, as anybody can modify it. Also, use an annoyingly long form to enable #include <bave/core/string_hash.hpp>
// ...
std::unordered_map<std::string, float, bave::StringHash, std::equal_to<>> key_map{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, fixed! |
||
|
||
}; | ||
} // namespace dog::player |
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.
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.
fixed