Skip to content

Commit

Permalink
Started implementing WASD movement.
Browse files Browse the repository at this point in the history
  • Loading branch information
A-rms committed Feb 8, 2024
1 parent b20ddd2 commit 58f6fa5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include(FetchContent)
FetchContent_Declare(
bave
GIT_REPOSITORY https://github.com/karnkaul/bave
GIT_TAG 412eda4
GIT_TAG 532fa551c0d90ac25eb93e91a730b526f6071559
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/bave"
)

Expand Down
3 changes: 1 addition & 2 deletions DogTales/dog/dogtales.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <dog/dogtales.hpp>

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

void DogTales::tick() {
auto const dt = get_app().get_dt();
Expand All @@ -18,7 +18,6 @@ void DogTales::render() const {

void DogTales::on_key(bave::KeyInput const& key_input) {
// Forward the key input to the player for handling
m_player.handle_input(key_input);
}

void DogTales::set_viewport_to_world_space() const {
Expand Down
2 changes: 1 addition & 1 deletion DogTales/dog/dogtales.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace dog {
class DogTales : public bave::Driver {
static constexpr glm::vec2 world_space_v{1280.0f, 720.0f};

Player m_player{world_space_v};
Player m_player;

void tick() final;
void render() const final;
Expand Down
46 changes: 20 additions & 26 deletions DogTales/dog/player.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
#include <dog/player.hpp>

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

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

m_physics.tick(dt);
m_sprite.transform.position = m_physics.position;

// m_physics.tick(dt);
// m_sprite.transform.position = m_physics.position;

// m_physics.position = m_sprite.transform.position;
auto const& key_state = m_app.get_key_state();
auto d_x_y = glm::vec2{};
if (key_state.is_pressed(bave::Key::eW)) { d_x_y.y += 1.0f; }
if (key_state.is_pressed(bave::Key::eS)) { d_x_y.y -= 1.0f; }
if (key_state.is_pressed(bave::Key::eA)) { d_x_y.x -= 1.0f; }
if (key_state.is_pressed(bave::Key::eD)) { d_x_y.x += 1.0f; }

// multiply direction * speed * dt
if (d_x_y.x != 0.0f || d_x_y.y != 0.0f) {
d_x_y = glm::normalize(d_x_y);
auto const displacement = d_x_y * speed_v * dt.count();
m_sprite.transform.position += displacement;
}
handle_wall_collision();
m_physics.position = m_sprite.transform.position;
}

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

void Player::update_movement(glm::vec2 const& direction) {
// Normalize the direction vector
// glm::vec2 normalized_direction = glm::normalize(direction);

// Apply speed to the normalized direction vector
}

void Player::handle_input(bave::KeyInput const& key_input) {
// Mapping keys to direction offsets
std::unordered_map<bave::Key, glm::vec2> keyMappings{
{bave::Key::eW, {0.0f, 1.0f}},
{bave::Key::eA, {-1.0f, 0.0f}},
{bave::Key::eS, {0.0f, -1.0f}},
{bave::Key::eD, {1.0f, 0.0f}},
};

// Check if the pressed key has a corresponding direction in the map
auto it = keyMappings.find(key_input.key);
if (it != keyMappings.end()) { update_movement(it->second); }
}

void Player::handle_wall_collision() {
auto& position = m_sprite.transform.position;
// bounce_rect represents the play area for the sprite, ie the limits for its centre.
Expand Down
7 changes: 3 additions & 4 deletions DogTales/dog/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Player {
static constexpr glm::vec2 speed_v{500.0f, 500.0f};
static constexpr glm::vec2 size_v{50.0f, 90.0f};

bave::App& m_app;

glm::vec2 m_world_space{};

bave::Sprite m_sprite{};
Expand All @@ -17,12 +19,9 @@ class Player {
void handle_wall_collision();

public:
explicit Player(glm::vec2 world_space);

void handle_input(bave::KeyInput const& key_input);
explicit Player(bave::App& app, glm::vec2 world_space);

void tick(bave::Seconds dt);
void draw(bave::Shader& shader) const;
void update_movement(glm::vec2 const& direction);
};
} // namespace dog

0 comments on commit 58f6fa5

Please sign in to comment.