Skip to content

Commit

Permalink
Counters added for calls to tick(), handle(), and each of the WASD keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
Legac3e committed Jan 30, 2024
1 parent e0700ff commit 2468464
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
78 changes: 63 additions & 15 deletions DogTales/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,60 @@
#include <src/player.hpp>

namespace {
constexpr void update_motion(bave::Action const action, bool& out_held) {
constexpr void update_motion(bave::Action const action, bool& out_held, KeyCounter& keyCount) {
switch (action) {
case bave::Action::ePress: out_held = true; break;
case bave::Action::eRelease: out_held = false; break;
case bave::Action::eRepeat: keyCount.repeat++; break;
case bave::Action::ePress:
keyCount.press++;
out_held = true;
break;
case bave::Action::eRelease:
keyCount.release++;
out_held = false;
break;
default: break;
}
}
} // namespace

void Player::display_key_counter_with_checkbox(char const* name, KeyCounter const& counter, bool& held) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("%s", name);
ImGui::TableSetColumnIndex(1);
ImGui::Checkbox("##held", &held); // Use the key name as part of the ID to ensure uniqueness
ImGui::TableSetColumnIndex(2);
ImGui::Text("%llu", counter.press);
ImGui::TableSetColumnIndex(3);
ImGui::Text("%llu", counter.release);
ImGui::TableSetColumnIndex(4);
ImGui::Text("%llu", counter.repeat);
}

Player::Player(glm::vec2 const world_space) : m_world_space(world_space) { m_sprite.set_size(size_v); }

void Player::handle(bave::KeyInput const& key_input) {
if (key_input.key == bave::Key::eW || key_input.key == bave::Key::eUp) {
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eUp)));
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eUp)), m_key_w_counter);
m_tick_event_counter.key_event++;
}
if (key_input.key == bave::Key::eA || key_input.key == bave::Key::eLeft) {
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eLeft)));
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eLeft)), m_key_a_counter);
m_tick_event_counter.key_event++;
}
if (key_input.key == bave::Key::eS || key_input.key == bave::Key::eDown) {
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eDown)));
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eDown)), m_key_s_counter);
m_tick_event_counter.key_event++;
}
if (key_input.key == bave::Key::eD || key_input.key == bave::Key::eRight) {
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eRight)));
update_motion(key_input.action, m_dir_held.at(static_cast<std::size_t>(Dir::eRight)), m_key_d_counter);
m_tick_event_counter.key_event++;
}
}

void Player::tick(bave::Seconds const dt) {
m_tick_event_counter.tick++;

auto dxy = glm::vec2{};
if (m_dir_held.at(static_cast<std::size_t>(Dir::eUp))) { dxy.y += 1.0f; }
if (m_dir_held.at(static_cast<std::size_t>(Dir::eLeft))) { dxy.x -= 1.0f; }
Expand All @@ -53,17 +80,38 @@ void Player::draw_debug_window() {
if constexpr (bave::imgui_v) {
if (ImGui::Begin("Player")) {
ImGui::DragFloat("speed", &m_speed, 1.0f, 1.0f, 10000.0f);

ImGui::Separator();
ImGui::Text("Tick Count: %llu", m_tick_event_counter.tick);
ImGui::Text("Key Event Count: %llu", m_tick_event_counter.key_event);

ImGui::Separator();
if (ImGui::BeginTable("Key Counts and Directions", 5)) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("Key (Dir)");
ImGui::TableSetColumnIndex(1);
ImGui::Text("Held");
ImGui::TableSetColumnIndex(2);
ImGui::Text("Press");
ImGui::TableSetColumnIndex(3);
ImGui::Text("Release");
ImGui::TableSetColumnIndex(4);
ImGui::Text("Repeat");

display_key_counter_with_checkbox("W (Up)", m_key_w_counter,
m_dir_held.at(static_cast<std::size_t>(Dir::eUp)));
display_key_counter_with_checkbox("A (Left)", m_key_a_counter,
m_dir_held.at(static_cast<std::size_t>(Dir::eLeft)));
display_key_counter_with_checkbox("S (Down)", m_key_s_counter,
m_dir_held.at(static_cast<std::size_t>(Dir::eDown)));
display_key_counter_with_checkbox("D (Right)", m_key_d_counter,
m_dir_held.at(static_cast<std::size_t>(Dir::eRight)));

bave::im_text("Held keys");
ImGui::BeginDisabled();
ImGui::Checkbox("up", &m_dir_held.at(static_cast<std::size_t>(Dir::eUp)));
ImGui::Checkbox("left", &m_dir_held.at(static_cast<std::size_t>(Dir::eLeft)));
ImGui::Checkbox("down", &m_dir_held.at(static_cast<std::size_t>(Dir::eDown)));
ImGui::Checkbox("right", &m_dir_held.at(static_cast<std::size_t>(Dir::eRight)));
ImGui::EndDisabled();
ImGui::EndTable();
}
ImGui::End();
}
ImGui::End();
}
}

Expand Down
18 changes: 18 additions & 0 deletions DogTales/src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@
#include <bave/app.hpp>
#include <bave/graphics/sprite.hpp>

struct TickAndKeyEventCounter {
uint64_t tick{0};
uint64_t key_event{0};
};

struct KeyCounter {
uint64_t press{0};
uint64_t release{0};
uint64_t repeat{0};
};

class Player {
KeyCounter m_key_w_counter{};
KeyCounter m_key_a_counter{};
KeyCounter m_key_s_counter{};
KeyCounter m_key_d_counter{};
TickAndKeyEventCounter m_tick_event_counter{};

enum class Dir : int { eLeft, eRight, eUp, eDown };

static constexpr glm::vec2 size_v{50.0f, 90.0f};
Expand All @@ -24,4 +41,5 @@ class Player {
void draw(bave::Shader& shader) const;

void draw_debug_window();
void display_key_counter_with_checkbox(char const* name, KeyCounter const& counter, bool& held);
};

0 comments on commit 2468464

Please sign in to comment.