Skip to content
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

add player controller. add debug window for testing. #33

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions DogTales/dog/dogtales.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <bave/imgui/im_text.hpp>
#include <dog/dogtales.hpp>

namespace dog {
Expand All @@ -6,6 +7,28 @@ DogTales::DogTales(bave::App& app) : bave::Driver(app), m_player(app, world_spac
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("General")) {
ImGui::Checkbox("Physics", &m_player.physics_enabled);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Player")) {
bave::im_text("Controller States");
ImGui::Separator();
bave::im_text("move_x (Press A/D or Left/Right): {:.2f}", m_player.get_controller_state("move_x"));
bave::im_text("move_y (Press W/S or Up/Down): {:.2f}", m_player.get_controller_state("move_y"));
bave::im_text("Jump (Press Space): {:.2f}", m_player.get_controller_state("jump"));
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
}
ImGui::End();
}

// Update player
m_player.tick(dt);
}
Expand Down
2 changes: 1 addition & 1 deletion DogTales/dog/dogtales.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <bave/driver.hpp>
#include <dog/player.hpp>
#include <dog/player/player.hpp>

namespace dog {
class DogTales : public bave::Driver {
Expand Down
20 changes: 13 additions & 7 deletions DogTales/dog/player.cpp → DogTales/dog/player/player.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#include <dog/player.hpp>
#include <dog/player/player.hpp>

namespace dog {
Player::Player(bave::App& app, glm::vec2 const world_space) : m_app(app), m_world_space(world_space) {
m_sprite.set_size(size_v);

m_player_controller.bind_throttle("move_x", {.lo = bave::Key::eLeft, .hi = bave::Key::eRight});
m_player_controller.bind_throttle("move_x", {.lo = bave::Key::eA, .hi = bave::Key::eD});
m_player_controller.bind_throttle("move_y", {.lo = bave::Key::eDown, .hi = bave::Key::eUp});
m_player_controller.bind_throttle("move_y", {.lo = bave::Key::eS, .hi = bave::Key::eW});
m_player_controller.bind_key("jump", bave::Key::eSpace);
}

void Player::tick(bave::Seconds const dt) {

m_physics.tick(dt);
if (physics_enabled) { m_physics.tick(dt); }

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; }

direction.x += m_player_controller.get_state("move_x");
direction.y += m_player_controller.get_state("move_y");

if (direction.x != 0.0f || direction.y != 0.0f) {
direction = glm::normalize(direction);
Expand All @@ -27,6 +31,8 @@ void Player::tick(bave::Seconds const dt) {

void Player::draw(bave::Shader& shader) const { m_sprite.draw(shader); }

float Player::get_controller_state(std::string_view key) const { return m_player_controller.get_state(key); }

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
8 changes: 7 additions & 1 deletion DogTales/dog/player.hpp → DogTales/dog/player/player.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include <bave/app.hpp>
#include <bave/graphics/sprite.hpp>
#include "components/physics.hpp"
#include <dog/components/physics.hpp>
#include <dog/player/player_controller.hpp>

namespace dog {
class Player {
Expand All @@ -15,6 +16,7 @@ class Player {
bave::Sprite m_sprite{};

component::Physics m_physics{};
PlayerController m_player_controller{&m_app};

void handle_wall_collision();

Expand All @@ -23,5 +25,9 @@ class Player {

void tick(bave::Seconds dt);
void draw(bave::Shader& shader) const;

float get_controller_state(std::string_view key) const;

bool physics_enabled{}; // for debugging
};
} // namespace dog
45 changes: 45 additions & 0 deletions DogTales/dog/player/player_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#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 id, Throttle const throttle) {
if (throttle.hi == bave::Key::eUnknown) { return; }
auto it = m_mappings.find(id);
if (it == m_mappings.end()) {
auto [i, _] = m_mappings.insert_or_assign(std::string{id}, std::vector<Mapping>{});
it = i;
}
assert(it != m_mappings.end());
it->second.push_back(throttle);
}

void PlayerController::bind_key(std::string_view id, bave::Key key) {
// if Throttle::lo is Key::eUnknown it will be ignored in get_state(), so we exploit that here.
bind_throttle(id, Throttle{.hi = key});
}

float PlayerController::get_state(std::string_view const id) const {
auto const search = m_mappings.find(id);
if (search == m_mappings.end()) { return 0.0f; }

auto const& mappings = search->second;
auto const get_throttle_state = [&](Throttle const throttle) {
auto const is_pressed = [this](bave::Key const key) { return m_app->get_key_state().is_pressed(key); };
if (throttle.lo != bave::Key::eUnknown && is_pressed(throttle.lo)) { return -1.0f; }
if (is_pressed(throttle.hi)) { return 1.0f; }
return 0.0f;
};

auto ret = 0.0f;
for (auto const& mapping : mappings) {
// later a visitor will be required here, to handle Throttle vs GamepadAxis vs GamepadButton.
ret += get_throttle_state(mapping);
}
return std::clamp(ret, -1.0f, 1.0f);
}

} // namespace dog
30 changes: 30 additions & 0 deletions DogTales/dog/player/player_controller.hpp
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{}; // -1
bave::Key hi{}; // +1
};

explicit PlayerController(bave::NotNull<bave::App const*> app);

void bind_throttle(std::string_view id, Throttle throttle);
void bind_key(std::string_view id, bave::Key key);

float get_state(std::string_view id) const;

private:
bave::NotNull<bave::App const*> m_app;

using Mapping = Throttle; // later to be a variant of Throttle / GamepadAxis / GamepadButton.

std::unordered_map<std::string, std::vector<Mapping>, bave::StringHash, std::equal_to<>> m_mappings{};
};
} // namespace dog