mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
feat: add block placement and destruction
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/gameplay/world.hpp>
|
||||
#include <Cubed/map_table.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/cubed_hash.hpp>
|
||||
World::World() {
|
||||
@@ -35,189 +36,36 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
|
||||
z = world_z - chunk_z * CHUCK_SIZE;
|
||||
// block id
|
||||
m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)];
|
||||
if (m_block_render_data.block_id == 0) {
|
||||
return m_block_render_data;
|
||||
}
|
||||
// draw_face
|
||||
m_block_render_data.draw_face.assign(6, true);
|
||||
if (x > 0 ) {
|
||||
if (chunk_blocks[Chunk::get_index(x - 1, y, z)]) {
|
||||
m_block_render_data.draw_face[3] = false;
|
||||
}
|
||||
}
|
||||
if (x < CHUCK_SIZE - 1) {
|
||||
if (chunk_blocks[Chunk::get_index(x + 1, y, z)]) {
|
||||
m_block_render_data.draw_face[1] = false;
|
||||
}
|
||||
}
|
||||
if (z > 0 ) {
|
||||
if (chunk_blocks[Chunk::get_index(x, y, z - 1)]) {
|
||||
m_block_render_data.draw_face[2] = false;
|
||||
}
|
||||
}
|
||||
if (z < CHUCK_SIZE - 1) {
|
||||
if (chunk_blocks[Chunk::get_index(x, y, z + 1)]) {
|
||||
m_block_render_data.draw_face[0] = false;
|
||||
}
|
||||
}
|
||||
if (y > 0 ) {
|
||||
if (chunk_blocks[Chunk::get_index(x, y - 1, z)]) {
|
||||
m_block_render_data.draw_face[5] = false;
|
||||
}
|
||||
}
|
||||
if (y < CHUCK_SIZE - 1) {
|
||||
if (chunk_blocks[Chunk::get_index(x, y + 1, z)]) {
|
||||
m_block_render_data.draw_face[4] = false;
|
||||
}
|
||||
}
|
||||
int adjacent_chunk_x;
|
||||
int adjacent_chunk_z;
|
||||
int adj_world_x;
|
||||
int adj_world_z;
|
||||
int adjacent_x, adjacent_z;
|
||||
if (x == 0) {
|
||||
|
||||
adj_world_x = world_x - 1;
|
||||
adj_world_z = world_z;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x / CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - 1 - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - adjacent_chunk_z * CHUCK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
|
||||
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
|
||||
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
|
||||
|
||||
} else
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[3] = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (x == CHUCK_SIZE - 1) {
|
||||
adj_world_x = world_x + 1;
|
||||
adj_world_z = world_z;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x/ CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x + 1 - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - adjacent_chunk_z * CHUCK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
|
||||
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
|
||||
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
|
||||
} else
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[1] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (z == 0) {
|
||||
adj_world_x = world_x;
|
||||
adj_world_z = world_z - 1;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x / CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - 1 - adjacent_chunk_z * CHUCK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
|
||||
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
|
||||
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
|
||||
} else
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (z == CHUCK_SIZE - 1) {
|
||||
adj_world_x = world_x;
|
||||
adj_world_z = world_z + 1;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x/ CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z + 1 - adjacent_chunk_z * CHUCK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
|
||||
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
|
||||
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
|
||||
} else
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[0] = false;
|
||||
}
|
||||
static const std::vector<glm::ivec3> DIR = {
|
||||
glm::ivec3(0, 0, 1),
|
||||
glm::ivec3(1, 0, 0),
|
||||
glm::ivec3(0 ,0, -1),
|
||||
glm::ivec3(-1, 0, 0),
|
||||
glm::ivec3(0, 1, 0),
|
||||
glm::ivec3(0, -1, 0)
|
||||
};
|
||||
glm::ivec3 world_pos = glm::ivec3(world_x, world_y, world_z);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (is_block(world_pos + DIR[i])) {
|
||||
m_block_render_data.draw_face[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
return m_block_render_data;
|
||||
}
|
||||
|
||||
const std::optional<glm::ivec3>& World::get_look_block_pos(const std::string& name) const{
|
||||
static std::optional<glm::ivec3> null_pos = std::nullopt;
|
||||
const std::optional<LookBlock>& World::get_look_block_pos(const std::string& name) const{
|
||||
static std::optional<LookBlock> null_look_block = std::nullopt;
|
||||
auto it = m_players.find(HASH::str(name));
|
||||
if (it == m_players.end()) {
|
||||
LOG::error("Can't find player {}", name);
|
||||
CUBED_ASSERT(0);
|
||||
return null_pos;
|
||||
return null_look_block;
|
||||
}
|
||||
|
||||
return it->second.get_look_block_pos();
|
||||
@@ -265,6 +113,7 @@ void World::init_world() {
|
||||
}
|
||||
|
||||
void World::render() {
|
||||
|
||||
for (const auto& chunk_map : m_chunks) {
|
||||
const auto& [pos, chunk] = chunk_map;
|
||||
|
||||
@@ -325,6 +174,136 @@ bool World::is_block(const glm::ivec3& block_pos) const{
|
||||
}
|
||||
}
|
||||
|
||||
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
|
||||
int chunk_x, chunk_z;
|
||||
int world_x, world_y, world_z;
|
||||
world_x = block_pos.x;
|
||||
world_y = block_pos.y;
|
||||
world_z = block_pos.z;
|
||||
|
||||
if (world_x < 0) {
|
||||
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (world_x >= 0) {
|
||||
chunk_x = world_x / CHUCK_SIZE;
|
||||
}
|
||||
if (world_z < 0) {
|
||||
chunk_z = (world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (world_z >= 0) {
|
||||
chunk_z = world_z / CHUCK_SIZE;
|
||||
}
|
||||
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
|
||||
if (it == m_chunks.end()) {
|
||||
return ;
|
||||
}
|
||||
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - chunk_x * CHUCK_SIZE;
|
||||
z = world_z - chunk_z * CHUCK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= CHUCK_SIZE || z >= CHUCK_SIZE) {
|
||||
return ;
|
||||
}
|
||||
|
||||
it->second.set_chunk_block(Chunk::get_index(x, y, z), id);
|
||||
|
||||
|
||||
int adjacent_chunk_x;
|
||||
int adjacent_chunk_z;
|
||||
int adj_world_x;
|
||||
int adj_world_z;
|
||||
if (x == 0) {
|
||||
adj_world_x = world_x - 1;
|
||||
adj_world_z = world_z;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x / CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
adjacent->second.gen_vertex_data();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (x == CHUCK_SIZE - 1) {
|
||||
adj_world_x = world_x + 1;
|
||||
adj_world_z = world_z;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x/ CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
adjacent->second.gen_vertex_data();
|
||||
}
|
||||
}
|
||||
|
||||
if (z == 0) {
|
||||
adj_world_x = world_x;
|
||||
adj_world_z = world_z - 1;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x / CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
adjacent->second.gen_vertex_data();
|
||||
}
|
||||
}
|
||||
|
||||
if (z == CHUCK_SIZE - 1) {
|
||||
adj_world_x = world_x;
|
||||
adj_world_z = world_z + 1;
|
||||
if (adj_world_x < 0) {
|
||||
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_x >= 0) {
|
||||
adjacent_chunk_x = adj_world_x/ CHUCK_SIZE;
|
||||
}
|
||||
if (adj_world_z < 0) {
|
||||
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
if (adj_world_z >= 0) {
|
||||
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
|
||||
}
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
adjacent->second.gen_vertex_data();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void World::update(float delta_time) {
|
||||
for (auto& player : m_players) {
|
||||
player.second.update(delta_time);
|
||||
|
||||
Reference in New Issue
Block a user