nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
combat_text.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4#include <raymath.h>
5
6#include <algorithm>
7#include <cfloat>
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "absl/strings/substitute.h"
13#include "client/nameplates.h"
14#include "client/translations.h"
15
16namespace nanommo {
17
18namespace {
19
20constexpr Color kHit = {255, 255, 255, 255};
21constexpr Color kHurt = {235, 70, 60, 255};
22constexpr Color kHeal = {90, 220, 90, 255};
23constexpr Color kLevel = {255, 205, 60, 255};
24
25// The others' are smaller, so that your own fight stands out in a crowd.
26constexpr float kTheirs = 0.7f;
27constexpr float kLevelScale = 1.5f;
28
29// In metres: where a popup starts, over the figure's nameplate, and how far
30// it rises over its life.
31constexpr float kStart = 0.4f;
32constexpr float kRise = 1.2f;
33
34} // namespace
35
36void CombatText::Add(const protocol::Event& event, uint32_t you) {
37 Popup popup{.unit = event.player(),
38 .scale = event.player() == you ? 1.0f : kTheirs};
39 switch (event.kind()) {
40 case protocol::EVENT_HIT:
41 popup.text = std::to_string(event.amount());
42 popup.unit = event.monster();
43 popup.monster = true;
44 popup.color = kHit;
45 break;
46 case protocol::EVENT_HURT:
47 popup.text = "-" + std::to_string(event.amount());
48 popup.color = kHurt;
49 break;
50 case protocol::EVENT_HEAL:
51 popup.text = "+" + std::to_string(event.amount());
52 popup.color = kHeal;
53 break;
54 case protocol::EVENT_LEVEL:
55 popup.text = absl::Substitute(Tr("Level $0"), event.amount());
56 popup.color = kLevel;
57 popup.scale *= kLevelScale;
58 break;
59 default:
60 // A kind from a newer server, which this client can't show.
61 return;
62 }
63 popups_.push_back(std::move(popup));
64}
65
66void CombatText::Update(float seconds) {
67 for (Popup& popup : popups_) {
68 popup.age += seconds;
69 }
70 std::erase_if(popups_, [](const Popup& popup) { return popup.age >= kLife; });
71}
72
73void DrawCombatText(const CombatText& text, const std::vector<Figure>& figures,
74 const Camera3D& camera) {
75 ImDrawList* draw = ImGui::GetBackgroundDrawList();
76 ImFont* font = ImGui::GetFont();
77 const Vector3 forward =
78 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
79 for (const Popup& popup : text.popups()) {
80 const auto figure =
81 std::ranges::find_if(figures, [&popup](const Figure& figure) {
82 return figure.id == popup.unit && figure.monster == popup.monster &&
83 !figure.npc;
84 });
85 if (figure == figures.end()) {
86 continue;
87 }
88 const float share = popup.age / CombatText::kLife;
89 const Vector3 over{
90 figure->position.x,
91 figure->position.y + Above(*figure) + kStart + kRise * share,
92 figure->position.z};
93 // Behind the camera, GetWorldToScreen would put it on the screen.
94 if (Vector3DotProduct(Vector3Subtract(over, camera.position), forward) <=
95 0.0f) {
96 continue;
97 }
98 const Vector2 at = GetWorldToScreen(over, camera);
99 // Each fades out over the second half of its life.
100 const float alpha = std::clamp(2.0f * (1.0f - share), 0.0f, 1.0f);
101 const float size = ImGui::GetFontSize() * 2.4f * popup.scale;
102 const ImVec2 extent =
103 font->CalcTextSizeA(size, FLT_MAX, 0.0f, popup.text.c_str());
104 const ImVec2 corner{at.x - extent.x / 2, at.y - extent.y};
105 draw->AddText(font, size, {corner.x + 2, corner.y + 2},
106 IM_COL32(0, 0, 0, static_cast<int>(200 * alpha)),
107 popup.text.c_str());
108 draw->AddText(font, size, corner,
109 IM_COL32(popup.color.r, popup.color.g, popup.color.b,
110 static_cast<int>(255 * alpha)),
111 popup.text.c_str());
112 }
113}
114
115} // namespace nanommo
const std::vector< Popup > & popups() const
Definition combat_text.h:42
static constexpr float kLife
In seconds.
Definition combat_text.h:34
void Add(const protocol::Event &event, uint32_t you)
Adds what the event shows, to the player whose id is you.
void Update(float seconds)
Ages each by the frame's time, and drops the ones past their life.
Definition chat.cc:9
const char * Tr(const char *text)
float Above(const Figure &figure)
In metres above a figure's feet, where its nameplate goes.
Definition nameplates.cc:87
void DrawCombatText(const CombatText &text, const std::vector< Figure > &figures, const Camera3D &camera)
Draws each over the figure it's about, rising and fading as it ages.
uint32_t id
Definition animator.h:21
Vector3 position
Definition animator.h:63
float scale
What's about you is bigger than what's about the others.
Definition combat_text.h:24
uint32_t unit
The unit it rises over, by its id: a monster's, or else a player's.
Definition combat_text.h:20
float age
In seconds, since it came.
Definition combat_text.h:26
std::string text
Definition combat_text.h:18