aboutsummaryrefslogtreecommitdiff
path: root/src/client/player.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/player.cc')
-rw-r--r--src/client/player.cc58
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);
+}