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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user