Ajout de la fonction render au monde
Joueur déplaçable début du cast de rayon Trop fatigué pour faire de la trigo
This commit is contained in:
parent
52a261de58
commit
f21fc6f805
5 changed files with 117 additions and 4 deletions
18
Player.cpp
18
Player.cpp
|
@ -8,3 +8,21 @@
|
||||||
Player::Player(float x, float y, float alpha) : x(x), y(y), orientation(alpha)
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
5
Player.h
5
Player.h
|
@ -14,7 +14,10 @@ public:
|
||||||
float y;
|
float y;
|
||||||
float orientation;
|
float orientation;
|
||||||
|
|
||||||
static constexpr float fov = 70;
|
float fov = 70;
|
||||||
|
|
||||||
|
void move(float dx, float dy);
|
||||||
|
void rotate(int alpha);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
66
World.cpp
66
World.cpp
|
@ -5,7 +5,8 @@
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
|
||||||
|
|
||||||
World::World(int w, int h, std::vector<BlockType> worldMap) : w(w), h(h), map(std::move(worldMap))
|
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)
|
||||||
{
|
{
|
||||||
map.resize(w*h,BlockType::WALL);
|
map.resize(w*h,BlockType::WALL);
|
||||||
}
|
}
|
||||||
|
@ -51,7 +52,14 @@ std::ostream& operator<<(std::ostream& ostream, World const& world)
|
||||||
{
|
{
|
||||||
case BlockType::AIR:
|
case BlockType::AIR:
|
||||||
{
|
{
|
||||||
ostream << " ";
|
if(static_cast<int>(world.player.x) == i%world.w && static_cast<int>(world.player.y) == i/world.h)
|
||||||
|
{
|
||||||
|
ostream << "P";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ostream << " ";
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case BlockType::WALL:
|
case BlockType::WALL:
|
||||||
|
@ -73,3 +81,57 @@ std::ostream& operator<<(std::ostream& ostream, World const& world)
|
||||||
}
|
}
|
||||||
return(ostream);
|
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<window.getSize().x;i++)
|
||||||
|
{
|
||||||
|
fillColumn(window, i, 0.5);
|
||||||
|
std::cout << i << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
16
World.h
16
World.h
|
@ -7,6 +7,10 @@
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Player.h"
|
||||||
|
|
||||||
enum class BlockType {
|
enum class BlockType {
|
||||||
AIR,
|
AIR,
|
||||||
|
@ -17,19 +21,29 @@ enum class BlockType {
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
World(int w, int h, std::vector<BlockType> worldMap = {});
|
Player player;
|
||||||
|
|
||||||
|
World(int w, int h, sf::Color groundColor = sf::Color::Green, sf::Color ceilingColor = sf::Color::Blue,
|
||||||
|
std::vector<BlockType> worldMap = {});
|
||||||
int getW() const;
|
int getW() const;
|
||||||
int getH() const;
|
int getH() const;
|
||||||
|
|
||||||
BlockType getBlock(float x, float y) const;
|
BlockType getBlock(float x, float y) const;
|
||||||
void setBlock(BlockType block, int x, int y, int width = 1, int height = 1);
|
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);
|
friend std::ostream& operator<<(std::ostream& ostream, World const & world);
|
||||||
private:
|
private:
|
||||||
int w;
|
int w;
|
||||||
int h;
|
int h;
|
||||||
std::vector<BlockType> map;
|
std::vector<BlockType> 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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
16
main.cpp
16
main.cpp
|
@ -9,6 +9,22 @@ int main()
|
||||||
World world(10,10);
|
World world(10,10);
|
||||||
world.setBlock(BlockType::AIR,1,1,8,8);
|
world.setBlock(BlockType::AIR,1,1,8,8);
|
||||||
world.setBlock(BlockType::WALL,4,4,2,2);
|
world.setBlock(BlockType::WALL,4,4,2,2);
|
||||||
|
world.player.move(2,2);
|
||||||
std::cout << world << std::endl;
|
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;
|
return 0;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue