From e8a32ba9f3b20a3c0a0938d6ff753e519ec7c735 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 6 Jan 2025 12:19:38 -0500 Subject: [PATCH] Tinkered with strafing but I too dumb. --- raycaster.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/raycaster.cpp b/raycaster.cpp index aae5759..10a5432 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -130,6 +130,13 @@ void draw_everything(sf::RenderWindow &window) { window.display(); } +bool collision(float x, float y) { + int col = int(x / TILE_SIZE); + int row = int(y / TILE_SIZE); + + return MAP[row][col] == 1; +} + int main() { using KB = sf::Keyboard; sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Raycaster"); @@ -138,6 +145,9 @@ int main() { while(window.isOpen()) { draw_everything(window); + float x = player_x; + float y = player_y; + if(KB::isKeyPressed(KB::A)) { player_angle -= 0.1; } else if(KB::isKeyPressed(KB::D)) { @@ -145,11 +155,16 @@ int main() { } if(KB::isKeyPressed(KB::W)) { - player_x += -1 * std::sin(player_angle) * 5; - player_y += std::cos(player_angle) * 5; + x += -1 * std::sin(player_angle) * 5; + y += std::cos(player_angle) * 5; } else if(KB::isKeyPressed(KB::S)) { - player_x -= -1 * std::sin(player_angle) * 5; - player_y -= std::cos(player_angle) * 5; + x -= -1 * std::sin(player_angle) * 5; + y -= std::cos(player_angle) * 5; + } + + if(!collision(x, y)) { + player_x = x; + player_y = y; } sf::Event event;