From 310d3b0fef265ed23a9474c1135abd2e864ca573 Mon Sep 17 00:00:00 2001 From: A-rms <154934589+A-rms@users.noreply.github.com> Date: Fri, 16 Feb 2024 14:09:15 -0500 Subject: [PATCH] Added basic WASD movement. (#27) * 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. --- CMakeLists.txt | 2 +- DogTales/dog/dogtales.cpp | 7 ++----- DogTales/dog/dogtales.hpp | 4 +--- DogTales/dog/player.cpp | 21 +++++++++++++++++---- DogTales/dog/player.hpp | 4 +++- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d735b41..0ae12ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" ) diff --git a/DogTales/dog/dogtales.cpp b/DogTales/dog/dogtales.cpp index eccaf33..1e86c9a 100644 --- a/DogTales/dog/dogtales.cpp +++ b/DogTales/dog/dogtales.cpp @@ -1,11 +1,12 @@ #include 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); } @@ -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; } diff --git a/DogTales/dog/dogtales.hpp b/DogTales/dog/dogtales.hpp index 68e1141..d627f1f 100644 --- a/DogTales/dog/dogtales.hpp +++ b/DogTales/dog/dogtales.hpp @@ -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: diff --git a/DogTales/dog/player.cpp b/DogTales/dog/player.cpp index 0989b8d..e8387ca 100644 --- a/DogTales/dog/player.cpp +++ b/DogTales/dog/player.cpp @@ -1,21 +1,34 @@ #include 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. diff --git a/DogTales/dog/player.hpp b/DogTales/dog/player.hpp index 0e4263f..07e51a2 100644 --- a/DogTales/dog/player.hpp +++ b/DogTales/dog/player.hpp @@ -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{}; @@ -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;