aboutsummaryrefslogtreecommitdiff
path: root/src/client/entity/player.cc
blob: 550968f28f898e67d2a7c3a7e50ce43362c88003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "player.hh"
namespace client {

glm::vec3 player::get_world_pos(const client::player& lp) noexcept {
    const auto& lp_cp = lp.get_chunk_pos();

    // clang-format off
    const float world_x = static_cast<float>(this->get_chunk_pos().x - lp_cp.x) * shared::world::chunk::WIDTH + this->local_pos.x;
    const float world_y = static_cast<float>(this->get_local_pos().y) + shared::player::HEIGHT / 2.0f;
    const float world_z = static_cast<float>(this->get_chunk_pos().z - lp_cp.z) * shared::world::chunk::WIDTH + this->local_pos.z;
    // clang-format on
    return {world_x, world_y, world_z};
}

void player::draw(const client::player& localplayer) noexcept {

    static render::model playermodel{"res/models/player/player.obj"};
    static 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 = render::camera::get_view();
    const glm::mat4& proj = render::camera::get_proj();

    playermodel.draw(program, proj * view * translate * rotate);
}

void player::draw_wts(const client::player& localplayer) noexcept {
    const auto& msg = this->get_message();
    if (!msg.has_value()) {
        return;
    }

    const glm::vec3 world_pos = this->get_world_pos(localplayer);
    const auto pos = math::world_to_screen(world_pos);

    if (!pos.has_value()) {
        return;
    }

    const glm::vec2& window = 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};
    render::render_text(msg->text, 30.0f, true, true, white, proj * tran);
}

} // namespace client