From 740e30cb2ba718785cad6b75d02d4c8b63bb0a14 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 3 Feb 2025 14:17:20 -0500 Subject: [PATCH] Strafing now works, thanks to rcr but needs a unit test on the camera and probbably a refactor so that rayview uses it or knows nothing about it? --- camera.cpp | 14 ++++++++++++++ camera.hpp | 3 +++ main.cpp | 30 ++++++++++++++++++++++-------- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/camera.cpp b/camera.cpp index a091637..14c0be6 100644 --- a/camera.cpp +++ b/camera.cpp @@ -38,3 +38,17 @@ bool CameraLOL::play_rotate(Raycaster &rayview) { return t > 1.0; } + +void CameraLOL::plan_strafe(Raycaster &rayview, int dir) { + t = 0.0; + + targetX = rayview.$posX + int(-rayview.$dirY * 1.5 * dir); + targetY = rayview.$posY + int(rayview.$dirX * 1.5 * dir); +} + +bool CameraLOL::play_strafe(Raycaster &rayview) { + t += moveSpeed; + rayview.$posX = std::lerp(rayview.$posX, targetX, t); + rayview.$posY = std::lerp(rayview.$posY, targetY, t); + return t >= 1.0; +} diff --git a/camera.hpp b/camera.hpp index a813569..b4f3ace 100644 --- a/camera.hpp +++ b/camera.hpp @@ -17,4 +17,7 @@ struct CameraLOL { bool play_run(Raycaster &rayview); void plan_rotate(Raycaster &rayview, int dir); bool play_rotate(Raycaster &rayview); + + void plan_strafe(Raycaster &rayview, int dir); + bool play_strafe(Raycaster &rayview); }; diff --git a/main.cpp b/main.cpp index 4258d80..619c839 100644 --- a/main.cpp +++ b/main.cpp @@ -12,7 +12,7 @@ using namespace components; -void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) { +void draw_gui(sf::RenderWindow &window, Raycaster &rayview, sf::Text &text, Stats &stats) { sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT}); rect.setPosition({0,0}); @@ -20,8 +20,11 @@ void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) { window.draw(rect); text.setString( - fmt::format("FPS\nmean:{:>8.5}\nsdev: {:>8.5}\nmin: {:>8.5}\nmax: {:>8.5}\ncount:{:<10}\n\nVSync? {}\nFR Limit: {}\nDebug? {}\n\nHit R to reset.", - stats.mean(), stats.stddev(), stats.min, stats.max, stats.n, VSYNC, FRAME_LIMIT, DEBUG_BUILD)); + fmt::format("FPS\nmean:{:>8.5}\nsdev: {:>8.5}\nmin: {:>8.5}\nmax: {:>8.5}\ncount:{:<10}\n\nVSync? {}\nFR Limit: {}\nDebug? {}\n\nHit R to reset.\n\ndir: {:>2.2},{:>2.2}\npos: {:>2.2},{:>2.2}", + stats.mean(), stats.stddev(), stats.min, + stats.max, stats.n, VSYNC, + FRAME_LIMIT, DEBUG_BUILD, rayview.$dirX, + rayview.$dirY, rayview.$posX, rayview.$posY)); window.draw(text); } @@ -64,12 +67,20 @@ inline void handle_window_events(sf::RenderWindow &window, Raycaster &rayview, state = MOVE; } - if(key->scancode == sf::Keyboard::Scan::D) { + if(key->scancode == sf::Keyboard::Scan::Q) { + camera.plan_rotate(rayview, 1); + state = ROTATE; + } else if(key->scancode == sf::Keyboard::Scan::E) { camera.plan_rotate(rayview, -1); state = ROTATE; + } + + if(key->scancode == sf::Keyboard::Scan::D) { + camera.plan_strafe(rayview, -1); + state = STRAFE; } else if(key->scancode == sf::Keyboard::Scan::A) { - camera.plan_rotate(rayview, 1); - state = ROTATE; + camera.plan_strafe(rayview, 1); + state = STRAFE; } } } @@ -131,7 +142,7 @@ int main() { stats.sample(1/elapsed.count()); auto weapon_sprite_ptr = rayview.$textures.sword.sprite; - draw_gui(window, text, stats); + draw_gui(window, rayview, text, stats); draw_weapon(window, *weapon_sprite_ptr, rotation); window.display(); @@ -146,12 +157,15 @@ int main() { state = IDLE; } } else if(state == STRAFE) { - state = IDLE; + if(camera.play_strafe(rayview)) { + state = IDLE; + } } else { dbc::sentinel("invalid move state."); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) { + rayview.position_camera(player.x + 0.5, player.y + 0.5); stats.reset(); }