mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-09 21:54:09 +08:00
fix: outline not disappearing when no block is targeted
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
struct MoveState {
|
||||
bool forward = false;
|
||||
@@ -30,6 +31,7 @@ private:
|
||||
glm::vec3 m_right;
|
||||
MoveState m_move_state;
|
||||
|
||||
std::optional<glm::ivec3> m_look_block_pos = std::nullopt;
|
||||
std::string m_name;
|
||||
World& m_world;
|
||||
|
||||
@@ -39,6 +41,7 @@ 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 glm::vec3& get_player_pos() const;
|
||||
const MoveState& get_move_state() const;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
@@ -8,7 +9,6 @@ class Player;
|
||||
class World {
|
||||
private:
|
||||
BlockRenderData m_block_render_data;
|
||||
glm::ivec3 last_block_pos = glm::ivec3(0, 0, 0);
|
||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
||||
std::unordered_map<std::size_t, Player> m_players;
|
||||
public:
|
||||
@@ -17,11 +17,10 @@ public:
|
||||
~World();
|
||||
|
||||
const BlockRenderData& get_block_render_data(int x, int y ,int z);
|
||||
const glm::ivec3& get_last_block_pos() const;
|
||||
const std::optional<glm::ivec3>& 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 mark_looked_block(const glm::ivec3& block_pos);
|
||||
void render();
|
||||
void update(float delta_time);
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@ const glm::vec3& Player::get_front() const {
|
||||
return m_front;
|
||||
}
|
||||
|
||||
const std::optional<glm::ivec3>& Player::get_look_block_pos() const {
|
||||
return m_look_block_pos;
|
||||
}
|
||||
|
||||
const glm::vec3& Player::get_player_pos() const {
|
||||
return m_player_pos;
|
||||
}
|
||||
@@ -68,7 +72,9 @@ void Player::update(float delta_time) {
|
||||
// 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);
|
||||
m_look_block_pos = std::move(block_pos);
|
||||
} else {
|
||||
m_look_block_pos = std::nullopt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -211,8 +211,17 @@ 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;
|
||||
const std::optional<glm::ivec3>& World::get_look_block_pos(const std::string& name) const{
|
||||
static std::optional<glm::ivec3> null_pos = 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 it->second.get_look_block_pos();
|
||||
|
||||
}
|
||||
|
||||
Player& World::get_player(const std::string& name){
|
||||
@@ -255,13 +264,6 @@ 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;
|
||||
|
||||
29
src/main.cpp
29
src/main.cpp
@@ -122,23 +122,22 @@ void display(GLFWwindow* window, double current_time) {
|
||||
glUseProgram(outline_program);
|
||||
mv_loc = glGetUniformLocation(outline_program, "mv_matrix");
|
||||
proj_loc = glGetUniformLocation(outline_program, "proj_matrix");
|
||||
const auto& block_pos = world.get_look_block_pos("TestPlayer");
|
||||
if (block_pos != std::nullopt) {
|
||||
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(block_pos.value()));
|
||||
mv_mat = v_mat * m_mat;
|
||||
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||
|
||||
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(world.get_last_block_pos()));
|
||||
mv_mat = v_mat * m_mat;
|
||||
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, outline_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, outline_indices_vbo);
|
||||
glLineWidth(5.0f);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, outline_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, outline_indices_vbo);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user