Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions DogTales/dog/dogtales.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
#include <dog/dogtales.hpp>
#include <bave/imgui/im_text.hpp>

namespace dog {
DogTales::DogTales(bave::App& app) : bave::Driver(app), m_player(app, world_space_v) {}

void DogTales::tick() {
auto const dt = get_app().get_dt();

//ImGui calls
if constexpr (bave::imgui_v) {
if (ImGui::Begin("Debug")) {
if(ImGui::BeginTabBar("Main")) {
if (ImGui::BeginTabItem("Player")) {
bave::im_text("Controller States");
ImGui::Separator();
ImGui::Text("Test (Press 'T'): %.2f", m_player.get_controller_state("test"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ImGui::Text("Test (Press 'T'): %.2f", m_player.get_controller_state("test"));
bave::im_text("Test (Press 'T'): {:.2f}", m_player.get_controller_state("test"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
}
ImGui::End();
}

// Update player
m_player.tick(dt);
}
Expand Down
12 changes: 7 additions & 5 deletions DogTales/dog/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a float const is redundant, compiler/linter should be warning about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I just make it float?

Copy link
Member

Choose a reason for hiding this comment

The 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; }
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what should i do instead? just return the element? or use if()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all the member function should be const. Once that's done key_map[key] will not compile anyway. So you'd want to use key_map.find(key) and return a default value / nullopt if the key isn't present.

}

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.
Expand Down
4 changes: 4 additions & 0 deletions DogTales/dog/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,6 +16,7 @@ class Player {
bave::Sprite m_sprite{};

component::Physics m_physics{};
player::PlayerController m_player_controller{};

void handle_wall_collision();

Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a std::string_view would optimize calls made with literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

};
} // namespace dog
13 changes: 13 additions & 0 deletions DogTales/dog/player/PlayerController.cpp
Copy link
Member

@karnkaul karnkaul Feb 20, 2024

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.

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
16 changes: 16 additions & 0 deletions DogTales/dog/player/PlayerController.hpp
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{};
Copy link
Member

Choose a reason for hiding this comment

The 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 string_view lookups:

#include <bave/core/string_hash.hpp>

// ...
std::unordered_map<std::string, float, bave::StringHash, std::equal_to<>> key_map{};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, fixed!


};
} // namespace dog::player