feat: add block placement and destruction

This commit is contained in:
2026-03-14 14:37:27 +08:00
parent ec2d3c3c0c
commit 8b1579b5fd
11 changed files with 329 additions and 198 deletions

View File

@@ -20,4 +20,9 @@ struct BlockRenderData {
glm::vec3 pos;
std::vector<bool> draw_face;
unsigned block_id;
};
struct LookBlock {
glm::ivec3 pos;
glm::ivec3 normal;
};

View File

@@ -14,7 +14,7 @@ private:
World& m_world;
// the index is a array of block id
std::vector<uint8_t> m_blocks;
GLuint m_vbo;
GLuint m_vbo = 0;
std::vector<Vertex> m_vertexs_data;
public:
@@ -28,5 +28,7 @@ public:
GLuint get_vbo() const;
const std::vector<Vertex>& get_vertex_data() const;
void init_chunk();
void set_chunk_block(int index, unsigned id);
};

View File

@@ -2,17 +2,11 @@
#include <glm/glm.hpp>
#include <Cubed/config.hpp>
#include <Cubed/gameplay/block.hpp>
#include <Cubed/input.hpp>
#include <optional>
#include <string>
struct MoveState {
bool forward = false;
bool back = false;
bool left = false;
bool right = false;
bool down = false;
bool up = false;
};
class World;
@@ -31,17 +25,17 @@ private:
glm::vec3 m_right;
MoveState m_move_state;
std::optional<glm::ivec3> m_look_block_pos = std::nullopt;
std::optional<LookBlock> m_look_block = std::nullopt;
std::string m_name;
World& m_world;
bool ray_cast(const glm::vec3& start, const glm::vec3& dir, glm::ivec3& block_pos, float distance = 4.0f);
bool ray_cast(const glm::vec3& start, const glm::vec3& dir, glm::ivec3& block_pos, glm::vec3& normal, float distance = 4.0f);
public:
Player(World& world, const std::string& name);
~Player();
const glm::vec3& get_front() const;
const std::optional<glm::ivec3>& get_look_block_pos() const;
const std::optional<LookBlock>& get_look_block_pos() const;
const glm::vec3& get_player_pos() const;
const MoveState& get_move_state() const;

View File

@@ -17,11 +17,12 @@ public:
~World();
const BlockRenderData& get_block_render_data(int x, int y ,int z);
const std::optional<glm::ivec3>& get_look_block_pos(const std::string& name) const;
const std::optional<LookBlock>& get_look_block_pos(const std::string& name) const;
Player& get_player(const std::string& name);
void init_world();
bool is_block(const glm::ivec3& block_pos) const;
void render();
void set_block(const glm::ivec3& pos, unsigned id);
void update(float delta_time);

24
include/Cubed/input.hpp Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
struct MoveState {
bool forward = false;
bool back = false;
bool left = false;
bool right = false;
bool down = false;
bool up = false;
};
struct MouseState {
bool left = false;
bool right = false;
};
struct InputState {
MoveState move_state;
MouseState mouse_state;
};
namespace Input {
InputState& get_input_state();
}