Skip to content

Commit

Permalink
Added basic WASD movement. (#27)
Browse files Browse the repository at this point in the history
* Added basic WASD movement.

Quad is static on program start.

W-UP
A-LEFT
S-DOWN
D-RIGHT

* Modified my WASD movement from if else if blocks to using an unordered_map.

Removed unused include, removed unused variables.

Currently working on adding flags to allow diagonal movement. And stoping movement on key_release.

* Started implementing WASD movement.

* Modified player movement with WASD to allow arrow keys. Diagonal movement is included.

* Reintegrated physics with movement. Removed certain comments. Addressed PR issues.

* Modified sprite_pos to be physics_pos.
  • Loading branch information
A-rms authored Feb 16, 2024
1 parent f3ab96d commit 310d3b0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 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
7 changes: 2 additions & 5 deletions DogTales/dog/dogtales.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#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();

// Update player
m_player.tick(dt);
}

Expand All @@ -15,10 +16,6 @@ void DogTales::render() const {
if (auto shader = get_app().load_shader("shaders/default.vert", "shaders/default.frag")) { m_player.draw(*shader); }
}

void DogTales::on_key(bave::KeyInput const& key_input) {
if (key_input.key == bave::Key::eEscape && key_input.action == bave::Action::eRelease) { get_app().shutdown(); }
}

void DogTales::set_viewport_to_world_space() const {
get_app().get_render_device().render_view.viewport = world_space_v;
}
Expand Down
4 changes: 1 addition & 3 deletions DogTales/dog/dogtales.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ 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;

void on_key(bave::KeyInput const& key_input) final;

void set_viewport_to_world_space() const;

public:
Expand Down
21 changes: 17 additions & 4 deletions DogTales/dog/player.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +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;

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);
auto const displacement = direction * speed_v * dt.count();
m_physics.position += displacement;
}
handle_wall_collision();
m_physics.position = m_sprite.transform.position;
m_sprite.transform.position = m_physics.position;
}

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

void Player::handle_wall_collision() {
auto& position = m_sprite.transform.position;
auto& position = m_physics.position;
// bounce_rect represents the play area for the sprite, ie the limits for its centre.
// the size is simply the total space minus the sprite size, centered at the origin.
// the second argument (glm::vec2{0.0f}) is the default value and can be omitted here.
Expand Down
4 changes: 3 additions & 1 deletion 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; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)

glm::vec2 m_world_space{};

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

public:
explicit Player(glm::vec2 world_space);
explicit Player(bave::App& app, glm::vec2 world_space);

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

0 comments on commit 310d3b0

Please sign in to comment.