nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
death_screen.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cstdint>
7#include <string>
8
9#include "absl/strings/substitute.h"
10#include "client/theme.h"
11#include "client/translations.h"
12
13namespace nanommo {
14
15namespace {
16
17// A frame that took long counts as a short one, as for the loading screen.
18constexpr float kLongestFrame = 1.0f / 30.0f;
19
20constexpr ImVec2 kButton{260.0f, 50.0f};
21
22} // namespace
23
24void DeathScreen::Update(float seconds, bool dead) {
25 const float time = std::min(seconds, kLongestFrame);
26 if (dead) {
27 dark_ = std::min(dark_ + time * kDim / kDarken, kDim);
28 } else if (was_dead_) {
29 // Back in the middle: from all dark, so that the jump there is hidden.
30 dark_ = 1.0f;
31 } else {
32 dark_ = std::max(dark_ - time / kReturn, 0.0f);
33 }
34 was_dead_ = dead;
35}
36
37bool DeathScreen::Build(bool dead, uint32_t wait_ms) const {
38 if (dark_ > 0.0f) {
39 ImGui::GetBackgroundDrawList()->AddRectFilled(
40 {0.0f, 0.0f}, ImGui::GetIO().DisplaySize,
41 IM_COL32(0, 0, 0, static_cast<int>(dark_ * 255.0f)));
42 }
43 if (!dead) {
44 return false;
45 }
46 const ImVec2 screen = ImGui::GetIO().DisplaySize;
47 ImDrawList* draw = ImGui::GetBackgroundDrawList();
48 // A red that comes in from the edges.
49 const float edge = std::min(screen.x, screen.y) * 0.3f;
50 const ImU32 red = Faded(colors::kWine, 0.7f);
51 const ImU32 clear = Faded(colors::kWine, 0.0f);
52 draw->AddRectFilledMultiColor({0.0f, 0.0f}, {edge, screen.y}, red, clear,
53 clear, red);
54 draw->AddRectFilledMultiColor({screen.x - edge, 0.0f}, screen, clear, red,
55 red, clear);
56 ImFont* title = GetFonts().title;
57 const char* words = Tr("You died");
58 const float size = Ui(72.0f);
59 const ImVec2 extent = title->CalcTextSizeA(size, FLT_MAX, 0.0f, words);
60 const float top = screen.y * 0.2f;
61 DrawGradientText(draw, title, size, {(screen.x - extent.x) / 2.0f, top},
62 IM_COL32(255, 120, 100, 255), colors::kCrimson, words);
63 DrawDivider(draw, {screen.x / 2.0f - extent.x * 0.6f, top + extent.y},
64 extent.x * 1.2f);
65
66 ImGui::SetNextWindowPos({screen.x / 2.0f, top + extent.y + Ui(30.0f)},
67 ImGuiCond_Always, {0.5f, 0.0f});
68 ImGui::Begin("Dead", nullptr,
69 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground |
70 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
71 ImGuiWindowFlags_NoSavedSettings);
72 // Whole seconds, rounded up, so that it says 1 until it's time.
73 const uint32_t seconds = (wait_ms + 999) / 1000;
74 const bool ready = wait_ms == 0;
75 // GameButton translates Respawn, and what's already in the player's language
76 // stays as it is.
77 const std::string label =
78 ready ? N_("Respawn") : absl::Substitute(Tr("Respawn in $0"), seconds);
79 const bool clicked = GameButton(label.c_str(), Ui(kButton), true, ready);
80 ImGui::End();
81 return clicked && ready;
82}
83
84} // namespace nanommo
static constexpr float kDarken
In seconds: how long the world takes to darken, and to come back.
void Update(float seconds, bool dead)
Moves on by the frame's time, from whether the player's dead.
static constexpr float kDim
How dark the world gets while they're dead, from 0 to 1.
bool Build(bool dead, uint32_t wait_ms) const
static constexpr float kReturn
constexpr ImU32 kCrimson
Definition theme.h:17
constexpr ImU32 kWine
Definition theme.h:16
Definition chat.cc:9
constexpr const char * N_(const char *text)
void DrawDivider(ImDrawList *draw, ImVec2 left, float width)
Definition theme.cc:257
float Ui(float points)
Definition theme.h:67
const char * Tr(const char *text)
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
void DrawGradientText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 top, ImU32 bottom, const char *text)
Definition theme.cc:283
const Fonts & GetFonts()
Definition theme.cc:66
ImFont * title
Definition theme.h:44