aboutsummaryrefslogtreecommitdiff
path: root/src/client/player.cc
blob: 5ff41180d06557e3252374f0b46cdb50eb1e493e (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
55
56
57
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);
}