1
0
Fork 0
Toy-Raytracer/World.h
Teo-CD 9678e8c286 World: Introduce a struct for raycast results
In order to implement more complexe behaviors, more information than the distance
is needed from a raycast.
Add a new struct to store and return this information, currently hit position
and hit type, and update castRay to return that instead.
2024-02-05 00:33:01 +00:00

78 lines
1.8 KiB
C++

//
// Created by trotfunky on 27/05/19.
//
#ifndef RAYCASTING_WORLD_H
#define RAYCASTING_WORLD_H
#include <vector>
#include <ostream>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <optional>
#include "Color.h"
#include "Player.h"
enum class BlockType {
AIR,
WALL,
DOOR,
WINDOW,
};
struct RaycastResult {
float distance;
float hitX;
float hitY;
BlockType hitBlock;
};
class World {
public:
Player player;
World(int w, int h,
sf::Color groundColor = Colors::Ground,
sf::Color ceilingColor = Colors::Ceiling,
std::vector<BlockType> worldMap = {});
int getW() const;
int getH() const;
void resizeWindow(const sf::FloatRect& resizedView);
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&);
/**
* Move the world one step forward.
* @param stepTime delta time since last step, in seconds
*/
void step(const float& stepTime);
friend std::ostream& operator<<(std::ostream& ostream, World const & world);
private:
int w;
int h;
std::vector<sf::RectangleShape> renderColumns;
std::vector<BlockType> map;
sf::Color groundColor;
sf::Color ceilingColor;
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
sf::Color fillColor = Colors::Wall);
/**
* Cast a ray from a given position and return its distance to the origin.
* @param originX Ray X origin, strictly positive
* @param originY Ray Y origin, strictly positive
* @param orientation Angle to cast to, in degrees between 0 and 360
* @return Result struct containing information on the hit.
*/
RaycastResult castRay(float originX, float originY, float orientation) const;
};
#endif //RAYCASTING_WORLD_H