nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
boss_bar.cc
Go to the documentation of this file.
1#include "client/boss_bar.h"
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cfloat>
7#include <cmath>
8#include <cstdint>
9#include <string>
10#include <vector>
11
12#include "absl/strings/str_cat.h"
13#include "absl/strings/substitute.h"
14#include "client/theme.h"
15#include "client/translations.h"
16
17namespace nanommo {
18
19namespace {
20
21// In points, at a scale of one: from the top of the screen, under the
22// target's frame, and the bar's size.
23constexpr float kTop = 146.0f;
24constexpr float kWidth = 520.0f;
25constexpr float kHeight = 18.0f;
26
27} // namespace
28
29const Figure* ShownBoss(const std::vector<Figure>& figures, uint32_t target) {
30 const auto you = std::ranges::find_if(figures, &Figure::you);
31 const Figure* shown = nullptr;
32 float nearest = kBossBarReach;
33 for (const Figure& figure : figures) {
34 if (!figure.monster || figure.phase == 0 ||
35 figure.to == protocol::MOTION_DEAD) {
36 continue;
37 }
38 if (figure.id == target) {
39 return &figure;
40 }
41 if (you == figures.end()) {
42 continue;
43 }
44 const float away = std::hypot(figure.position.x - you->position.x,
45 figure.position.z - you->position.z);
46 if (away <= nearest) {
47 shown = &figure;
48 nearest = away;
49 }
50 }
51 return shown;
52}
53
54void DrawBossBar(const Figure& boss, const MonsterTable& monsters) {
55 ImDrawList* draw = ImGui::GetBackgroundDrawList();
56 const float width = Ui(kWidth);
57 const float left = (ImGui::GetIO().DisplaySize.x - width) / 2.0f;
58 const float top = Ui(kTop);
59 DrawTitle(draw, {left + width / 2.0f, top}, Ui(20.0f), boss.name.c_str());
60
61 const ImVec2 min{left, top + Ui(28.0f)};
62 const ImVec2 max{left + width, min.y + Ui(kHeight)};
63 const float share = boss.max_health > 0
64 ? static_cast<float>(boss.health) /
65 static_cast<float>(boss.max_health)
66 : 0.0f;
67 const std::string health = absl::StrCat(boss.health, " / ", boss.max_health);
68 DrawBar(draw, min, max, share, colors::kHealth, colors::kHealthDark,
69 health.c_str());
70
71 // Where each phase after the first starts, so that everyone can see the
72 // next coming.
73 int phases = static_cast<int>(boss.phase);
74 if (const auto kind = monsters.kinds.find(boss.kind);
75 kind != monsters.kinds.end()) {
76 phases = kind->second.phases_size();
77 for (int i = 1; i < phases; ++i) {
78 const float from =
79 static_cast<float>(kind->second.phases(i).health()) / 100.0f;
80 const float x = left + width * from;
81 draw->AddLine({x, min.y - Ui(3.0f)}, {x, max.y + Ui(3.0f)},
82 colors::kGoldLight, std::max(1.0f, Ui(2.0f)));
83 }
84 }
85
86 const std::string phase =
87 absl::Substitute(Tr("Phase $0 of $1"), boss.phase, phases);
88 ImFont* bold = GetFonts().bold;
89 const ImVec2 extent =
90 bold->CalcTextSizeA(Ui(13.0f), FLT_MAX, 0.0f, phase.c_str());
91 DrawShadowedText(draw, bold, Ui(13.0f),
92 {left + (width - extent.x) / 2.0f, max.y + Ui(5.0f)},
93 colors::kMuted, phase.c_str());
94}
95
96} // namespace nanommo
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kMuted
Definition theme.h:27
constexpr ImU32 kHealth
Definition theme.h:30
constexpr ImU32 kHealthDark
Definition theme.h:31
Definition chat.cc:9
void DrawBossBar(const Figure &boss, const MonsterTable &monsters)
Definition boss_bar.cc:54
constexpr float kBossBarReach
Definition boss_bar.h:14
void DrawBar(ImDrawList *draw, ImVec2 min, ImVec2 max, float share, ImU32 color, ImU32 dark, const char *text)
Definition theme.cc:452
const Figure * ShownBoss(const std::vector< Figure > &figures, uint32_t target)
Definition boss_bar.cc:29
float Ui(float points)
Definition theme.h:67
const char * Tr(const char *text)
void DrawTitle(ImDrawList *draw, ImVec2 top_centre, float size, const char *text)
A title in carved gold capitals, centred on the point at its top.
Definition theme.cc:299
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
Definition theme.cc:272
const Fonts & GetFonts()
Definition theme.cc:66
uint32_t kind
A monster's or an NPC's kind, by its id in the content folder.
Definition animator.h:33
std::string name
Definition animator.h:22
uint32_t phase
A boss's phase, from one, or 0 for any other monster.
Definition animator.h:52
ImFont * bold
Definition theme.h:43
The kinds of monster, by id, and where they spawn.
Definition monsters.h:17
std::map< uint32_t, monsters::Kind > kinds
Definition monsters.h:18