diff --git a/Player.cpp b/Player.cpp index 7a8196d..b23f8c5 100644 --- a/Player.cpp +++ b/Player.cpp @@ -8,3 +8,21 @@ Player::Player(float x, float y, float alpha) : x(x), y(y), orientation(alpha) {} +void Player::move(float dx, float dy) +{ + x += dx; + y += dy; +} + +void Player::rotate(int alpha) +{ + orientation += alpha%360; + if(orientation >= 360) + { + orientation -= 360; + } + if(orientation <= 360) + { + orientation += 360; + } +} diff --git a/Player.h b/Player.h index 8063e66..05426c5 100644 --- a/Player.h +++ b/Player.h @@ -14,7 +14,10 @@ public: float y; float orientation; - static constexpr float fov = 70; + float fov = 70; + + void move(float dx, float dy); + void rotate(int alpha); }; diff --git a/World.cpp b/World.cpp index 30738b5..ec29ffa 100644 --- a/World.cpp +++ b/World.cpp @@ -5,7 +5,8 @@ #include "World.h" -World::World(int w, int h, std::vector worldMap) : w(w), h(h), map(std::move(worldMap)) +World::World(int w, int h, sf::Color groundColor, sf::Color ceilingColor, std::vector worldMap) : w(w), h(h), + groundColor(groundColor), ceilingColor(ceilingColor), map(std::move(worldMap)), player(0,0,0) { map.resize(w*h,BlockType::WALL); } @@ -51,7 +52,14 @@ std::ostream& operator<<(std::ostream& ostream, World const& world) { case BlockType::AIR: { - ostream << " "; + if(static_cast(world.player.x) == i%world.w && static_cast(world.player.y) == i/world.h) + { + ostream << "P"; + } + else + { + ostream << " "; + } break; } case BlockType::WALL: @@ -73,3 +81,57 @@ std::ostream& operator<<(std::ostream& ostream, World const& world) } return(ostream); } + +void World::fillColumn(sf::RenderWindow& window, int column, float scale, sf::Color wallColor) const +{ + float columnHeight = window.getSize().y*scale; + sf::RectangleShape pixelColumn(sf::Vector2f(1,columnHeight)); + pixelColumn.setPosition(column,(window.getSize().y-columnHeight)/2.0); + pixelColumn.setFillColor(wallColor); + + window.draw(pixelColumn); +} + +float World::castRay(float originX, float originY, float orientation) +{ + float deltaX; + float deltaY; + if(orientation < 45 || orientation > 315) + { + deltaX = ; + } + else if(orientation < 135) + { + + } + else if(orientation < 225) + { + + } + else + { + + } + + return(); +} + +void World::render(sf::RenderWindow& window) const +{ + sf::RectangleShape ground = sf::RectangleShape(sf::Vector2f(window.getSize().x,window.getSize().y/2.0)); + ground.setFillColor(groundColor); + ground.setPosition(0,window.getSize().y/2.0); + + sf::RectangleShape ceiling = sf::RectangleShape(sf::Vector2f(window.getSize().x,window.getSize().y/2.0)); + ceiling.setFillColor(ceilingColor); + + window.draw(ground); + window.draw(ceiling); + + for(int i = 0;i #include +#include +#include + +#include "Player.h" enum class BlockType { AIR, @@ -17,19 +21,29 @@ enum class BlockType { class World { public: - World(int w, int h, std::vector worldMap = {}); + Player player; + World(int w, int h, sf::Color groundColor = sf::Color::Green, sf::Color ceilingColor = sf::Color::Blue, + std::vector worldMap = {}); int getW() const; int getH() const; BlockType getBlock(float x, float y) const; void setBlock(BlockType block, int x, int y, int width = 1, int height = 1); + void render(sf::RenderWindow&) const; + friend std::ostream& operator<<(std::ostream& ostream, World const & world); private: int w; int h; std::vector map; + + sf::Color groundColor; + sf::Color ceilingColor; + + void fillColumn(sf::RenderWindow&, int column, float scale, sf::Color wallColor = sf::Color(127,127,127)) const; + float castRay(float originX, float originY, float orientation); }; diff --git a/main.cpp b/main.cpp index 3689d6d..803c4d9 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,22 @@ int main() World world(10,10); world.setBlock(BlockType::AIR,1,1,8,8); world.setBlock(BlockType::WALL,4,4,2,2); + world.player.move(2,2); std::cout << world << std::endl; + + sf::RenderWindow window(sf::VideoMode(800,600),"Da raycasting"); + world.render(window); + window.display(); + + while (window.isOpen()) + { + sf::Event event; + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + } + } + return 0; } \ No newline at end of file