summaryrefslogtreecommitdiffhomepage
path: root/View.cpp
blob: 0aa3b7be372840fe1f2c421769f60c9f91613f78 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#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_texture {LoadTexture("resources/wreck.png")},
    m_active {nullptr}
{
    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);
}


View::~View()
{
    UnloadTexture(m_texture);
}


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());
    auto mouse = GetMousePosition();
    for (const auto& wreck : wrecks) {
        const auto pos = GetWorldToScreen(wreck.position, m_camera);
        const auto base = GetWorldToScreen({wreck.position.x, 0.0f, wreck.position.z}, m_camera);
        if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y)
            if (0 > base.x || width < base.x || 0 > base.y || height < base.y)
                continue;
        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));
        const float length =
            std::sqrt(
                std::pow(pos.x - base.x, 2) +
                std::pow(pos.y - base.y, 2));
        const bool hover =
            8.0f > std::sqrt(
                std::pow(mouse.x - pos.x, 2) +
                std::pow(mouse.y - pos.y, 2));
        m_labels.push_back(Label{pos, base, depth, length, hover});
    }
    std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; });
    m_active = nullptr;
    for (auto& label : m_labels) {
        if (label.hover) {
            if (m_active) {
                if (label.depth < m_active->depth)
                    m_active = &label;
            }
            else
                m_active = &label;
            label.hover = false;
        }
    }
    if (m_active)
        m_active->hover = true;
}


void
View::draw() const
{
    BeginDrawing();
    ClearBackground(RAYWHITE);
    BeginMode3D(m_camera);
    DrawGrid(12, 1.0f);
    EndMode3D();
    for (const auto& point : m_labels) {
        if (point.length > 8)
            DrawLine(point.base.x, point.base.y, point.pos.x, point.pos.y, point.hover ? ORANGE : DARKGRAY);
        DrawTexture(m_texture, point.pos.x - 8, point.pos.y - 8, point.hover ? ORANGE : WHITE);
    }
    DrawFPS(5, 5);
    DrawText(TextFormat("%d", m_labels.size()), 5, 25, 20, DARKGRAY);
    EndDrawing();
}