2019-05-27 13:50:49 +02:00
|
|
|
//
|
|
|
|
// Created by trotfunky on 27/05/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "World.h"
|
|
|
|
|
|
|
|
|
2019-06-01 06:42:29 +02:00
|
|
|
World::World(int w, int h, sf::Color groundColor, sf::Color ceilingColor, std::vector<BlockType> worldMap) : w(w), h(h),
|
|
|
|
groundColor(groundColor), ceilingColor(ceilingColor), map(std::move(worldMap)), player(0,0,0)
|
2019-05-27 13:50:49 +02:00
|
|
|
{
|
|
|
|
map.resize(w*h,BlockType::WALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int World::getW() const
|
|
|
|
{
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
int World::getH() const
|
|
|
|
{
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockType World::getBlock(float x, float y) const
|
|
|
|
{
|
|
|
|
return(map.at(static_cast<int>(x)+w* static_cast<int>(y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void World::setBlock(BlockType block, int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
for(int i = 0;i<height;i++)
|
|
|
|
{
|
|
|
|
for(int j = 0;j<width;j++)
|
|
|
|
{
|
|
|
|
if(x+j<w && y+i < h)
|
|
|
|
{
|
|
|
|
map.at((y+i)*w+x+j) = block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& ostream, World const& world)
|
|
|
|
{
|
|
|
|
for(int i = 0;i<world.w*world.h;i++)
|
|
|
|
{
|
|
|
|
if(i%world.w == 0)
|
|
|
|
{
|
|
|
|
ostream << std::endl;
|
|
|
|
}
|
|
|
|
switch(world.getBlock(i%world.w,i/world.w))
|
|
|
|
{
|
|
|
|
case BlockType::AIR:
|
|
|
|
{
|
2019-06-01 06:42:29 +02:00
|
|
|
if(static_cast<int>(world.player.x) == i%world.w && static_cast<int>(world.player.y) == i/world.h)
|
|
|
|
{
|
|
|
|
ostream << "P";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ostream << " ";
|
|
|
|
}
|
2019-05-27 13:50:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BlockType::WALL:
|
|
|
|
{
|
|
|
|
ostream << "W";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BlockType::DOOR:
|
|
|
|
{
|
|
|
|
ostream << "D";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BlockType::WINDOW:
|
|
|
|
{
|
|
|
|
ostream << "W";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(ostream);
|
|
|
|
}
|
2019-06-01 06:42:29 +02:00
|
|
|
|
|
|
|
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<window.getSize().x;i++)
|
|
|
|
{
|
|
|
|
fillColumn(window, i, 0.5);
|
|
|
|
std::cout << i << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|