mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
perf: fast chunk generation
This commit is contained in:
@@ -13,6 +13,11 @@ private:
|
||||
|
||||
bool m_is_gened = false;
|
||||
bool m_dirty = false;
|
||||
|
||||
static constexpr int SIZE_X = CHUCK_SIZE;
|
||||
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||
static constexpr int SIZE_Z = CHUCK_SIZE;
|
||||
|
||||
ChunkPos m_chunk_pos;
|
||||
World& m_world;
|
||||
// the index is a array of block id
|
||||
@@ -26,6 +31,10 @@ private:
|
||||
public:
|
||||
Chunk(World& world, ChunkPos chunk_pos);
|
||||
~Chunk();
|
||||
Chunk(const Chunk&) = delete;
|
||||
Chunk& operator=(const Chunk&) = delete;
|
||||
Chunk(Chunk&&) = default;
|
||||
Chunk& operator=(Chunk&&) = delete;
|
||||
const std::vector<uint8_t>& get_chunk_blocks() const;
|
||||
|
||||
static int get_index(int x, int y, int z);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
struct ChunkPos {
|
||||
int x;
|
||||
int z;
|
||||
|
||||
bool operator==(const ChunkPos&) const = default;
|
||||
struct Hash {
|
||||
std::size_t operator()(const ChunkPos& pos) const{
|
||||
@@ -24,3 +25,6 @@ struct ChunkPos {
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <Cubed/AABB.hpp>
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
#include <Cubed/gameplay/chunk_status.hpp>
|
||||
#include <Cubed/input.hpp>
|
||||
|
||||
#include <optional>
|
||||
@@ -43,10 +44,14 @@ private:
|
||||
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 move_distance {0.0f, 0.0f, 0.0f};
|
||||
// player is tow block tall, the pos is the lower pos
|
||||
|
||||
glm::vec3 m_player_pos {0.0f, 120.0f, 0.0f};
|
||||
ChunkPos m_player_chunk_pos {0, 0};
|
||||
|
||||
glm::vec3 m_front {0, 0, -1};
|
||||
glm::vec3 m_right {0, 0, 0};
|
||||
glm::vec3 m_size {0.6f, 1.8f, 0.6f};
|
||||
|
||||
Gait m_gait = Gait::WALK;
|
||||
MoveState m_move_state {};
|
||||
|
||||
@@ -56,6 +61,7 @@ private:
|
||||
|
||||
bool ray_cast(const glm::vec3& start, const glm::vec3& dir, glm::ivec3& block_pos, glm::vec3& normal, float distance = 4.0f);
|
||||
|
||||
void check_player_chunk_transition();
|
||||
void update_direction();
|
||||
void update_lookup_block();
|
||||
void update_move(float delta_time);
|
||||
|
||||
@@ -8,24 +8,21 @@
|
||||
class Player;
|
||||
|
||||
class World {
|
||||
private:
|
||||
bool need_gen_chunk = false;
|
||||
|
||||
BlockRenderData m_block_render_data;
|
||||
private:
|
||||
bool m_need_gen_chunk;
|
||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
||||
std::unordered_map<std::size_t, Player> m_players;
|
||||
std::vector<glm::vec4> m_planes;
|
||||
|
||||
std::pair<int, int> chunk_pos(int world_x, int world_z) const;
|
||||
void gen_chunks();
|
||||
|
||||
|
||||
public:
|
||||
|
||||
World();
|
||||
~World();
|
||||
|
||||
bool can_move(const AABB& player_box) const;
|
||||
const BlockRenderData& get_block_render_data(int x, int y ,int z);
|
||||
//const BlockRenderData& get_block_render_data(int x, int y ,int z);
|
||||
const std::optional<LookBlock>& get_look_block_pos(const std::string& name) const;
|
||||
Player& get_player(const std::string& name);
|
||||
void init_world();
|
||||
@@ -34,6 +31,8 @@ public:
|
||||
int get_block(const glm::ivec3& block_pos) const;
|
||||
bool is_block(const glm::ivec3& block_pos) const;
|
||||
|
||||
ChunkPos chunk_pos(int world_x, int world_z) const;
|
||||
|
||||
void need_gen();
|
||||
void render(const glm::mat4& mvp_matrix);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user