diff options
| author | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 18:05:18 +1100 |
|---|---|---|
| committer | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 18:05:18 +1100 |
| commit | 1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (patch) | |
| tree | 222dfcd07a1e40716127a347bbfd7119ce3d0984 /src/client/player.cc | |
initial commit
Diffstat (limited to 'src/client/player.cc')
| -rw-r--r-- | src/client/player.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/client/player.cc b/src/client/player.cc new file mode 100644 index 0000000..5ff4118 --- /dev/null +++ b/src/client/player.cc @@ -0,0 +1,58 @@ +#include "player.hh" + +glm::vec3 client::player::get_world_pos(const shared::player& lp) noexcept { + // clang-format off + const float world_x = static_cast<float>(this->chunk_pos.x - lp.chunk_pos.x) * shared::world::chunk::WIDTH + this->local_pos.x; + const float world_y = static_cast<float>(this->local_pos.y) + shared::player::HEIGHT / 2.0f; + const float world_z = static_cast<float>(this->chunk_pos.z - lp.chunk_pos.z) * shared::world::chunk::WIDTH + this->local_pos.z; + // clang-format on + return {world_x, world_y, world_z}; +} + +void client::player::draw_playermodel( + const shared::player& localplayer) noexcept { + + static client::render::model playermodel{"res/models/player/player.obj"}; + static client::render::program program{"res/shaders/model.vs", + "res/shaders/model.fs"}; + + const glm::vec3 world_pos = this->get_world_pos(localplayer); + const glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), -this->viewangles.yaw, + glm::vec3(0.0f, 1.0f, 0.0f)); + const glm::mat4 translate = glm::translate(glm::mat4(1.0f), world_pos); + const glm::mat4& view = client::render::camera::get_view(); + const glm::mat4& proj = client::render::camera::get_proj(); + + playermodel.draw(program, proj * view * translate * rotate); +} + +void client::player::draw_message(const shared::player& localplayer) noexcept { + // Clear the message if it's too old. + if (this->message.has_value()) { + const auto now = std::chrono::steady_clock::now(); + if (now > this->message->receive_time + player::MSG_SHOW_TIME) { + this->message.reset(); + } + } + + if (!this->message.has_value()) { + return; + } + + const glm::vec3 world_pos = this->get_world_pos(localplayer); + const auto pos = client::math::world_to_screen(world_pos); + + if (!pos.has_value()) { + return; + } + + const glm::vec2& window = client::render::get_window_size(); + const glm::mat4 proj = glm::ortho(0.0f, window.x, 0.0f, window.y); + const glm::mat4 tran = + glm::translate(glm::mat4(1.0f), + glm::vec3{std::floor(pos->x), std::floor(pos->y), 0.0f}); + + constexpr glm::vec4 white{1.0f, 1.0f, 1.0f, 1.0f}; + client::render::render_text(this->message->text, 30.0f, true, true, white, + proj * tran); +} |
