summaryrefslogtreecommitdiffhomepage
path: root/View.cpp
blob: 77ab271eae0bac82e3773e13133a9c323ef24b60 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "View.h"

#include <algorithm>
#include <cmath>

#include <raylib.h>

#include "Grid.h"
#include "Label.h"


View::View(std::vector<Grid> grids) :
    m_camera {},
    m_grids {grids},
    m_labels {},
    m_grid {0}
{
    m_camera.position = Vector3{10.0f, 10.0f, 10.0f};
    m_camera.target = Vector3{0.0f, 0.0f, 0.0f};
    m_camera.up = Vector3{0.0f, 1.0f, 0.0f};
    m_camera.fovy = 45;
    m_camera.projection = CAMERA_PERSPECTIVE;
    SetCameraMode(m_camera, CAMERA_ORBITAL);
}


void
View::update(const float dt)
{
    if (IsKeyPressed(KEY_SPACE)) {
        m_grid++;
        if (m_grid >= m_grids.size())
            m_grid = 0;
    }
    UpdateCamera(&m_camera);
    const int height = GetScreenHeight();
    const int width = GetScreenWidth();
    const auto wrecks = m_grids.at(m_grid).wrecks;
    m_labels.clear();
    m_labels.reserve(wrecks.size());
    for (const auto& wreck : wrecks) {
        const auto pos = GetWorldToScreen(wreck.position, m_camera);
        const float depth =
            std::sqrt(
                std::pow(m_camera.position.x - wreck.position.x, 2) +
                std::pow(m_camera.position.y - wreck.position.y, 2) +
                std::pow(m_camera.position.z - wreck.position.z, 2));
        if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y)
            continue;
        const auto base = GetWorldToScreen({wreck.position.x, 0.0f, wreck.position.z}, m_camera);
        m_labels.push_back(Label{pos, base, depth});
    }
    std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; });
}


void
View::draw() const
{
    BeginDrawing();
    ClearBackground(RAYWHITE);
    BeginMode3D(m_camera);
    DrawGrid(12, 1.0f);
    EndMode3D();
    for (const auto& point : m_labels) {
        DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, LIGHTGRAY);
        DrawCircle(point.pos.x, point.pos.y, 3, RED);
    }
    DrawFPS(5, 5);
    int y = 25;
    const int height = GetScreenHeight();
    for (const auto& point : m_labels) {
        DrawText(TextFormat("%5.1f x %5.1f x %5.1f", point.pos.x, point.pos.y, point.depth), 5, y, 10, DARKGRAY);
        y += 10;
        if (y > height)
            break;
    }
    EndDrawing();
}