OpenMiner/include/world/Chunk.hpp

111 lines
2.9 KiB
C++
Raw Normal View History

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
*
* 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>
#include <unordered_map>
2014-12-18 07:02:48 +01:00
#include <vector>
2015-02-06 01:44:16 +01:00
#include "Block.hpp"
#include "BlockData.hpp"
2018-06-19 23:00:50 +02:00
#include "ChunkBuilder.hpp"
#include "ChunkLightmap.hpp"
#include "IDrawable.hpp"
#include "NonCopyable.hpp"
2014-12-18 07:02:48 +01:00
#include "Shader.hpp"
#include "Vector3.hpp"
2014-12-18 07:02:48 +01:00
#include "VertexBuffer.hpp"
class Chunk : public NonCopyable {
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:
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
u16 getBlock(int x, int y, int z) const;
u16 getData(int x, int y, int z) const;
void setBlock(int x, int y, int z, u16 type);
void setData(int x, int y, int z, u16 data);
BlockData *getBlockData(int x, int y, int z);
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
Chunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
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
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; }
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;
void drawLayer(RenderTarget &target, RenderStates states, u16 layer) const;
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);
// void drawOutlines(RenderTarget &target, RenderStates states) const;
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
Texture &m_texture;
2018-06-05 01:24:54 +02:00
using DataArray = u32[Chunk::width][Chunk::height][Chunk::depth];
DataArray m_data;
2018-06-05 01:24:54 +02:00
2018-06-19 23:00:50 +02:00
ChunkBuilder m_builder;
ChunkLightmap m_lightmap{this};
2018-06-19 23:00:50 +02:00
std::array<VertexBuffer, ChunkBuilder::layers> m_vbo;
std::array<std::size_t, ChunkBuilder::layers> m_verticesCount;
Chunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
2018-06-05 01:24:54 +02:00
bool m_isChanged = false;
bool m_isInitialized = false;
bool m_isGenerated = false;
2018-06-28 09:58:37 +02:00
u32 m_lastTick = 0;
std::unordered_map<std::size_t, const Block&> m_tickingBlocks;
std::unordered_map<Vector3i, BlockData> m_blockData;
2014-12-18 07:02:48 +01:00
};
#endif // CHUNK_HPP_