载入中...
搜索中...
未找到
CardTypes.h
浏览该文件的文档.
1#pragma once
2
8#include "common/ECS.h"
9
10#include <glm/vec3.hpp>
11#include <glm/vec4.hpp>
12
13#include <cstdint>
14#include <string>
15#include <vector>
16
17namespace eve::graphics {
18class Graphics;
19class Texture;
20} // namespace eve::graphics
21
22namespace eve::card {
23
25enum class CardState : uint8_t {
26 Deck,
27 Hand,
28 Hovered,
31 Played,
34};
35
37const char *cardStateName(CardState state);
38
42 float cardW = 110.f;
43 float cardH = 150.f;
45 float spacing = 26.f;
47 float handX = 640.f;
48 float handY = 640.f;
50 float arcHeight = 46.f;
52 float rotationAngle = 14.f;
54 bool hoverRotation = true;
56 float hoverScale = 1.22f;
57 float hoverLift = 46.f;
58 float hoverSpeed = 0.18f;
60 float motionSpeed = 0.12f;
62 float disabledAlpha = 0.35f;
64 bool showZones = true;
66 float deckX = 360.f;
67 float deckY = 640.f;
69 float dragThreshold = 64.f;
70};
71
74 std::string id;
75 std::string name;
76 std::string kind = "creature"; // "creature" | "spell" | ...
77 int cost = 0;
78 int attack = 0;
79 int health = 0;
80 glm::vec3 tint{0.62f, 0.50f, 0.40f};
81};
82
84class CardData : public ecs::Entity {
85public:
86 ENTITY(CardData, ecs::Entity)
87
88 void release() override { ecs::DestroyEntity(this); }
89
91 struct Identity {
92 std::string id;
93 std::string name;
94 std::string kind = "creature";
95 };
97 struct Stats {
98 int cost = 0;
99 int attack = 0;
100 int health = 0;
101 };
103 struct Visual {
104 bool faceUp = true;
105 bool disabled = false;
106 glm::vec3 tint{0.62f, 0.50f, 0.40f};
108 };
110 struct Layout {
111 float x = 0.f;
112 float y = 0.f;
113 float w = 0.f;
114 float h = 0.f;
115 float angle = 0.f;
116 float scale = 1.f;
117 float alpha = 1.f;
118 };
120 struct State {
122 bool hovered = false;
123 bool dragging = false;
124 };
125
126 COMPONENT(Identity, identity)
127 COMPONENT(Stats, stats)
128 COMPONENT(Visual, visual)
129 COMPONENT(Layout, layout)
130 COMPONENT(State, state)
131
132
133 static CardData *createCard();
134
136 bool hit(float px, float py);
138 std::string describe();
139};
140
142class Deck : public ecs::Entity {
143public:
144 ENTITY(Deck, ecs::Entity)
145
146 void release() override { ecs::DestroyEntity(this); }
147
149 struct Membership {
150 std::vector<CardData *> cards;
151 };
152
153 COMPONENT(Membership, membership)
154
155
156 static Deck *createDeck();
157
159 void push(CardData *c) { membership()->cards.push_back(c); }
161 CardData *draw();
163 CardData *peek();
165 void clear() { membership()->cards.clear(); }
167 void shuffle();
169 int count() { return static_cast<int>(membership()->cards.size()); }
170 bool isEmpty() { return membership()->cards.empty(); }
171 CardData *get(int index);
172};
173
175class Zone : public ecs::Entity {
176public:
177 ENTITY(Zone, ecs::Entity)
178
179 void release() override { ecs::DestroyEntity(this); }
180
182 struct Rect {
183 std::string id;
184 std::string label;
185 float x = 0.f, y = 0.f, w = 0.f, h = 0.f;
186 glm::vec3 color{0.30f, 0.60f, 0.30f};
187 float alpha = 0.16f;
188 bool enabled = true;
189 };
191 struct Filter {
192 std::vector<std::string> acceptKinds;
193 };
194
195 COMPONENT(Rect, rect)
196 COMPONENT(Filter, filter)
197
198
199 static Zone *createZone();
200
202 bool contains(float px, float py);
204 bool accepts(CardData *card);
206 void render(graphics::Graphics *gfx, bool showLabel);
207};
208
210struct CardEvent {
212 std::string type;
214 std::string hand;
216 std::string zoneId;
217 std::string cardId;
219 std::string reason;
220};
221
223class Hand : public ecs::Entity {
224public:
225 ENTITY(Hand, ecs::Entity)
226
227 void release() override { ecs::DestroyEntity(this); }
228
230 struct Meta {
231 std::string owner = "player";
233 bool faceDown = false;
234 bool peek = false;
235 bool interactive = true;
236 };
238 struct Membership {
239 std::vector<CardData *> cards;
240 };
242 struct Drag {
243 CardData *card = nullptr;
244 CardData *press = nullptr;
245 float ox = 0.f, oy = 0.f;
246 float pressX = 0.f, pressY = 0.f;
247 bool wasDown = false;
248 };
249
250 COMPONENT(Meta, meta)
251 COMPONENT(Membership, membership)
252 COMPONENT(Drag, drag)
253
254
255 static Hand *createHand();
256
258 void addCard(CardData *c);
259 bool removeCard(CardData *c);
260 void clear();
261 int count() { return static_cast<int>(membership()->cards.size()); }
262 CardData *get(int index);
263 CardData *find(const std::string &id);
265 CardData *pick(float px, float py);
266
268 void slotTransform(int i, int n, float &ox, float &oy, float &angle);
269
271 void update(float dt, float mx, float my, bool down, const std::vector<Zone *> &zones,
272 std::vector<CardEvent> &out);
273
275 void render(graphics::Graphics *gfx);
276};
277
279void printCentered(graphics::Graphics *gfx, const std::string &text, float cx, float cy, float scale,
280 const glm::vec4 &color);
282void renderCardBack(graphics::Graphics *gfx, float x, float y, float w, float h, float angle, float a);
284void renderCard(graphics::Graphics *gfx, const CardData &card, const LayoutConfig &cfg, bool back);
285
286} // namespace eve::card
float cx
Definition CardTypes.cpp:31
float cy
Definition CardTypes.cpp:32
std::string layout
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
glm::vec3 n
Definition Grass.cpp:64
int h
int w
std::vector< Colorf > px
JobSystemThreadPool::State * state
uint32_t a
uint32_t c
std::string filter
image::ImageData::Colorf color
float scale
Definition TreeMesh.cpp:122
单张卡牌:ECS 实体,数据拆成 Identity / Stats / Visual / Layout / State。
Definition CardTypes.h:84
static CardData * createCard()
创建并触摸全部组件。
bool hit(float px, float py)
以当前中心 + 缩放判断点是否命中。
void release() override
Definition CardTypes.h:88
std::string describe()
卡牌描述字符串(调试用)。
牌库(栈顶在末尾)。
Definition CardTypes.h:142
CardData * get(int index)
CardData * peek()
查看栈顶卡牌(不弹出)。
void push(CardData *c)
将卡牌压入栈顶(末尾)。
Definition CardTypes.h:159
void shuffle()
洗牌。
CardData * draw()
弹出栈顶卡牌(末尾);空牌库返回 nullptr。
void clear()
清空牌库。
Definition CardTypes.h:165
static Deck * createDeck()
创建并触摸 Membership。
void release() override
Definition CardTypes.h:146
int count()
牌库数量/是否为空/按下标取牌。
Definition CardTypes.h:169
手牌:扇形布局 + 悬浮 + 拖拽 + 落区判定 + 渲染。
Definition CardTypes.h:223
void slotTransform(int i, int n, float &ox, float &oy, float &angle)
第 i 张(共 n 张)的扇形目标位置(牌中心)。
void release() override
Definition CardTypes.h:227
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)
落牌区(手牌区 / 出牌区 / 弃牌区),用于拖放命中判定。
Definition CardTypes.h:175
void render(graphics::Graphics *gfx, bool showLabel)
绘制落牌区。
static Zone * createZone()
创建并触摸组件。
bool accepts(CardData *card)
该区域是否接受这张卡。
void release() override
Definition CardTypes.h:179
bool contains(float px, float py)
点是否落在区域内。
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:17
void renderCardBack(graphics::Graphics *gfx, float x, float y, float w, float h, float angle, float a)
绘制牌背。
CardState
单张卡牌的运行时状态。
Definition CardTypes.h:25
const char * cardStateName(CardState state)
状态枚举的字符串名(用于脚本/调试)。
Definition CardTypes.cpp:10
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)
绘制单张卡牌(正面或牌背)。
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition AnimTrail.h:6
稳定实例 id、显示名、种类。
Definition CardTypes.h:91
运行时布局(由 Hand 维护):中心坐标与变换。
Definition CardTypes.h:110
运行时交互状态。
Definition CardTypes.h:120
费用 / 攻击 / 生命。
Definition CardTypes.h:97
朝向、禁用、色调、卡图。
Definition CardTypes.h:103
graphics::Texture * texture
Definition CardTypes.h:107
卡牌类型定义(registerCardsFromJson 注册)。
Definition CardTypes.h:73
一次交互事件。
Definition CardTypes.h:210
std::string hand
来源手牌 owner。
Definition CardTypes.h:214
std::string type
事件类型:"click" | "drop"。
Definition CardTypes.h:212
std::string reason
dropRejected 时为拒绝原因。
Definition CardTypes.h:219
std::string cardId
Definition CardTypes.h:217
std::string zoneId
drop 目标的落牌区 id(click 为空)。
Definition CardTypes.h:216
有序牌堆;栈顶在 vector 末尾。
Definition CardTypes.h:149
std::vector< CardData * > cards
Definition CardTypes.h:150
拖拽内部状态(勿直接修改)。
Definition CardTypes.h:242
有序手牌列表。
Definition CardTypes.h:238
std::vector< CardData * > cards
Definition CardTypes.h:239
所有者与布局/交互开关。
Definition CardTypes.h:230
LayoutConfig * config
Definition CardTypes.h:232
UiCard 风格的布局配置(脚本可实时修改,对应 UiCard 的 Configs 面板)。
Definition CardTypes.h:40
float cardW
卡牌宽/高(像素)。
Definition CardTypes.h:42
bool hoverRotation
悬浮时是否恢复为水平。
Definition CardTypes.h:54
float arcHeight
扇形弧度(两端上翘高度)。
Definition CardTypes.h:50
float disabledAlpha
禁用牌透明度。
Definition CardTypes.h:62
float handX
手牌扇形中心 X / 基准高度 Y。
Definition CardTypes.h:47
float motionSpeed
通用归位运动速度。
Definition CardTypes.h:60
float deckX
牌库堆绘制位置。
Definition CardTypes.h:66
float spacing
手牌水平间距。
Definition CardTypes.h:45
float hoverScale
悬浮缩放/上移像素/运动速度。
Definition CardTypes.h:56
bool showZones
是否绘制落牌区。
Definition CardTypes.h:64
float rotationAngle
手牌两端最大旋转角(度)。
Definition CardTypes.h:52
float dragThreshold
拖拽触发距离(像素)。
Definition CardTypes.h:69
接受的卡牌 kind;空 = 接受任意 kind。
Definition CardTypes.h:191
std::vector< std::string > acceptKinds
Definition CardTypes.h:192
区域矩形与绘制参数。
Definition CardTypes.h:182