mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add outline for block that is looked
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/gameplay/world.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
|
||||
Player::Player(const World& world, const std::string& name) :
|
||||
Player::Player(World& world, const std::string& name) :
|
||||
m_world(world),
|
||||
m_name(name)
|
||||
{
|
||||
@@ -23,6 +25,20 @@ const MoveState& Player::get_move_state() const {
|
||||
return m_move_state;
|
||||
}
|
||||
|
||||
bool Player::ray_cast(const glm::vec3& start, const glm::vec3& dir,glm::ivec3& block_pos, float distance) {
|
||||
float step = 0.1f;
|
||||
glm::ivec3 cur = glm::floor(start);
|
||||
for (float t = 0.0f; t < distance; t += step) {
|
||||
glm::vec3 point = start + dir * t;
|
||||
block_pos = glm::floor(point);
|
||||
if (m_world.is_block(block_pos)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Player::set_player_pos(const glm::vec3& pos) {
|
||||
m_player_pos = pos;
|
||||
}
|
||||
@@ -49,6 +65,11 @@ void Player::update(float delta_time) {
|
||||
if (m_move_state.down) {
|
||||
m_player_pos -= glm::vec3(0.0f, 1.0f, 0.0f) * speed;
|
||||
}
|
||||
// calculate the block that is looked
|
||||
glm::ivec3 block_pos;
|
||||
if(ray_cast(glm::vec3(m_player_pos.x + 0.5f, (m_player_pos.y + 1.0f), m_player_pos.z + 0.5f), m_front, block_pos)) {
|
||||
m_world.mark_looked_block(block_pos);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ World::~World() {
|
||||
}
|
||||
|
||||
const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) {
|
||||
static int chunk_x, chunk_z;
|
||||
int chunk_x, chunk_z;
|
||||
if (world_x < 0) {
|
||||
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
|
||||
}
|
||||
@@ -211,6 +211,10 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
|
||||
return m_block_render_data;
|
||||
}
|
||||
|
||||
const glm::ivec3& World::get_last_block_pos() const {
|
||||
return last_block_pos;
|
||||
}
|
||||
|
||||
Player& World::get_player(const std::string& name){
|
||||
auto it = m_players.find(HASH::str(name));
|
||||
if (it == m_players.end()) {
|
||||
@@ -251,6 +255,13 @@ void World::init_world() {
|
||||
m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer"));
|
||||
}
|
||||
|
||||
void World::mark_looked_block(const glm::ivec3& block_pos) {
|
||||
if (last_block_pos == block_pos) {
|
||||
return;
|
||||
}
|
||||
last_block_pos = block_pos;
|
||||
}
|
||||
|
||||
void World::render() {
|
||||
for (const auto& chunk_map : m_chunks) {
|
||||
const auto& [pos, chunk] = chunk_map;
|
||||
@@ -266,7 +277,49 @@ void World::render() {
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size() * 3);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
//LOG::info("Chunk {} {} render finished", pos.x, pos.z);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool World::is_block(const glm::ivec3& block_pos) const{
|
||||
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 false;
|
||||
}
|
||||
|
||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||
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 false;
|
||||
}
|
||||
auto id = chunk_blocks[Chunk::get_index(x, y, z)];
|
||||
if (id == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user