Skip to content

Commit

Permalink
add player controller. add debug window for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
swagween committed Feb 19, 2024
1 parent 310d3b0 commit ee94aee
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
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"));
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) {
try {
return m_player_controller.key_map[key];
} catch (std::out_of_range const& oor) { return -1.f; }
}

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);
};
} // namespace dog
13 changes: 13 additions & 0 deletions DogTales/dog/player/PlayerController.cpp
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{};

};
} // namespace dog::player

0 comments on commit ee94aee

Please sign in to comment.