1
0
Fork 0

First commit, world map creation

This commit is contained in:
trotFunky 2019-05-27 13:50:49 +02:00
commit 52a261de58
7 changed files with 176 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
**/cmake-build-*
**/.idea/

18
CMakeLists.txt Normal file
View file

@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.14)
project(raycasting)
set(CMAKE_CXX_STANDARD 14)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML COMPONENTS system window graphics network audio REQUIRED)
if(NOT SFML_FOUND)
message(FATAL_ERROR "SFML could not be found")
endif()
add_executable(raycasting main.cpp Player.cpp Player.h World.cpp World.h)
target_link_libraries(raycasting
sfml-window
sfml-graphics)

10
Player.cpp Normal file
View file

@ -0,0 +1,10 @@
//
// Created by trotfunky on 27/05/19.
//
#include "Player.h"
Player::Player(float x, float y, float alpha) : x(x), y(y), orientation(alpha)
{}

21
Player.h Normal file
View file

@ -0,0 +1,21 @@
//
// Created by trotfunky on 27/05/19.
//
#ifndef RAYCASTING_PLAYER_H
#define RAYCASTING_PLAYER_H
class Player {
public:
Player(float x, float y, float alpha);
float x;
float y;
float orientation;
static constexpr float fov = 70;
};
#endif //RAYCASTING_PLAYER_H

75
World.cpp Normal file
View file

@ -0,0 +1,75 @@
//
// Created by trotfunky on 27/05/19.
//
#include "World.h"
World::World(int w, int h, std::vector<BlockType> worldMap) : w(w), h(h), map(std::move(worldMap))
{
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:
{
ostream << " ";
break;
}
case BlockType::WALL:
{
ostream << "W";
break;
}
case BlockType::DOOR:
{
ostream << "D";
break;
}
case BlockType::WINDOW:
{
ostream << "W";
break;
}
}
}
return(ostream);
}

36
World.h Normal file
View file

@ -0,0 +1,36 @@
//
// Created by trotfunky on 27/05/19.
//
#ifndef RAYCASTING_WORLD_H
#define RAYCASTING_WORLD_H
#include <vector>
#include <ostream>
enum class BlockType {
AIR,
WALL,
DOOR,
WINDOW,
};
class World {
public:
World(int w, int h, std::vector<BlockType> 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);
friend std::ostream& operator<<(std::ostream& ostream, World const & world);
private:
int w;
int h;
std::vector<BlockType> map;
};
#endif //RAYCASTING_WORLD_H

14
main.cpp Normal file
View file

@ -0,0 +1,14 @@
#include <iostream>
#include "SFML/Graphics.hpp"
#include "World.h"
int main()
{
std::cout << "Hello, World!" << std::endl;
World world(10,10);
world.setBlock(BlockType::AIR,1,1,8,8);
world.setBlock(BlockType::WALL,4,4,2,2);
std::cout << world << std::endl;
return 0;
}