summaryrefslogtreecommitdiffhomepage
path: root/OverScreen.cpp
blob: a4654642d45ee8ac69c4fed72332f278df3671a4 (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
#include "OverScreen.h"

#include <memory>

#include <raylib.h>

#include "Globals.h"
#include "Stats.h"
#include "TitleScreen.h"


static constexpr const char* DIED {"Game Over"};
static constexpr const char* LABEL {"Your Score: "};


static const char* points_text(std::shared_ptr<Stats> stats);


OverScreen::OverScreen(std::shared_ptr<Stats> stats) :
    m_stats {stats}
{
    const int screen_width = GetScreenWidth();
    const int died_width = MeasureText(DIED, 20);
    const int label_width = MeasureText(LABEL, 20);
    const int score_width = MeasureText(points_text(m_stats), 20);
    const int label_and_score = label_width + score_width;
    m_died_x = (screen_width - died_width) / 2;
    m_label_x = (screen_width - label_and_score) / 2;
    m_score_x = m_label_x + label_width;
    m_flash.start();
}


void
OverScreen::update(const float dt)
{
    m_flash.update(dt);
    if (IsKeyPressed(KEY_SPACE))
        g_game.set(std::make_unique<TitleScreen>());
}


void
OverScreen::draw()
{
    ClearBackground(BLACK);
    DrawText(DIED, m_died_x, 160, 20, RED);
    DrawText(LABEL, m_label_x, 180, 20, GRAY);
    DrawText(points_text(m_stats), m_score_x, 180, 20, GOLD);
    m_flash.draw();
}


const char*
points_text(std::shared_ptr<Stats> stats)
{
    if (stats->lifes > 0)
        return TextFormat("%d + %d", stats->points, 1000 * stats->lifes);
    return TextFormat("%d", stats->points);
}