载入中...
搜索中...
未找到
DamageNumbers.cpp
浏览该文件的文档.
1// eve-hot-path
3
4#include <algorithm>
5#include <cmath>
6#include <iomanip>
7#include <locale>
8#include <sstream>
9
10namespace eve::graphics {
11namespace {
12Result<void> invalid(std::string message) {
14 Diagnostic::error(DiagnosticCode::InvalidArgument, std::move(message), "graphics.damageNumbers"));
15}
17bool bounded(double value, double maximum) { return std::isfinite(value) && std::abs(value) <= maximum; }
18std::string text(const DamageNumberRequest& request, const DamageNumberStyle& style) {
19 if (request.kind == DamageNumberKind::Miss) return "MISS";
20 double value = request.amount;
21 std::string suffix;
22 if (style.compact && value >= 1000000.0) {
23 value /= 1000000.0;
24 suffix = "M";
25 } else if (style.compact && value >= 1000.0) {
26 value /= 1000.0;
27 suffix = "K";
28 }
29 std::ostringstream stream;
30 stream.imbue(std::locale::classic());
31 stream << (request.kind == DamageNumberKind::Heal ? "+" : "-") << std::fixed << std::setprecision(style.decimals)
32 << value << suffix;
33 if (request.kind == DamageNumberKind::Critical) stream << '!';
34 return stream.str();
35}
36} // namespace
37
38DamageNumbers::DamageNumbers() : impl_(std::make_unique<Impl>()) {
39 impl_->items.reserve(capacity());
40 auto& critical = impl_->styles[1];
41 critical.scale = 1.4f;
42 critical.shake = 4.f;
43 critical.g = 0.78f;
44 critical.b = 0.15f;
45 critical.lifetime = 1.35f;
46 critical.pop = 0.7f;
47 auto& heal = impl_->styles[2];
48 heal.r = 0.3f;
49 heal.g = 1.f;
50 heal.b = 0.6f;
51 heal.gravity = 0.f;
52 heal.velocityY = -65.f;
53 auto& miss = impl_->styles[3];
54 miss.r = 0.65f;
55 miss.g = 0.75f;
56 miss.b = 0.9f;
57 miss.scale = 0.8f;
58 miss.mergeWindow = 0.0;
59}
61
63 if (!validKind(kind) || !bounded(style.lifetime, 60.0) || style.lifetime <= 0.0 ||
64 !bounded(style.velocityY, 10000.0) || !bounded(style.gravity, 10000.0) || !bounded(style.scale, 20.0) ||
65 style.scale <= 0.f || !bounded(style.pop, 5.0) || style.pop < 0.f || !bounded(style.shake, 100.0) ||
66 style.shake < 0.f || !bounded(style.r, 1.0) || style.r < 0.f || !bounded(style.g, 1.0) || style.g < 0.f ||
67 !bounded(style.b, 1.0) || style.b < 0.f || !bounded(style.outline, 8.0) || style.outline < 0.f ||
68 !bounded(style.fadeIn, style.lifetime) || style.fadeIn < 0.0 || !bounded(style.fadeOut, style.lifetime) ||
69 style.fadeOut < 0.0 || !bounded(style.mergeWindow, style.lifetime) || style.mergeWindow < 0.0 ||
70 !bounded(style.mergeRadius, 4096.0) || style.mergeRadius < 0.f || style.decimals < 0 || style.decimals > 3)
71 return invalid("Invalid popup style");
72 impl_->styles[static_cast<std::size_t>(kind)] = style;
73 return Result<void>::success();
74}
76 if (!validKind(kind)) return Result<DamageNumberStyle>::failure(invalid("Invalid popup kind").status());
77 return Result<DamageNumberStyle>::success(impl_->styles[static_cast<std::size_t>(kind)]);
78}
80 if (!validKind(request.kind) || !bounded(request.amount, 1.0e12) || request.amount < 0.0 ||
81 !bounded(request.x, 1.0e7) || !bounded(request.y, 1.0e7) || !bounded(request.velocityX, 10000.0) ||
82 request.group.size() > 128)
83 return Result<DamageNumberAdmission>::failure(invalid("Invalid popup request").status());
84 const auto style = impl_->styles[static_cast<std::size_t>(request.kind)];
85 if (!request.group.empty() && request.kind != DamageNumberKind::Miss) {
86 for (auto& p : impl_->items) {
87 const double dx = static_cast<double>(p.request.x) - request.x;
88 const double dy = static_cast<double>(p.request.y) - request.y;
89 if (p.request.kind == request.kind && p.request.group == request.group && p.style.mergeWindow > 0.0 &&
90 p.age < p.style.mergeWindow && dx * dx + dy * dy <= p.style.mergeRadius * p.style.mergeRadius &&
91 p.request.amount + request.amount <= 1.0e12) {
92 auto merged = p.request;
93 merged.amount += request.amount;
94 auto label = text(merged, p.style);
95 p.request = std::move(merged);
96 p.text = std::move(label);
98 }
99 }
100 }
101 // Explicit deterministic lanes reduce exact overlap without a per-frame pairwise solver.
102 Impl::Popup popup{request, style, text(request, style), 0.0,
103 static_cast<float>(static_cast<int>(impl_->sequence % 5) - 2) * 7.f};
104 auto admission = DamageNumberAdmission::Spawned;
105 if (impl_->items.size() == capacity()) {
106 impl_->items.erase(impl_->items.begin());
108 }
109 impl_->items.push_back(std::move(popup));
110 impl_->sequence = (impl_->sequence + 1) % 5;
112}
114 if (!std::isfinite(seconds) || seconds < 0.0) return invalid("dt must be finite and nonnegative");
115 for (auto& p : impl_->items) p.age = std::min(static_cast<double>(p.style.lifetime), p.age + seconds);
116 std::erase_if(impl_->items, [](const auto& p) { return p.age >= p.style.lifetime; });
117 return Result<void>::success();
118}
120 impl_->items.clear();
121 impl_->sequence = 0;
122}
123std::size_t DamageNumbers::count() const { return impl_->items.size(); }
125 const auto& s = p.style;
126 const float t = static_cast<float>(p.age);
127 const double fadeIn = s.fadeIn > 0.0 ? std::min(1.0, p.age / s.fadeIn) : 1.0;
128 const double fadeOut = s.fadeOut > 0.0 ? std::min(1.0, (s.lifetime - p.age) / s.fadeOut) : 1.0;
129 const float pulse = std::max(0.f, 1.f - t / 0.18f);
130 const float shake = std::sin(t * 110.f) * s.shake * std::max(0.f, 1.f - t / 0.24f);
131 return {p.text,
132 p.request.x + p.lane + p.request.velocityX * t + shake,
133 p.request.y + s.velocityY * t + 0.5f * s.gravity * t * t,
134 s.scale * (1.f + s.pop * pulse),
135 static_cast<float>(fadeIn * fadeOut),
136 s.r,
137 s.g,
138 s.b,
139 s.outline};
140}
141std::vector<DamageNumberSample> DamageNumbers::snapshot() const {
142 std::vector<DamageNumberSample> samples;
143 samples.reserve(count());
144 for (const auto& p : impl_->items) samples.push_back(Impl::sample(p));
145 return samples;
146}
147} // namespace eve::graphics
const std::string & s
glm::vec4 p[6]
std::string message
float maximum[3]
const GltfImportRequest & request
wgpu::PopErrorScopeStatus status
std::string label
glm::dvec3 dx
Object::Ptr value
float t
std::string text
std::string kind
float dy
Definition Shadow.cpp:151
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
std::size_t count() const
Return live popup count.
Result< DamageNumberStyle > style(DamageNumberKind kind) const
Return an owning preset copy; invalid enum values fail.
DamageNumbers()
Construct a pool with four built-in styles and 256 reusable slots.
static constexpr std::size_t capacity()
Fixed maximum live popup budget; overflow replaces the oldest.
Result< DamageNumberAdmission > spawn(const DamageNumberRequest &request)
Validate and admit one popup. Failure leaves pool and sequence unchanged.
Result< void > update(double seconds)
Advance injected seconds; nonfinite/negative dt fails without mutation.
Result< void > setStyle(DamageNumberKind kind, const DamageNumberStyle &style)
Validate and replace one future-spawn style; existing entries keep their own copy.
std::vector< DamageNumberSample > snapshot() const
Build an independent owning frame snapshot for observation. @cost Linear allocations/copies in live e...
void clear()
Remove every transient entry; deterministic spawn sequence restarts.
Status invalid(const char *message)
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
DamageNumberKind
Presentation categories, independent of any combat or settlement module.
Owning display request. The caller supplies actual committed amounts, never pending damage.
Independent owning frame sample, useful for observation and alternative renderers.
Owning style copied into each popup at spawn. Coordinates are drawable pixels.
static DamageNumberSample sample(const Popup &popup)