2014-12-18 07:02:48 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: Chunk.hpp
|
|
|
|
*
|
2018-06-05 01:24:54 +02:00
|
|
|
* Description:
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
|
|
|
* Created: 15/12/2014 17:31:17
|
|
|
|
*
|
2018-06-12 09:24:43 +02:00
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#ifndef CHUNK_HPP_
|
|
|
|
#define CHUNK_HPP_
|
|
|
|
|
2015-02-06 01:44:16 +01:00
|
|
|
#include <memory>
|
2018-06-24 01:09:32 +02:00
|
|
|
#include <unordered_map>
|
2014-12-18 07:02:48 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2015-02-06 01:44:16 +01:00
|
|
|
#include "Block.hpp"
|
2018-06-19 23:00:50 +02:00
|
|
|
#include "ChunkBuilder.hpp"
|
2018-06-25 15:31:01 +02:00
|
|
|
#include "ChunkLightmap.hpp"
|
2018-06-14 22:11:25 +02:00
|
|
|
#include "IDrawable.hpp"
|
2018-06-12 23:08:42 +02:00
|
|
|
#include "NonCopyable.hpp"
|
2014-12-18 07:02:48 +01:00
|
|
|
#include "Shader.hpp"
|
2014-12-20 01:46:31 +01:00
|
|
|
#include "Texture.hpp"
|
2014-12-18 07:02:48 +01:00
|
|
|
#include "VertexBuffer.hpp"
|
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
class Chunk : public NonCopyable, public IDrawable {
|
2018-06-28 09:22:57 +02:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Front,
|
|
|
|
Back,
|
|
|
|
Bottom,
|
|
|
|
Top
|
|
|
|
};
|
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
public:
|
2015-02-06 01:44:16 +01:00
|
|
|
Chunk(s32 x, s32 y, s32 z, Texture &texture);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-28 09:58:37 +02:00
|
|
|
void update(Player &player, World &world);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-23 23:05:36 +02:00
|
|
|
u32 getBlock(int x, int y, int z) const;
|
2018-06-16 16:45:48 +02:00
|
|
|
void setBlock(int x, int y, int z, u32 id);
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
s32 x() const { return m_x; }
|
|
|
|
s32 y() const { return m_y; }
|
|
|
|
s32 z() const { return m_z; }
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-19 05:38:14 +02:00
|
|
|
Chunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
2018-06-25 15:31:01 +02:00
|
|
|
const Chunk *getSurroundingChunk(u8 i) const { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
2018-06-28 09:22:57 +02:00
|
|
|
void setSurroundingChunk(u8 i, Chunk *chunk) { if (i < 6) m_surroundingChunks[i] = chunk; }
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-12 23:08:42 +02:00
|
|
|
bool isGenerated() const { return m_isGenerated; }
|
|
|
|
bool isInitialized() const { return m_isInitialized; }
|
|
|
|
|
|
|
|
void setChanged(bool isChanged) { m_isChanged = isChanged; }
|
|
|
|
void setGenerated(bool isGenerated) { m_isGenerated = isGenerated; }
|
|
|
|
void setInitialized(bool isInitialized) { m_isInitialized = isInitialized; }
|
|
|
|
|
2018-06-25 15:31:01 +02:00
|
|
|
ChunkLightmap &lightmap() { return m_lightmap; }
|
|
|
|
const ChunkLightmap &lightmap() const { return m_lightmap; }
|
2018-06-25 00:05:21 +02:00
|
|
|
|
2018-06-28 09:22:57 +02:00
|
|
|
static constexpr u8 width = CHUNK_WIDTH;
|
|
|
|
static constexpr u8 height = CHUNK_HEIGHT;
|
|
|
|
static constexpr u8 depth = CHUNK_DEPTH;
|
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
private:
|
2018-06-28 09:58:37 +02:00
|
|
|
void updateNeighbours(int x, int y, int z);
|
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
void draw(RenderTarget &target, RenderStates states) const override;
|
2018-06-14 23:36:01 +02:00
|
|
|
void drawOutlines(RenderTarget &target, RenderStates states) const;
|
2018-06-14 22:11:25 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
s32 m_x;
|
|
|
|
s32 m_y;
|
|
|
|
s32 m_z;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-21 05:45:17 +02:00
|
|
|
Texture &m_texture;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-23 23:05:36 +02:00
|
|
|
using DataArray = u32[Chunk::width][Chunk::height][Chunk::depth];
|
2018-06-16 16:45:48 +02:00
|
|
|
DataArray m_data;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-19 23:00:50 +02:00
|
|
|
ChunkBuilder m_builder;
|
2018-06-25 15:31:01 +02:00
|
|
|
ChunkLightmap m_lightmap{this};
|
2018-06-19 23:00:50 +02:00
|
|
|
|
2018-06-20 01:07:18 +02:00
|
|
|
VertexBuffer m_vbo;
|
2018-06-20 00:57:27 +02:00
|
|
|
std::size_t m_verticesCount;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-20 00:57:27 +02:00
|
|
|
Chunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-20 00:57:27 +02:00
|
|
|
bool m_isChanged = false;
|
|
|
|
bool m_isInitialized = false;
|
|
|
|
bool m_isGenerated = false;
|
2018-06-24 01:09:32 +02:00
|
|
|
|
2018-06-28 09:58:37 +02:00
|
|
|
u32 m_lastTick = 0;
|
2018-06-24 01:09:32 +02:00
|
|
|
std::unordered_map<std::size_t, const Block&> m_tickingBlocks;
|
2014-12-18 07:02:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CHUNK_HPP_
|