diff --git a/World.cpp b/World.cpp index f632b86..40120ed 100644 --- a/World.cpp +++ b/World.cpp @@ -6,8 +6,9 @@ #include -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) +World::World(int w, int h, sf::Color groundColor, sf::Color ceilingColor, std::vector worldMap) : + player(0,0,0), w(w), h(h), map(std::move(worldMap)), + groundColor(groundColor), ceilingColor(ceilingColor) { map.resize(w*h,BlockType::WALL); } @@ -22,9 +23,14 @@ int World::getH() const return h; } +BlockType World::getBlock(int x, int y) const +{ + return map[x + w*y]; +} + BlockType World::getBlock(float x, float y) const { - return(map.at(static_cast(x)+w* static_cast(y))); + return map[static_cast(x) + w*static_cast(y)]; } void World::setBlock(BlockType block, int x, int y, int width, int height) @@ -83,16 +89,6 @@ 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) const { /* @@ -169,7 +165,8 @@ float World::castRay(float originX, float originY, float orientation) const float hCheckX = originX + hOffsetX; float hCheckY = hOffsetY; /* Bounds + sanity check. */ - while (hCheckX >= 0 && hCheckX <= w && hCheckY >= 0 && hCheckY <= h && i < h) { + while (hCheckX >= 0 && hCheckX <= static_cast(w) && + hCheckY >= 0 && hCheckY <= static_cast(h) && i < h) { if (getBlock(floorf(hCheckX), floorf(hCheckY) + hRound) == BlockType::WALL) { break; } @@ -184,7 +181,8 @@ float World::castRay(float originX, float originY, float orientation) const float vCheckY = originY + vOffsetY; /* Bounds + sanity check. */ - while (vCheckX >= 0 && vCheckX < w && vCheckY >= 0 && vCheckY < h && i < w) { + while (vCheckX >= 0 && vCheckX < static_cast(w) && + vCheckY >= 0 && vCheckY < static_cast(h) && i < w) { if (getBlock(floorf(vCheckX) + vRound, floorf(vCheckY)) == BlockType::WALL) { break; } @@ -208,18 +206,32 @@ float World::castRay(float originX, float originY, float orientation) const return player.focalLength*2/finalDist; } +void World::fillColumn(sf::RenderWindow& window, unsigned int column, + float scale, sf::Color wallColor) const +{ + float columnHeight = static_cast(window.getSize().y)*scale; + sf::RectangleShape pixelColumn(sf::Vector2f(1,columnHeight)); + pixelColumn.setPosition(static_cast(column), + (static_cast(window.getSize().y)-columnHeight)/2.0f); + pixelColumn.setFillColor(wallColor); + + window.draw(pixelColumn); +} + void World::render(sf::RenderWindow& window) const { + float windowX = static_cast(window.getSize().x); + float windowY = static_cast(window.getSize().y); /* * Draw ground and sky planes through half of the screen, as the walls * will get drawn over them. * This doesn't work if we support textures/levels. */ - sf::RectangleShape ground = sf::RectangleShape(sf::Vector2f(window.getSize().x,window.getSize().y/2.0)); + sf::RectangleShape ground = sf::RectangleShape(sf::Vector2f(windowX,windowY/2.0f)); ground.setFillColor(groundColor); - ground.setPosition(0,window.getSize().y/2.0); + ground.setPosition(0,windowY/2.0f); - sf::RectangleShape ceiling = sf::RectangleShape(sf::Vector2f(window.getSize().x,window.getSize().y/2.0)); + sf::RectangleShape ceiling = sf::RectangleShape(sf::Vector2f(windowX,windowY/2.0f)); ceiling.setFillColor(ceilingColor); window.draw(ground); @@ -229,9 +241,9 @@ void World::render(sf::RenderWindow& window) const * Throw rays and draw walls over the ceiling and ground. * Only throws in the plane, which doesn't work for levels/3D. */ - for(int i = 0;i(i)-windowX/2.0f); float rayAngle = player.orientation + deltaAngle; if (rayAngle < 0) { rayAngle += 360; @@ -247,7 +259,7 @@ void World::step(const float& stepTime) { player.move(player.currentMoveSpeedX*stepTime, player.currentMoveSpeedY*stepTime); /* Undo last move if the player would end up in a wall. */ - if (getBlock((int)player.x, (int)player.y) != BlockType::AIR) { + if (getBlock(player.x, player.y) != BlockType::AIR) { player.move(-player.currentMoveSpeedX*stepTime, -player.currentMoveSpeedY*stepTime); } diff --git a/World.h b/World.h index c098e68..7f716e3 100644 --- a/World.h +++ b/World.h @@ -30,8 +30,9 @@ public: int getW() const; int getH() const; - inline BlockType getBlock(float x, float y) const; - void setBlock(BlockType block, int x, int y, int width = 1, int height = 1); + inline BlockType getBlock(int x, int y) const; + inline 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; @@ -50,7 +51,7 @@ private: sf::Color groundColor; sf::Color ceilingColor; - void fillColumn(sf::RenderWindow&, int column, float scale, + void fillColumn(sf::RenderWindow&, unsigned int column, float scale, sf::Color wallColor = sf::Color(84,56,34)) const; /** * Cast a ray from a given position and return the on-screen scale.