35struct RenderTransform {
42RenderTransform gRenderTransform;
44glm::vec2 transformedCenter(
float x,
float y,
float w,
float h) {
46 if (!gRenderTransform.active || std::abs(gRenderTransform.degrees) < 0.001f)
return center;
47 const float rad = gRenderTransform.degrees * 3.14159265358979323846f / 180.f;
48 const float c = std::cos(rad);
49 const float s = std::sin(rad);
50 const float dx =
center.x - gRenderTransform.cx;
51 const float dy =
center.y - gRenderTransform.cy;
52 return {gRenderTransform.cx +
dx *
c -
dy *
s, gRenderTransform.cy +
dx *
s +
dy *
c};
55float clampf(
float v,
float lo,
float hi) {
return v < lo ? lo : (
v > hi ? hi :
v); }
56float lerpf(
float a,
float b,
float t) {
return a + (
b -
a) *
t; }
59float frameRateK(
float k,
float dt) {
60 if (
dt <= 0.f)
return k;
61 const float u = 1.f - k;
62 const float n =
dt * 60.f;
63 const int whole =
static_cast<int>(
n);
64 const float frac =
n -
static_cast<float>(whole);
66 for (
int i = 0; i < whole; ++i)
r *=
u;
67 r *= (1.f + frac * (
u - 1.f));
71constexpr float kPi = 3.14159265358979323846f;
76 pose.progress = clampf(flipProgress, 0.f, 1.f);
77 pose.angleRadians =
pose.progress * kPi;
78 pose.angleDegrees =
pose.progress * 180.f;
79 pose.lift = std::sin(
pose.angleRadians) * liftPeak;
80 pose.axisScale = std::fabs(std::cos(
pose.angleRadians));
81 pose.showingFront =
pose.progress < 0.5f;
86 const float target = clampf(targetFaceDown, 0.f, 1.f);
87 const float k = frameRateK(clampf(speed, 0.f, 1.f),
dt);
88 flip = lerpf(flip,
target, k);
93 if (handFaceDown)
return handPeek;
100 const glm::vec4 &
color) {
101 if (!gRenderTransform.active || std::abs(
angle) < 0.001f)
104 const glm::vec2
center = transformedCenter(
x,
y,
w,
h);
109void drawTexture(graphics::Graphics *gfx, graphics::Texture *
texture,
float x,
float y,
float w,
float h,
111 if (!gRenderTransform.active || std::abs(
angle) < 0.001f) {
114 const glm::vec2
center = transformedCenter(
x,
y,
w,
h);
120void drawGem(graphics::Graphics *gfx,
float x,
float y,
float size,
float angle,
121 const glm::vec3 &rgb,
float a) {
123 const float p =
size * 0.20f;
142 c->definitionBinding();
147 const auto *L =
layout().operator->();
148 if (L->w <= 0.f || L->h <= 0.f)
return false;
149 const float cw = L->w * L->scale;
150 const float ch = L->h * L->scale;
151 return px >= L->x - cw * 0.5f &&
px <= L->x + cw * 0.5f &&
py >= L->y - ch * 0.5f &&
152 py <= L->y + ch * 0.5f;
156 const auto *I =
identity().operator->();
158 if (!projected) projected.ignore(
"card description retained legacy projection after attribute failure");
159 const auto *S =
stats().operator->();
160 if (I->kind ==
"spell")
return I->name +
"(法术 · 费用 " + std::to_string(S->cost) +
")";
161 return I->name +
"(" + std::to_string(S->attack) +
"/" + std::to_string(S->health) +
" · 费用 " +
162 std::to_string(S->cost) +
")";
170 Deck *
d = Deck::create();
176 auto &cards = membership()->cards;
177 if (cards.empty())
return nullptr;
184 auto &cards = membership()->cards;
185 return cards.empty() ? nullptr : cards.back();
189 auto &cards = membership()->cards;
190 if (
index < 0 ||
static_cast<size_t>(
index) >= cards.size())
return nullptr;
191 return cards[
static_cast<size_t>(
index)];
195 static thread_local std::mt19937 rng(std::random_device{}());
196 std::shuffle(membership()->cards.begin(), membership()->cards.end(), rng);
204 Zone *
z = Zone::create();
211 const auto *R = rect().operator->();
212 return px >= R->x &&
px <= R->x + R->w &&
py >= R->y &&
py <= R->y + R->h;
216 const auto *R = rect().operator->();
217 if (!R->enabled || !card)
return false;
218 const auto &kinds =
filter()->acceptKinds;
219 if (kinds.empty())
return true;
220 const std::string &
kind = card->identity()->kind;
221 for (
const auto &k : kinds)
222 if (k ==
kind)
return true;
227 auto *R = rect().operator->();
228 gfx->
drawSolidRect(R->x, R->y, R->w, R->h, glm::vec4(R->color, R->alpha));
229 if (showLabel && gfx->
getFont() && !R->label.empty()) {
230 const glm::vec4
tint(clampf(R->color.r * 0.9f + 0.3f, 0.f, 1.f),
231 clampf(R->color.g * 0.9f + 0.3f, 0.f, 1.f),
232 clampf(R->color.b * 0.9f + 0.3f, 0.f, 1.f), 1.f);
233 printCentered(gfx, R->label, R->x + R->w * 0.5f, R->y + R->h * 0.5f, 0.9f,
tint);
242 const glm::vec4 &
color) {
246 const float th = font->getHeight() *
scale;
252 const RenderTransform
previous = gRenderTransform;
253 gRenderTransform = {
x +
w * 0.5f,
y +
h * 0.5f,
angle,
true};
257 glm::vec4(0.9f, 0.85f, 0.95f,
a));
258 const float i2 =
w * 0.10f;
260 glm::vec4(0.11f, 0.08f, 0.22f,
a));
261 const float i1 =
w * 0.05f;
263 glm::vec4(0.30f, 0.17f, 0.45f,
a));
264 drawSolid(gfx,
x,
y,
w,
h,
angle, glm::vec4(0.06f, 0.06f, 0.10f,
a));
272 projected.ignore(
"card renderer skipped an invalid attribute projection");
275 const auto *L = card.layout().operator->();
276 const auto *V = card.visual().operator->();
277 const auto *I = card.identity().operator->();
278 const auto *S = card.stats().operator->();
281 const bool showBack = back || !
pose.showingFront;
282 const float baseW = L->w * L->scale;
283 const float baseH = L->h * L->scale;
284 const float w = (cfg.
flipAxis == 0) ? baseW *
pose.axisScale : baseW;
285 const float h = (cfg.
flipAxis == 1) ? baseH *
pose.axisScale : baseH;
286 const float cx = L->x;
287 const float cy = L->y -
pose.lift;
288 const float x =
cx -
w * 0.5f;
289 const float y =
cy -
h * 0.5f;
290 const float a = L->alpha;
292 const RenderTransform
previous = gRenderTransform;
293 gRenderTransform = {
cx,
cy, L->angle,
true};
302 drawSolid(gfx,
x,
y,
w,
h, L->angle, glm::vec4(0.07f, 0.07f, 0.09f,
a));
303 const float inset =
w * 0.035f;
304 const float ix =
x + inset;
305 const float iy =
y + inset;
306 const float iw =
w - inset * 2.f;
307 const float ih =
h - inset * 2.f;
310 drawSolid(gfx, ix, iy, iw, ih, L->angle, glm::vec4(V->tint,
a));
313 const float headerH = ih * 0.16f;
314 drawSolid(gfx, ix, iy, iw, headerH, L->angle,
315 glm::vec4(V->tint.r * 0.82f, V->tint.g * 0.82f, V->tint.b * 0.82f,
a));
318 const float artY = iy + headerH;
319 const float artH = ih * 0.34f;
321 drawTexture(gfx, V->texture, ix + 2.f, artY + 2.f, iw - 4.f, artH - 4.f, L->angle,
322 glm::vec4(1.f, 1.f, 1.f,
a));
324 drawSolid(gfx, ix + 2.f, artY + 2.f, iw - 4.f, artH - 4.f, L->angle,
325 glm::vec4(V->tint.r * 0.55f, V->tint.g * 0.55f, V->tint.b * 0.55f,
a));
329 const float bodyY = artY + artH;
330 const float bodyH = iy + ih - bodyY;
331 drawSolid(gfx, ix, bodyY, iw, bodyH, L->angle, glm::vec4(0.96f, 0.94f, 0.90f,
a));
332 drawSolid(gfx, ix + iw * 0.12f, bodyY + bodyH * 0.5f - 1.f, iw * 0.76f, 2.f, L->angle,
333 glm::vec4(0.45f, 0.42f, 0.38f,
a));
336 const float gem =
w * 0.13f;
337 drawGem(gfx,
x + inset, iy, gem, L->angle, glm::vec3(1.00f, 0.78f, 0.25f),
a);
338 if (I->kind !=
"spell") {
339 drawGem(gfx,
x + inset, iy + ih - gem, gem, L->angle, glm::vec3(0.92f, 0.38f, 0.22f),
a);
340 drawGem(gfx,
x + iw - inset - gem, iy + ih - gem, gem, L->angle, glm::vec3(0.30f, 0.78f, 0.42f),
a);
346 const float ts = 1.f * (
w / 110.f);
347 printCentered(gfx, std::to_string(S->cost),
x + inset + gem * 0.5f, iy + gem * 0.55f, ts,
348 glm::vec4(0.12f, 0.08f, 0.05f,
a));
349 if (I->kind !=
"spell") {
350 printCentered(gfx, std::to_string(S->attack),
x + inset + gem * 0.5f, iy + ih - gem * 0.45f,
351 ts, glm::vec4(0.10f, 0.05f, 0.05f,
a));
352 printCentered(gfx, std::to_string(S->health),
x + iw - inset - gem * 0.5f,
353 iy + ih - gem * 0.45f, ts, glm::vec4(0.05f, 0.10f, 0.05f,
a));
355 printCentered(gfx, I->name,
x +
w * 0.5f, iy + headerH * 0.5f, ts * 0.8f,
356 glm::vec4(0.10f, 0.08f, 0.06f,
a));
366 Hand *
h = Hand::create();
374 membership()->cards.push_back(
c);
376 auto *md = meta().operator->();
378 c->layout()->w = md->config ? md->config->cardW : 110.f;
379 c->layout()->h = md->config ? md->config->cardH : 150.f;
380 c->layout()->scale = 0.01f;
381 c->layout()->alpha = (
c->visual()->disabled && md->config) ? md->config->disabledAlpha : 1.f;
383 if (md->faceDown && !md->peek)
384 c->visual()->flip = 1.f;
385 else if (!md->faceDown &&
c->visual()->faceUp)
386 c->visual()->flip = 0.f;
391 auto &cards = membership()->cards;
392 for (
size_t i = 0; i < cards.size(); ++i) {
394 cards.erase(cards.begin() +
static_cast<ptrdiff_t
>(i));
402 membership()->cards.clear();
403 auto *
d = drag().operator->();
409 auto &cards = membership()->cards;
410 if (
index < 0 ||
static_cast<size_t>(
index) >= cards.size())
return nullptr;
411 return cards[
static_cast<size_t>(
index)];
415 for (
auto *
c : membership()->cards)
416 if (
c &&
c->identity()->id ==
id)
return c;
421 auto &cards = membership()->cards;
422 for (
auto it = cards.rbegin(); it != cards.rend(); ++it) {
423 if (*it && (*it)->hit(
px,
py))
return *it;
430 const float totalW =
static_cast<float>(
n) * cfg.
cardW +
static_cast<float>(
n - 1) * cfg.
spacing;
431 const float left = cfg.
handX - totalW * 0.5f;
433 const float t = (
n > 1) ?
static_cast<float>(i) /
static_cast<float>(
n - 1) : 0.5f;
434 const float d = 2.f *
t - 1.f;
439void Hand::update(
float dt,
float mx,
float my,
bool down,
const std::vector<Zone *> &zones,
440 std::vector<CardEvent> &
out) {
441 auto *md = meta().operator->();
442 auto *
d = drag().operator->();
443 auto &cards = membership()->cards;
445 const int n =
static_cast<int>(cards.size());
448 for (
int i = 0; i <
n; ++i) {
449 CardData *card = cards[
static_cast<size_t>(i)];
450 if (!card || card ==
d->card)
continue;
452 float sx = 0.f,
sy = 0.f, sa = 0.f;
456 if (
d->card ==
nullptr) hover = (card ==
pick(mx, my));
457 auto *st = card->state().operator->();
458 auto *vis = card->visual().operator->();
459 auto *lay = card->layout().operator->();
466 float targetScale = 1.f;
468 float targetAngle = sa;
475 lay->x = lerpf(lay->x,
sx, k);
476 lay->y = lerpf(lay->y, targetY, k);
477 lay->scale = lerpf(lay->scale, targetScale, k);
478 lay->angle = lerpf(lay->angle, targetAngle, k);
479 lay->alpha = lerpf(lay->alpha, vis->disabled ? cfg.
disabledAlpha : 1.f, k);
485 const bool press = down && !
d->wasDown;
486 const bool release = !down &&
d->wasDown;
489 if (press && md->interactive) {
490 d->press =
pick(mx, my);
491 if (
d->press &&
d->press->visual()->disabled)
d->press =
nullptr;
496 if (down &&
d->press && !
d->card) {
497 const float dx = mx -
d->pressX;
498 const float dy = my -
d->pressY;
501 d->card->state()->dragging =
true;
503 d->ox = mx -
d->card->layout()->x;
504 d->oy = my -
d->card->layout()->y;
508 if (
d->card && down) {
509 auto *lay =
d->card->layout().operator->();
510 auto *vis =
d->card->visual().operator->();
522 d->card->state()->dragging =
false;
526 for (
Zone *
z : zones) {
527 if (
z &&
z->contains(mx, my)) {
528 const std::string cardId =
dropped ?
dropped->identity()->id :
"";
530 out.push_back(
CardEvent{
"drop", md->owner,
z->rect()->id, cardId,
""});
532 out.push_back(
CardEvent{
"dropRejected", md->owner,
z->rect()->id, cardId,
533 "kind_not_allowed"});
538 }
else if (
d->press) {
539 out.push_back(
CardEvent{
"click", md->owner,
"",
d->press->identity()->id,
""});
546 auto *md = meta().operator->();
549 const CardData *dragCard = drag()->card;
550 for (
auto *
c : membership()->cards) {
553 if (dragCard)
renderCard(gfx, *dragCard, cfg,
false);
eve::EntitySpatialPose pose
Selective canonical attributes for card combat values.
std::array< float, 3 > scale
std::shared_ptr< const OceanSprayFrame > previous
std::vector< EquipmentStat > stats
static eve::Result< void > project(CardData &card)
Project canonical values into the legacy integer Stats fields.
单张卡牌:ECS 实体,数据拆成 Identity / Stats / Visual / Layout / State。
static CardData * createCard()
创建并触摸全部组件。
bool hit(float px, float py)
以当前中心 + 缩放判断点是否命中。
std::string describe()
卡牌描述字符串(调试用)。
CardData * get(int index)
CardData * peek()
查看栈顶卡牌(不弹出)。
CardData * draw()
弹出栈顶卡牌(末尾);空牌库返回 nullptr。
static Deck * createDeck()
创建并触摸 Membership。
手牌:扇形布局 + 悬浮 + 拖拽 + 落区判定 + 渲染。
void slotTransform(int i, int n, float &ox, float &oy, float &angle)
第 i 张(共 n 张)的扇形目标位置(牌中心)。
void release() override
Entity.
CardData * get(int index)
static Hand * createHand()
创建并触摸全部组件。
bool removeCard(CardData *c)
CardData * pick(float px, float py)
命中检测:返回 (px,py) 处最上层卡牌。
void addCard(CardData *c)
手牌增删与查询。
void render(graphics::Graphics *gfx)
绘制整手牌。
void update(float dt, float mx, float my, bool down, const std::vector< Zone * > &zones, std::vector< CardEvent > &out)
每帧推进:布局 + 悬浮 + 拖拽状态机,产出事件到 out。
CardData * find(const std::string &id)
落牌区(手牌区 / 出牌区 / 弃牌区),用于拖放命中判定。
void render(graphics::Graphics *gfx, bool showLabel)
绘制落牌区。
static Zone * createZone()
创建并触摸组件。
bool accepts(CardData *card)
该区域是否接受这张卡。
bool contains(float px, float py)
点是否落在区域内。
float getWidth(const std::string &text) const
Pixel width of text (UTF-8), including kerning; delegates to FontData.
virtual void drawSolidRect(float x, float y, float w, float h, float r, float g, float b, float a=1.f)
RGBA-float overload matching the script-facing drawSolidRect name.
virtual void drawText(Font *font, const std::string &text, float x, float y, const Color &color=Color(1.f, 1.f, 1.f, 1.f), float scale=1.f)
Draw UTF-8 text with an explicitly supplied GPU font.
virtual void drawSolidRectRotated(float cx, float cy, float w, float h, float degrees, const Color &color, BlendMode blend=BlendMode::Alpha)=0
Rotated solid quad degrees clockwise (screen Y-down) around (cx, cy).
Font * getFont() const
Returns the font.
constexpr float lerpf(float a, float b, float t) noexcept
Lerpf.
constexpr float clampf(float v, float lo, float hi) noexcept
Clampf.
void renderCardBack(graphics::Graphics *gfx, float x, float y, float w, float h, float angle, float a)
绘制牌背。
void advanceCardFlip(float &flip, float targetFaceDown, float speed, float dt)
Advance animated flip toward a face-down target with frame-rate-normalized speed.
bool resolveCardWantFaceUp(bool cardFaceUp, bool handFaceDown, bool handPeek)
Resolve whether a card should read as face-up under hand faceDown/peek rules.
const char * cardStateName(CardState state)
状态枚举的字符串名(用于脚本/调试)。
CardFlipPose evaluateCardFlip(float flipProgress, float liftPeak)
Evaluate face-down progress into an adapter-facing flip pose.
void printCentered(graphics::Graphics *gfx, const std::string &text, float cx, float cy, float scale, const glm::vec4 &color)
渲染辅助(也供 Zone / Deck 复用)。
void renderCard(graphics::Graphics *gfx, const CardData &cardRef, const LayoutConfig &cfg, bool back)
绘制单张卡牌(正面或牌背;back 强制牌背,忽略 flip)。
Evaluated flip pose for external 2D/3D adapters (graphics-independent).
UiCard 风格的布局配置(脚本可实时修改,对应 UiCard 的 Configs 面板)。
bool hoverRotation
悬浮时是否恢复为水平。
float arcHeight
扇形弧度(两端上翘高度)。
float flipLift
Peak lift (logical pixels) at the edge-on midpoint of a flip.
float disabledAlpha
禁用牌透明度。
float handX
手牌扇形中心 X / 基准高度 Y。
float motionSpeed
通用归位运动速度。
float flipSpeed
Face flip lerp speed (same 60fps-normalized units as motionSpeed). Drives the animated 0....
int flipAxis
Flip hinge: 0 = vertical (width foreshortens / rotate about local up), 1 = horizontal (height foresho...
float hoverScale
悬浮缩放/上移像素/运动速度。
float rotationAngle
手牌两端最大旋转角(度)。
float dragThreshold
拖拽触发距离(像素)。