diff --git a/src/Game.cpp b/src/Game.cpp index 79dfb8b..aa377d8 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -111,25 +111,25 @@ void Game::update(double dt) { player.update(dt, window, worldView); // INIT/UPDATE RAYS ------------------------------------------ - auto mousePos = sf::Mouse::getPosition(window); - auto mousePosWorld = window.mapPixelToCoords(mousePos, worldView); - + sf::Vector2 playerPosition = player.position; rayVertices.clear(); hitPoints.clear(); for (sf::Vector2 targetPoint : walls.getPoints()) { - hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, 0.f)); - hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, 0.001f)); - hitPoints.emplace_back(updateRay(mousePosWorld, targetPoint, -0.001f)); + hitPoints.emplace_back(updateRay(playerPosition, targetPoint, 0.f)); + hitPoints.emplace_back(updateRay(playerPosition, targetPoint, 0.001f)); + hitPoints.emplace_back(updateRay(playerPosition, targetPoint, -0.001f)); } std::sort(hitPoints.begin(), hitPoints.end(), - [mousePosWorld](sf::Vector2f a, sf::Vector2f b) -> bool { - auto angleA = atan2(mousePosWorld.y - a.y, mousePosWorld.x - a.x); - auto angleB = atan2(mousePosWorld.y - b.y, mousePosWorld.x - b.x); + [playerPosition](sf::Vector2f a, sf::Vector2f b) -> bool { + auto angleA = + atan2(playerPosition.y - a.y, playerPosition.x - a.x); + auto angleB = + atan2(playerPosition.y - b.y, playerPosition.x - b.x); return angleA < angleB; }); - rayVertices.append({mousePosWorld, lightColor}); + rayVertices.append({playerPosition, lightColor}); for (auto hitPoint : hitPoints) { rayVertices.append({hitPoint, lightColor}); } diff --git a/src/Game.hpp b/src/Game.hpp index 05f742b..7fb6299 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -12,7 +12,7 @@ #include "Player.hpp" #include "Walls.hpp" -constexpr bool VSYNC = false; +constexpr bool VSYNC = true; constexpr int WIDTH = 1920; constexpr int HEIGHT = 1080; constexpr int DEBUG_REFRESH_RATE_IN_MS = 250; diff --git a/src/Player.cpp b/src/Player.cpp index 8c63c66..262ebbf 100644 --- a/src/Player.cpp +++ b/src/Player.cpp @@ -1,4 +1,5 @@ #include "Player.hpp" +#include "Game.hpp" #include #include @@ -11,8 +12,12 @@ void Player::update(double dt, sf::RenderWindow &window, const sf::View &view) { auto mousePos = sf::Mouse::getPosition(window); auto mousePosWorld = window.mapPixelToCoords(mousePos, view); - circleShape.setPosition( - {mousePosWorld.x - shapeRadius, mousePosWorld.y - shapeRadius}); + if ((mousePosWorld.x > WIDTH || mousePosWorld.x < 0) || + (mousePosWorld.y > HEIGHT || mousePosWorld.y < 0)) + return; + + position = {mousePosWorld.x - shapeRadius, mousePosWorld.y - shapeRadius}; + circleShape.setPosition(position); }; void Player::render(sf::RenderWindow &window) { window.draw(circleShape); }; diff --git a/src/Player.hpp b/src/Player.hpp index e63ee19..eeb8212 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -5,11 +5,14 @@ #include #include #include +#include class Player { public: Player(float size, sf::Color color); + sf::Vector2f position; + void update(double dt, sf::RenderWindow &window, const sf::View &view); void render(sf::RenderWindow &window); diff --git a/src/Walls.cpp b/src/Walls.cpp index b568af8..af2987c 100644 --- a/src/Walls.cpp +++ b/src/Walls.cpp @@ -4,10 +4,18 @@ #include Walls::Walls() : walls(sf::PrimitiveType::Lines) { - add({1.f, 1.f}, {WIDTH - 1.f, 1.f}); - add({1.f, 1.f}, {1.f, HEIGHT - 1.f}); - add({WIDTH - 1.f, 1.f}, {WIDTH - 1.f, HEIGHT - 1.f}); - add({1.f, HEIGHT - 1.f}, {WIDTH - 1.f, HEIGHT - 1.f}); + constexpr int dWidth = WIDTH * 2; + constexpr int dHeight = HEIGHT * 2; + + constexpr sf::Vector2f topLeft = {-WIDTH, -HEIGHT}; + constexpr sf::Vector2f topRight = {dWidth, -HEIGHT}; + constexpr sf::Vector2f bottomLeft = {-WIDTH, dHeight}; + constexpr sf::Vector2f bottomRight = {dWidth, dHeight}; + + add(topLeft, topRight); + add(topLeft, bottomLeft); + add(topRight, bottomRight); + add(bottomLeft, bottomRight); add({1000.f, 500.f}, {1500.f, 500.f}); add({500.f, 240.f}, {500.f, 760.f});