From 58f6fa5e6f0be0c1084955e3ab1549b39aafdf93 Mon Sep 17 00:00:00 2001 From: Arms Date: Wed, 7 Feb 2024 23:59:58 -0500 Subject: [PATCH] Started implementing WASD movement. --- CMakeLists.txt | 2 +- DogTales/dog/dogtales.cpp | 3 +-- DogTales/dog/dogtales.hpp | 2 +- DogTales/dog/player.cpp | 46 +++++++++++++++++---------------------- DogTales/dog/player.hpp | 7 +++--- 5 files changed, 26 insertions(+), 34 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 67ec791..917f8ff 100644 --- a/DogTales/dog/dogtales.cpp +++ b/DogTales/dog/dogtales.cpp @@ -1,7 +1,7 @@ #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(); @@ -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 { diff --git a/DogTales/dog/dogtales.hpp b/DogTales/dog/dogtales.hpp index 68e1141..4563233 100644 --- a/DogTales/dog/dogtales.hpp +++ b/DogTales/dog/dogtales.hpp @@ -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; diff --git a/DogTales/dog/player.cpp b/DogTales/dog/player.cpp index b9601b6..947217c 100644 --- a/DogTales/dog/player.cpp +++ b/DogTales/dog/player.cpp @@ -1,40 +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; - + // 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 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. diff --git a/DogTales/dog/player.hpp b/DogTales/dog/player.hpp index 453602d..fe990f6 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; + glm::vec2 m_world_space{}; bave::Sprite m_sprite{}; @@ -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