SlotGame.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. // =============================================================
  2. // SlotGame.ts —— 《果冻消消乐 · Jelly Pop》消除式老虎机(可玩原型)
  3. // by anim_studio
  4. // v4:先预加载全部 UI 美术,再搭界面——HUD 胶囊用 hud_pill、
  5. // 圆按钮用 btn_round、SPIN 用 btn_spin;隐藏预览性能面板;
  6. // 4×5 大符号 + 每格圆角卡片。
  7. // =============================================================
  8. import {
  9. _decorator, Component, Node, sp, resources, JsonAsset, SpriteFrame, Sprite,
  10. ParticleSystem2D, UITransform, Label, Graphics, Button, Color, Vec3,
  11. view, EventTouch, tween, UIOpacity,
  12. } from 'cc';
  13. import * as cc from 'cc';
  14. import { applyParticleConfig } from './ParticleConfig';
  15. const { ccclass } = _decorator;
  16. const SYMBOLS = __SYMBOLS__;
  17. const GENERATED_CONFIG = __GAME_CONFIG__;
  18. // ============ 画面结构(先定区域,再往里填元素)============
  19. // 所有数值都是相对屏幕的比例;cy = 区域中心相对屏幕中心的 Y(向上为正)。
  20. const LAYOUT = (GENERATED_CONFIG.layout || {
  21. logo: { cy: 0.41, maxW: 0.82, maxH: 0.15 }, // 顶部 Logo
  22. reel: { cy: 0.02, h: 0.60, aspect: 0.652, // 卷轴框(按新图:霓虹边+白板)
  23. holeW: 0.86, holeH: 0.92, cols: 4, rows: 6 }, // 白板内孔 + 网格行列
  24. hud: { cy: -0.33, pillH: 0.40 }, // 三个 HUD 胶囊(按图比例)
  25. controls: { cy: -0.405, spinR: 0.13, smallR: 0.075, // 控制按钮(按宽度定标、做大)
  26. xMinus: 0.24, xTurbo: 0.40 },
  27. });
  28. const ECONOMY = GENERATED_CONFIG.economy || { startingBalance: 5000, defaultBet: 50 };
  29. const WIN_RULES = GENERATED_CONFIG.winRules || { minMatch: 4 };
  30. const FEATURE_RULES = GENERATED_CONFIG.features || {};
  31. const FEEDBACK_CONFIG = GENERATED_CONFIG.feedback || {};
  32. const PAYTABLE = GENERATED_CONFIG.paytable || {};
  33. const SYMBOL_RULES = GENERATED_CONFIG.symbols || [];
  34. const MATH_MODEL = GENERATED_CONFIG.mathModel || {};
  35. const PAYOUT_SCALE = MATH_MODEL.payoutScale ?? 1;
  36. const REEL_STRIPS = MATH_MODEL.reelStrips || [];
  37. const BOSS_CONFIG = GENERATED_CONFIG.boss || { enabled: false };
  38. const BOSS_ID = BOSS_CONFIG.id || 'boss_demon_lord';
  39. // ============ 文字样式表(统一定义,按角色取用)============
  40. // size<=1 表示相对所在容器高度的比例;否则按像素。ow=描边宽。
  41. const STYLE = {
  42. hudTitle: { size: 0.30, color: [205, 222, 255], bold: false, outline: [40, 28, 78], ow: 0, letter: 0.04 },
  43. hudValue: { size: 0.52, color: [255, 255, 255], bold: true, outline: [54, 30, 96], ow: 3 },
  44. mult: { size: 66, color: [255, 214, 70], bold: true, outline: [188, 70, 24], ow: 6 },
  45. floatWin: { size: 46, color: [255, 244, 130], bold: true, outline: [196, 92, 24], ow: 5 },
  46. bigWin: { size: 0.16, color: [255, 224, 70], bold: true, outline: [180, 50, 16], ow: 9 }, // 报幕(相对屏宽)
  47. };
  48. // ============ 每个符号的"按内容定标"参数(s=缩放/格, oyf=居中下移/格)============
  49. // 由素材实际内容包围盒算出,保证果冻填满格子、垂直居中——这就是"按规格画/摆"。
  50. const SYMFIT: Record<string, {s: number; oyf: number}> = __SYMBOL_FIT__;
  51. const SYMFIT_DEFAULT = __SYMBOL_FIT_DEFAULT__;
  52. // ============ 交互/动效反馈(统一定义)============
  53. const FEEDBACK = {
  54. btnPress: 0.86,
  55. btnBack: 0.16,
  56. idleBob: 0.016,
  57. dropFrom: 1.05,
  58. dropTime: FEEDBACK_CONFIG.dropTimeSec ?? 0.30,
  59. clearPop: FEEDBACK_CONFIG.clearPopScale ?? 1.30,
  60. spinDuration: FEEDBACK_CONFIG.spinDurationSec ?? 0.45,
  61. bigWinThresholdBet: FEEDBACK_CONFIG.bigWinThresholdBet ?? 25,
  62. screenShake: FEEDBACK_CONFIG.screenShake ?? false,
  63. };
  64. const COLS = LAYOUT.reel.cols, ROWS = LAYOUT.reel.rows;
  65. const MIN_MATCH = WIN_RULES.minMatch ?? 4;
  66. const CASCADE_RULES = FEATURE_RULES.cascades || {};
  67. const MAX_CASCADE = CASCADE_RULES.maxCascades ?? 6;
  68. const CASCADE_MULT_STEP = CASCADE_RULES.multiplierStep ?? 1;
  69. const CASCADE_MULT_MAX = CASCADE_RULES.maxMultiplier ?? 6;
  70. const START_BALANCE = ECONOMY.startingBalance ?? 5000;
  71. const DEFAULT_BET = ECONOMY.defaultBet ?? 50;
  72. const SCATTER_RULES = FEATURE_RULES.scatterFreeSpins || {};
  73. const HOLD_RULES = FEATURE_RULES.holdAndWin || {};
  74. const IS_CLUSTER = WIN_RULES.evaluation === 'cluster_count';
  75. const FREE_SPIN_TRIGGER = SCATTER_RULES.triggerCount ?? 3;
  76. const FREE_SPIN_AWARD = SCATTER_RULES.awardSpins ?? 8;
  77. const HOLD_TRIGGER = HOLD_RULES.triggerCount ?? 6;
  78. const HOLD_RESPINS = HOLD_RULES.respins ?? 3;
  79. const SYMBOL_ROLE: Record<string, string> = {};
  80. SYMBOL_RULES.forEach((s: any) => { SYMBOL_ROLE[s.id] = s.role; });
  81. @ccclass('SlotGame')
  82. export class SlotGame extends Component {
  83. private dataMap: Record<string, sp.SkeletonData> = {};
  84. private art: Record<string, SpriteFrame | null> = {};
  85. private cells: sp.Skeleton[][] = [];
  86. private ids: string[][] = [];
  87. private cell = 90;
  88. private gridX0 = 0;
  89. private gridY0 = 0;
  90. private spinning = false;
  91. private balance = START_BALANCE;
  92. private displayBalance = START_BALANCE;
  93. private bet = DEFAULT_BET;
  94. private multiplier = 1;
  95. private roundWin = 0;
  96. private cascade = 0;
  97. private freeSpins = 0;
  98. private holdActive = false;
  99. private holdRespinsLeft = 0;
  100. private holdHeld: boolean[][] = [];
  101. private holdWinTotal = 0;
  102. private balanceLabel!: Label;
  103. private betLabel!: Label;
  104. private winLabel!: Label;
  105. private multLabel!: Label;
  106. private spinBtn!: Node;
  107. private particleTex: SpriteFrame | null = null;
  108. private frameCY = 0; private frameW = 0; private frameH = 0;
  109. private W = 0; private H = 0;
  110. private symBaseY: number[][] = [];
  111. private t = 0;
  112. private turbo = false; private auto = false;
  113. private turboBtn!: Node; private autoBtn!: Node;
  114. private bossNode: Node | null = null;
  115. private bossSk: sp.Skeleton | null = null;
  116. onLoad() {
  117. try { (cc as any).profiler?.hideStats?.(); } catch (e) {}
  118. this.node.setPosition(0, 0, 0);
  119. const ut = this.node.getComponent(UITransform) || this.node.addComponent(UITransform);
  120. ut.setAnchorPoint(0.5, 0.5);
  121. const s = view.getVisibleSize();
  122. this.W = s.width; this.H = s.height;
  123. ut.setContentSize(s.width, s.height);
  124. this.computeLayout(s.width, s.height);
  125. this.buildBackground(s.width, s.height); // 渐变兜底
  126. resources.load('vfx/particle/spriteFrame', SpriteFrame, (_e, sf) => { if (sf) this.particleTex = sf; });
  127. const artIds = ['bg_main', 'reel_frame', 'logo', 'hud_pill', 'btn_round', 'btn_spin', 'btn_plus', 'btn_minus', 'btn_turbo', 'btn_auto', 'coin'];
  128. let toLoad = artIds.length;
  129. artIds.forEach((id) => {
  130. resources.load(`ui_art/${id}/spriteFrame`, SpriteFrame, (err, sf) => {
  131. this.art[id] = err ? null : sf;
  132. if (--toLoad === 0) this.afterArt();
  133. });
  134. });
  135. }
  136. private afterArt() {
  137. const W = this.W, H = this.H;
  138. this.buildArt(W, H);
  139. this.buildHud(W, H);
  140. this.buildControls(W, H);
  141. const loadIds = BOSS_CONFIG.enabled ? SYMBOLS.concat([BOSS_ID]) : SYMBOLS;
  142. let left = loadIds.length;
  143. loadIds.forEach((id) => {
  144. resources.load(`characters/${id}`, sp.SkeletonData, (err, data) => {
  145. if (!err) this.dataMap[id] = data;
  146. if (--left === 0) { this.buildGrid(); this.buildBoss(); }
  147. });
  148. });
  149. }
  150. private computeLayout(W: number, H: number) {
  151. const R = LAYOUT.reel;
  152. let fh = H * R.h;
  153. let fw = fh * R.aspect;
  154. if (fw > W * 0.96) { fw = W * 0.96; fh = fw / R.aspect; }
  155. this.frameW = fw; this.frameH = fh;
  156. this.frameCY = H * R.cy;
  157. const innerW = fw * R.holeW, innerH = fh * R.holeH;
  158. this.cell = Math.min(innerW / COLS, innerH / ROWS);
  159. this.gridX0 = -((COLS - 1) * this.cell) / 2;
  160. this.gridY0 = this.frameCY + ((ROWS - 1) * this.cell) / 2;
  161. }
  162. private cellPos(c: number, r: number): [number, number] {
  163. return [this.gridX0 + c * this.cell, this.gridY0 - r * this.cell];
  164. }
  165. private buildBackground(W: number, H: number) {
  166. const n = new Node('bg'); n.parent = this.node; n.setSiblingIndex(0);
  167. const g = n.addComponent(Graphics);
  168. g.fillColor = new Color(120, 200, 255, 255); g.rect(-W / 2, 0, W, H / 2); g.fill();
  169. g.fillColor = new Color(255, 150, 175, 255); g.rect(-W / 2, -H / 2, W, H / 2); g.fill();
  170. }
  171. // ---- 背景大图 / 卷轴框 / Logo(有图用图,没图兜底)----
  172. private buildArt(W: number, H: number) {
  173. if (this.art['bg_main']) {
  174. const [w, h] = this.coverSize(this.art['bg_main']!, W, H);
  175. this.spriteNode(this.art['bg_main']!, w, h, 0, 0, 1);
  176. }
  177. if (this.art['reel_frame']) {
  178. this.spriteNode(this.art['reel_frame']!, this.frameW, this.frameH, 0, this.frameCY, 3);
  179. } else {
  180. const n = new Node('frame'); n.parent = this.node; n.setPosition(0, this.frameCY, 0); n.setSiblingIndex(3);
  181. const g = n.addComponent(Graphics);
  182. g.lineWidth = 10; g.strokeColor = new Color(120, 180, 255, 255);
  183. this.roundRect(g, -this.frameW / 2, -this.frameH / 2, this.frameW, this.frameH, 26); g.stroke();
  184. }
  185. const logoCy = H * LAYOUT.logo.cy;
  186. if (this.art['logo']) {
  187. const [w, h] = this.fitSize(this.art['logo']!, W * LAYOUT.logo.maxW, H * LAYOUT.logo.maxH);
  188. this.spriteNode(this.art['logo']!, w, h, 0, logoCy, 21);
  189. } else {
  190. const t = new Node('logo'); t.parent = this.node; t.setPosition(0, logoCy, 0); t.setSiblingIndex(21);
  191. const lab = t.addComponent(Label); lab.string = '🍬 JELLY POP'; lab.fontSize = 42; lab.color = new Color(255, 90, 70, 255);
  192. }
  193. }
  194. private buildBoss() {
  195. if (!BOSS_CONFIG.enabled || !this.dataMap[BOSS_ID]) return;
  196. const node = new Node('boss'); node.parent = this.node; node.setSiblingIndex(8);
  197. const sk = node.addComponent(sp.Skeleton);
  198. sk.skeletonData = this.dataMap[BOSS_ID];
  199. sk.premultipliedAlpha = false;
  200. sk.setAnimation(0, 'idle', true);
  201. const scale = Math.min(0.22, this.W / 3900);
  202. node.setScale(scale, scale, 1);
  203. node.setPosition(this.frameW * 0.40, this.frameCY + this.frameH * 0.32, 0);
  204. this.bossNode = node; this.bossSk = sk;
  205. }
  206. // ---------------- HUD:用 hud_pill 美术 ----------------
  207. private buildHud(W: number, H: number) {
  208. const y = H * LAYOUT.hud.cy;
  209. const pw = (W - 36) / 3 - 8, gap = 8;
  210. const ps = this.art['hud_pill'];
  211. const ph = ps ? pw * (ps.rect.height / ps.rect.width) : pw * LAYOUT.hud.pillH; // 按图比例,不拉伸
  212. this.balanceLabel = this.makePill(-(pw + gap), y, pw, ph, '余额', `${START_BALANCE}`);
  213. this.betLabel = this.makePill(0, y, pw, ph, '下注', `${this.bet}`);
  214. this.winLabel = this.makePill(pw + gap, y, pw, ph, '本局赢', '0');
  215. const m = new Node('mult'); m.parent = this.node; m.setPosition(0, this.frameCY, 0); m.setSiblingIndex(25);
  216. this.multLabel = m.addComponent(Label); this.applyStyle(this.multLabel, STYLE.mult, 0); this.multLabel.string = '';
  217. }
  218. private makePill(x: number, y: number, w: number, h: number, title: string, val: string): Label {
  219. const n = new Node('pill'); n.parent = this.node; n.setPosition(x, y, 0); n.setSiblingIndex(16);
  220. if (this.art['hud_pill']) {
  221. const s = n.addComponent(Sprite); s.sizeMode = Sprite.SizeMode.CUSTOM; s.spriteFrame = this.art['hud_pill']!;
  222. } else {
  223. const g = n.addComponent(Graphics); g.fillColor = new Color(70, 50, 120, 235);
  224. this.roundRect(g, -w / 2, -h / 2, w, h, h / 2); g.fill();
  225. }
  226. const ut = n.getComponent(UITransform) || n.addComponent(UITransform); ut.setContentSize(w, h);
  227. // 标题(小字,靠左)
  228. const tl = new Node('t'); tl.parent = n; tl.setPosition(-w * 0.30, 0, 0);
  229. const tlab = tl.addComponent(Label); tlab.string = title; this.applyStyle(tlab, STYLE.hudTitle, h);
  230. // 数值(大字带描边,靠右)
  231. const vl = new Node('v'); vl.parent = n; vl.setPosition(w * 0.12, 0, 0);
  232. const lab = vl.addComponent(Label); lab.string = val; this.applyStyle(lab, STYLE.hudValue, h);
  233. return lab;
  234. }
  235. // 统一文字样式
  236. private applyStyle(lab: Label, st: any, base: number) {
  237. lab.fontSize = st.size <= 1 ? Math.round(base * st.size) : st.size;
  238. lab.lineHeight = lab.fontSize;
  239. lab.color = new Color(st.color[0], st.color[1], st.color[2], 255);
  240. if (st.bold) lab.isBold = true;
  241. if (st.ow && st.ow > 0) {
  242. lab.enableOutline = true;
  243. lab.outlineColor = new Color(st.outline[0], st.outline[1], st.outline[2], 255);
  244. lab.outlineWidth = st.ow;
  245. }
  246. }
  247. // 数字变化时弹一下
  248. private pop(node: Node, s = 1.22) {
  249. node.setScale(s, s, 1);
  250. tween(node).to(0.22, { scale: new Vec3(1, 1, 1) }, { easing: 'backOut' }).start();
  251. }
  252. // 中奖时在网格中央飘一个 +N 上升淡出
  253. private floatWin(amount: number) {
  254. const n = new Node('fw'); n.parent = this.node; n.setPosition(0, this.frameCY + this.cell, 0); n.setSiblingIndex(27);
  255. const lab = n.addComponent(Label); lab.string = '+' + amount; this.applyStyle(lab, STYLE.floatWin, 0);
  256. const op = n.addComponent(UIOpacity);
  257. n.setScale(0.5, 0.5, 1);
  258. tween(n).to(0.16, { scale: new Vec3(1.15, 1.15, 1) }, { easing: 'backOut' })
  259. .to(0.12, { scale: new Vec3(1, 1, 1) }).start();
  260. tween(n).by(0.85, { position: new Vec3(0, this.cell * 1.6, 0) }, { easing: 'cubicOut' }).start();
  261. tween(op).delay(0.35).to(0.5, { opacity: 0 }).call(() => n.destroy()).start();
  262. }
  263. // ---------------- 控制区:btn_round / btn_spin 美术 ----------------
  264. private buildControls(W: number, H: number) {
  265. const C = LAYOUT.controls;
  266. const y = H * C.cy;
  267. const big = W * C.spinR; // SPIN 按屏宽定标,最突出
  268. const sr = W * C.smallR;
  269. this.spinBtn = this.makeBtn(0, y, big, 'btn_spin', '⟳', big * 0.6, () => this.spin());
  270. this.turboBtn = this.makeBtn(-W * C.xTurbo, y, sr, 'btn_turbo', '⚡', sr * 0.8, () => this.toggleTurbo());
  271. this.makeBtn(-W * C.xMinus, y, sr, 'btn_minus', '−', sr, () => this.changeBet(-10));
  272. this.makeBtn(W * C.xMinus, y, sr, 'btn_plus', '+', sr, () => this.changeBet(10));
  273. this.autoBtn = this.makeBtn(W * C.xTurbo, y, sr, 'btn_auto', '▶', sr * 0.8, () => this.toggleAuto());
  274. }
  275. private makeBtn(x: number, y: number, r: number, artId: string, glyph: string, fs: number, onClick: () => void): Node {
  276. const node = new Node('btn'); node.parent = this.node; node.setPosition(x, y, 0); node.setSiblingIndex(17);
  277. let hasArt = false;
  278. if (this.art[artId]) {
  279. const s = node.addComponent(Sprite); s.sizeMode = Sprite.SizeMode.CUSTOM; s.spriteFrame = this.art[artId]!; hasArt = true;
  280. } else {
  281. const g = node.addComponent(Graphics); g.fillColor = new Color(90, 70, 140, 255); g.circle(0, 0, r); g.fill();
  282. g.lineWidth = 4; g.strokeColor = new Color(255, 255, 255, 235); g.circle(0, 0, r); g.stroke();
  283. }
  284. const ut = node.getComponent(UITransform) || node.addComponent(UITransform); ut.setContentSize(r * 2, r * 2);
  285. // 有美术(图标已烤进图里)就不叠字;没有才用文字兜底
  286. if (!hasArt) {
  287. const gl = new Node('g'); gl.parent = node;
  288. const lab = gl.addComponent(Label); lab.string = glyph; lab.fontSize = fs; lab.color = new Color(255, 255, 255, 255);
  289. }
  290. const btn = node.addComponent(Button);
  291. btn.transition = Button.Transition.NONE; // 自己控制反馈,更跟手
  292. const press = () => { tween(node).to(0.07, { scale: new Vec3(FEEDBACK.btnPress, FEEDBACK.btnPress, 1) }).start(); };
  293. const release = () => { tween(node).to(FEEDBACK.btnBack, { scale: new Vec3(1, 1, 1) }, { easing: 'backOut' }).start(); };
  294. node.on(Node.EventType.TOUCH_START, press);
  295. node.on(Node.EventType.TOUCH_END, (_e: EventTouch) => { release(); onClick(); });
  296. node.on(Node.EventType.TOUCH_CANCEL, release);
  297. return node;
  298. }
  299. private makeTextLabel(x: number, y: number, text: string, fs: number) {
  300. const n = new Node('tl'); n.parent = this.node; n.setPosition(x, y, 0); n.setSiblingIndex(17);
  301. const lab = n.addComponent(Label); lab.string = text; lab.fontSize = fs; lab.color = new Color(255, 255, 255, 235);
  302. }
  303. private changeBet(d: number) {
  304. if (this.spinning) return;
  305. this.bet = Math.max(10, Math.min(500, this.bet + d));
  306. this.betLabel.string = `${this.bet}`;
  307. this.pop(this.betLabel.node);
  308. }
  309. // TURBO:加速;AUTO:自动连转。带高亮状态。
  310. private td(v: number) { return this.turbo ? v * 0.4 : v; }
  311. private toggleTurbo() {
  312. this.turbo = !this.turbo; this.tintBtn(this.turboBtn, this.turbo);
  313. }
  314. private toggleAuto() {
  315. this.auto = !this.auto; this.tintBtn(this.autoBtn, this.auto);
  316. if (this.auto && !this.spinning) this.spin();
  317. }
  318. private tintBtn(node: Node, on: boolean) {
  319. const s = node.getComponent(Sprite);
  320. if (s) s.color = on ? new Color(255, 224, 90, 255) : new Color(255, 255, 255, 255);
  321. tween(node).to(0.12, { scale: new Vec3(on ? 1.12 : 1, on ? 1.12 : 1, 1) }, { easing: 'backOut' }).start();
  322. }
  323. // 大额报幕 + 金币雨
  324. private celebrate(amount: number) {
  325. const n = new Node('bigwin'); n.parent = this.node; n.setPosition(0, this.frameCY, 0);
  326. n.setSiblingIndex(this.node.children.length); // 强制置顶
  327. const lab = n.addComponent(Label); lab.string = '+' + amount; this.applyStyle(lab, STYLE.bigWin, this.W);
  328. const op = n.addComponent(UIOpacity);
  329. n.setScale(0.2, 0.2, 1);
  330. tween(n).to(0.28, { scale: new Vec3(1.18, 1.18, 1) }, { easing: 'backOut' })
  331. .to(0.12, { scale: new Vec3(1, 1, 1) })
  332. .delay(0.6).to(0.25, { scale: new Vec3(1.25, 1.25, 1) }).start();
  333. tween(op).delay(0.95).to(0.35, { opacity: 0 }).call(() => n.destroy()).start();
  334. this.coinShower(Math.max(12, Math.min(40, Math.floor(amount / 15))));
  335. }
  336. private bossDefeated() {
  337. if (!this.bossNode || !this.bossSk) return;
  338. this.bossSk.setAnimation(0, 'hurt', false);
  339. this.bossSk.addAnimation(0, 'idle', true, 0.2);
  340. this.playParticle('boss_explosion');
  341. const n = this.bossNode;
  342. const base = n.scale.x;
  343. tween(n).to(0.10, { scale: new Vec3(base * 1.18, base * 0.86, 1), angle: -7 })
  344. .to(0.12, { scale: new Vec3(base * 0.82, base * 1.18, 1), angle: 8 })
  345. .to(0.18, { scale: new Vec3(base, base, 1), angle: 0 }, { easing: 'backOut' }).start();
  346. this.flashMult('大魔王裂开爆炸!');
  347. }
  348. private bossTaunt() {
  349. if (!this.bossNode || !this.bossSk) return;
  350. this.bossSk.setAnimation(0, 'attack', false);
  351. this.bossSk.addAnimation(0, 'idle', true, 0);
  352. const n = this.bossNode;
  353. const x = n.position.x, y = n.position.y;
  354. tween(n).to(0.12, { position: new Vec3(x - this.cell * 0.12, y + this.cell * 0.10, 0), angle: -5 })
  355. .to(0.16, { position: new Vec3(x + this.cell * 0.10, y - this.cell * 0.18, 0), angle: 7 })
  356. .to(0.18, { position: new Vec3(x, y, 0), angle: 0 }, { easing: 'backOut' }).start();
  357. this.flashMult('大魔王举剑耀武扬威');
  358. }
  359. private coinShower(count: number) {
  360. const sf = this.art['coin']; if (!sf) return;
  361. const sz = this.cell * 0.6;
  362. for (let i = 0; i < count; i++) {
  363. const x0 = (Math.random() * 2 - 1) * this.frameW * 0.42;
  364. const c = this.spriteNode(sf, sz, sz, x0, this.H * 0.5 + this.cell, this.node.children.length + 1);
  365. const op = c.addComponent(UIOpacity);
  366. const dur = 0.9 + Math.random() * 0.7;
  367. const dx = (Math.random() * 2 - 1) * this.W * 0.12;
  368. tween(c).delay(Math.random() * 0.35)
  369. .by(dur, { position: new Vec3(dx, -this.H - this.cell * 2, 0), angle: (Math.random() * 2 - 1) * 720 }, { easing: 'sineIn' })
  370. .call(() => c.destroy()).start();
  371. tween(op).delay(dur * 0.7).to(dur * 0.3, { opacity: 0 }).start();
  372. }
  373. }
  374. private fitSize(sf: SpriteFrame, boxW: number, boxH: number): [number, number] {
  375. const r = sf.rect; const aw = r.width || 1, ah = r.height || 1;
  376. const s = Math.min(boxW / aw, boxH / ah); return [aw * s, ah * s];
  377. }
  378. private coverSize(sf: SpriteFrame, boxW: number, boxH: number): [number, number] {
  379. const r = sf.rect; const aw = r.width || 1, ah = r.height || 1;
  380. const s = Math.max(boxW / aw, boxH / ah); return [aw * s, ah * s];
  381. }
  382. private spriteNode(sf: SpriteFrame, w: number, h: number, x: number, y: number, sib: number): Node {
  383. const n = new Node('art'); n.parent = this.node; n.setPosition(x, y, 0); n.setSiblingIndex(sib);
  384. const s = n.addComponent(Sprite); s.sizeMode = Sprite.SizeMode.CUSTOM; s.spriteFrame = sf;
  385. const ut = n.getComponent(UITransform) || n.addComponent(UITransform); ut.setContentSize(w, h);
  386. return n;
  387. }
  388. // ---------------- 网格:圆角卡片 + 大符号 ----------------
  389. private fit(id: string) { return SYMFIT[id] || SYMFIT_DEFAULT; }
  390. private buildGrid() {
  391. const cardSize = this.cell * 0.92;
  392. for (let c = 0; c < COLS; c++) {
  393. this.cells[c] = []; this.ids[c] = []; this.symBaseY[c] = [];
  394. for (let r = 0; r < ROWS; r++) {
  395. const id = this.reelRand(c);
  396. const [x, y] = this.cellPos(c, r);
  397. const card = new Node(`card_${c}_${r}`); card.parent = this.node; card.setPosition(x, y, 0); card.setSiblingIndex(5);
  398. const cg = card.addComponent(Graphics);
  399. cg.fillColor = new Color(255, 255, 255, 215);
  400. this.roundRect(cg, -cardSize / 2, -cardSize / 2, cardSize, cardSize, cardSize * 0.24); cg.fill();
  401. cg.lineWidth = 3; cg.strokeColor = new Color(120, 175, 255, 240);
  402. this.roundRect(cg, -cardSize / 2, -cardSize / 2, cardSize, cardSize, cardSize * 0.24); cg.stroke();
  403. const f = this.fit(id);
  404. const scale = this.cell * f.s;
  405. const baseY = y - this.cell * f.oyf;
  406. this.symBaseY[c][r] = baseY;
  407. const node = new Node(`cell_${c}_${r}`); node.parent = this.node; node.setSiblingIndex(6);
  408. node.setPosition(x, baseY, 0); node.setScale(scale, scale, 1);
  409. const sk = node.addComponent(sp.Skeleton);
  410. sk.skeletonData = this.dataMap[id]; sk.premultipliedAlpha = false;
  411. sk.setAnimation(0, 'idle', true);
  412. this.cells[c][r] = sk; this.ids[c][r] = id;
  413. }
  414. }
  415. }
  416. private setSym(c: number, r: number, id: string) {
  417. const sk = this.cells[c][r];
  418. sk.skeletonData = this.dataMap[id]; sk.premultipliedAlpha = false;
  419. sk.setAnimation(0, 'idle', true);
  420. this.ids[c][r] = id;
  421. const [x, y] = this.cellPos(c, r);
  422. const f = this.fit(id);
  423. const scale = this.cell * f.s;
  424. const baseY = y - this.cell * f.oyf;
  425. this.symBaseY[c][r] = baseY;
  426. const nd = sk.node;
  427. nd.setScale(scale, scale, 1);
  428. nd.setPosition(x, baseY + this.cell * FEEDBACK.dropFrom, 0); // 从上方落入
  429. tween(nd).to(FEEDBACK.dropTime, { position: new Vec3(x, baseY, 0) }, { easing: 'backOut' }).start();
  430. }
  431. private spin() {
  432. if (this.spinning) return;
  433. if (this.holdActive) { this.holdRespin(); return; }
  434. const freeSpin = this.freeSpins > 0;
  435. if (!freeSpin && this.balance < this.bet) { this.flashMult('余额不足'); return; }
  436. if (freeSpin) {
  437. this.freeSpins -= 1;
  438. this.flashMult(`免费旋转 ${this.freeSpins}`);
  439. } else {
  440. this.balance -= this.bet; this.displayBalance = this.balance;
  441. this.balanceLabel.string = `${Math.floor(this.displayBalance)}`;
  442. this.pop(this.balanceLabel.node);
  443. }
  444. this.multiplier = 1; this.roundWin = 0; this.cascade = 0;
  445. this.winLabel.string = '0'; this.multLabel.string = '';
  446. this.spinning = true; this.setSpinEnabled(false);
  447. tween(this.spinBtn).by(FEEDBACK.spinDuration, { angle: -360 }, { easing: 'cubicOut' }).start(); // 联动旋转
  448. for (let c = 0; c < COLS; c++) {
  449. const strip = this.reel(c);
  450. const start = Math.floor(Math.random() * strip.length);
  451. for (let r = 0; r < ROWS; r++) this.setSym(c, r, strip[(start + r) % strip.length]);
  452. }
  453. this.scheduleOnce(() => this.resolve(), this.td(0.45));
  454. }
  455. private resolve() {
  456. if (this.cascade === 0) this.resolveFeatureTriggers();
  457. const count: Record<string, number> = {};
  458. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) count[this.ids[c][r]] = (count[this.ids[c][r]] || 0) + 1;
  459. const result = IS_CLUSTER ? this.findClusterWins() : this.findCountWins(count);
  460. if (result.groups.length === 0) { this.endRound(); return; }
  461. let cleared = 0; const winners: sp.Skeleton[] = [];
  462. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
  463. if (result.mask[c] && result.mask[c][r]) { winners.push(this.cells[c][r]); cleared++; }
  464. }
  465. const pay = this.calcPay(result.groups);
  466. this.roundWin += pay; this.balance += pay; this.winLabel.string = `${this.roundWin}`;
  467. this.pop(this.winLabel.node, 1.3); this.floatWin(pay);
  468. this.flashMult(`x${this.multiplier} +${pay}`);
  469. winners.forEach((sk) => {
  470. sk.setAnimation(0, 'win', false); sk.addAnimation(0, 'idle', true, 0);
  471. const nd = sk.node; const sc = nd.scale.x;
  472. tween(nd).to(0.1, { scale: new Vec3(sc * FEEDBACK.clearPop, sc * FEEDBACK.clearPop, 1) })
  473. .to(0.14, { scale: new Vec3(sc, sc, 1) }, { easing: 'backOut' }).start();
  474. });
  475. this.playParticle('coin_rain');
  476. this.cascade += 1;
  477. this.scheduleOnce(() => {
  478. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++)
  479. if (result.mask[c] && result.mask[c][r]) this.setSym(c, r, this.reelRand(c));
  480. this.multiplier = Math.min(CASCADE_MULT_MAX, this.multiplier + CASCADE_MULT_STEP);
  481. if (this.cascade >= MAX_CASCADE) { this.endRound(); return; }
  482. this.scheduleOnce(() => this.resolve(), this.td(0.4));
  483. }, this.td(0.6));
  484. }
  485. private calcPay(groups: {id: string; n: number}[]) {
  486. let total = 0;
  487. groups.forEach(({id, n}) => {
  488. const table = PAYTABLE[id] || {};
  489. const capped = Math.min(6, Math.max(MIN_MATCH, n));
  490. const mult = Number(table[String(capped)] ?? table[String(MIN_MATCH)] ?? capped);
  491. total += mult * this.bet;
  492. });
  493. return Math.max(1, Math.floor(total * this.multiplier * PAYOUT_SCALE));
  494. }
  495. private resolveFeatureTriggers() {
  496. if (SCATTER_RULES.enabled) {
  497. const scatterCount = this.countRole('scatter');
  498. if (scatterCount >= FREE_SPIN_TRIGGER) {
  499. const scatterPay = this.featurePay('scatter', scatterCount);
  500. this.freeSpins += FREE_SPIN_AWARD;
  501. if (scatterPay > 0) { this.roundWin += scatterPay; this.balance += scatterPay; this.winLabel.string = `${this.roundWin}`; }
  502. this.flashMult(`FREE SPINS +${FREE_SPIN_AWARD}`);
  503. this.playParticle('bigwin_glow');
  504. }
  505. }
  506. if (HOLD_RULES.enabled && !this.holdActive && this.countRole('bonus') >= HOLD_TRIGGER) {
  507. this.startHoldAndWin();
  508. }
  509. }
  510. private findCountWins(count: Record<string, number>) {
  511. const wildCount = this.countRole('wild');
  512. const mask = this.emptyMask();
  513. const groups = Object.keys(count).filter((id) => {
  514. if (this.isWild(id) || this.isScatter(id) || this.isBonus(id)) return false;
  515. return (count[id] || 0) + wildCount >= MIN_MATCH;
  516. }).map((id) => ({id, n: (count[id] || 0) + wildCount}));
  517. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
  518. const id = this.ids[c][r];
  519. if (groups.some((g) => g.id === id) || (this.isWild(id) && groups.length > 0)) mask[c][r] = true;
  520. }
  521. return {groups, mask};
  522. }
  523. private findClusterWins() {
  524. const groups: {id: string; n: number}[] = [];
  525. const mask = this.emptyMask();
  526. const regular = SYMBOLS.filter((id: string) => !this.isWild(id) && !this.isScatter(id) && !this.isBonus(id));
  527. regular.forEach((sid: string) => {
  528. const seen = this.emptyMask();
  529. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
  530. if (seen[c][r] || (this.ids[c][r] !== sid && !this.isWild(this.ids[c][r]))) continue;
  531. const stack: [number, number][] = [[c, r]];
  532. const cells: [number, number][] = [];
  533. seen[c][r] = true;
  534. while (stack.length) {
  535. const [x, y] = stack.pop()!;
  536. cells.push([x, y]);
  537. [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]].forEach(([nx, ny]) => {
  538. if (nx < 0 || nx >= COLS || ny < 0 || ny >= ROWS || seen[nx][ny]) return;
  539. const nid = this.ids[nx][ny];
  540. if (nid === sid || this.isWild(nid)) { seen[nx][ny] = true; stack.push([nx, ny]); }
  541. });
  542. }
  543. if (cells.length >= MIN_MATCH) {
  544. groups.push({id: sid, n: cells.length});
  545. cells.forEach(([x, y]) => { mask[x][y] = true; });
  546. }
  547. }
  548. });
  549. return {groups, mask};
  550. }
  551. private emptyMask() {
  552. const mask: boolean[][] = [];
  553. for (let c = 0; c < COLS; c++) {
  554. mask[c] = [];
  555. for (let r = 0; r < ROWS; r++) mask[c][r] = false;
  556. }
  557. return mask;
  558. }
  559. private featurePay(id: string, count: number) {
  560. const table = PAYTABLE[id] || {};
  561. const n = Math.min(6, Math.max(3, count));
  562. return Math.floor(Number(table[String(n)] ?? 0) * this.bet * PAYOUT_SCALE);
  563. }
  564. private role(id: string) { return SYMBOL_ROLE[id] || ''; }
  565. private isWild(id: string) { return this.role(id) === 'wild' || id === 'wild'; }
  566. private isScatter(id: string) { return this.role(id) === 'scatter' || id.indexOf('scatter') >= 0; }
  567. private isBonus(id: string) { return this.role(id) === 'bonus' || id.indexOf('coin') >= 0 || id === 'collect'; }
  568. private countRole(role: string) {
  569. let n = 0;
  570. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
  571. const id = this.ids[c][r];
  572. if ((role === 'wild' && this.isWild(id)) || (role === 'scatter' && this.isScatter(id)) || (role === 'bonus' && this.isBonus(id))) n++;
  573. }
  574. return n;
  575. }
  576. private startHoldAndWin() {
  577. this.holdActive = true;
  578. this.holdRespinsLeft = HOLD_RESPINS;
  579. this.holdWinTotal = 0;
  580. this.holdHeld = [];
  581. for (let c = 0; c < COLS; c++) {
  582. this.holdHeld[c] = [];
  583. for (let r = 0; r < ROWS; r++) {
  584. const held = this.isBonus(this.ids[c][r]);
  585. this.holdHeld[c][r] = held;
  586. if (held) this.holdWinTotal += this.coinValue(c, r);
  587. }
  588. }
  589. this.flashMult(`HOLD & WIN ${this.holdRespinsLeft}`);
  590. this.playParticle('bigwin_glow');
  591. }
  592. private holdRespin() {
  593. this.spinning = true; this.setSpinEnabled(false);
  594. let newCoin = false;
  595. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
  596. if (this.holdHeld[c] && this.holdHeld[c][r]) continue;
  597. const id = Math.random() < 0.18 ? this.randomBonusSymbol() : this.randRegular();
  598. this.setSym(c, r, id);
  599. if (this.isBonus(id)) {
  600. this.holdHeld[c][r] = true;
  601. this.holdWinTotal += this.coinValue(c, r);
  602. newCoin = true;
  603. }
  604. }
  605. this.holdRespinsLeft = newCoin ? HOLD_RESPINS : this.holdRespinsLeft - 1;
  606. this.flashMult(`HOLD & WIN ${this.holdRespinsLeft}`);
  607. this.scheduleOnce(() => {
  608. if (this.holdRespinsLeft <= 0 || this.allHoldFilled()) {
  609. this.balance += this.holdWinTotal; this.roundWin += this.holdWinTotal; this.winLabel.string = `${this.roundWin}`;
  610. this.holdActive = false; this.holdHeld = [];
  611. this.floatWin(this.holdWinTotal); this.playParticle('coin_rain');
  612. this.endRound();
  613. } else {
  614. this.spinning = false; this.setSpinEnabled(true);
  615. }
  616. }, this.td(0.6));
  617. }
  618. private allHoldFilled() {
  619. for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) if (!this.holdHeld[c][r]) return false;
  620. return true;
  621. }
  622. private coinValue(c: number, r: number) { return Math.floor(this.bet * (1 + ((c + r) % 5)) * PAYOUT_SCALE); }
  623. private randomBonusSymbol() {
  624. const bonus = SYMBOLS.filter((id: string) => this.isBonus(id));
  625. return bonus.length ? bonus[Math.floor(Math.random() * bonus.length)] : this.rand();
  626. }
  627. private randRegular() {
  628. const regular = SYMBOLS.filter((id: string) => !this.isWild(id) && !this.isScatter(id) && !this.isBonus(id));
  629. return regular.length ? regular[Math.floor(Math.random() * regular.length)] : this.rand();
  630. }
  631. private endRound() {
  632. if (this.roundWin > 0) { this.multLabel.string = ''; this.celebrate(this.roundWin); this.bossDefeated(); }
  633. else { this.multLabel.string = ''; this.bossTaunt(); }
  634. this.spinning = false; this.setSpinEnabled(true);
  635. if (this.holdActive) return;
  636. if (this.freeSpins > 0) this.scheduleOnce(() => this.spin(), this.td(0.9));
  637. else if (this.auto && this.balance >= this.bet) this.scheduleOnce(() => this.spin(), this.td(0.9));
  638. }
  639. private flashMult(text: string) {
  640. this.multLabel.string = text;
  641. const n = this.multLabel.node; n.setScale(0.6, 0.6, 1);
  642. tween(n).to(0.3, { scale: new Vec3(1, 1, 1) }, { easing: 'elasticOut' }).start();
  643. }
  644. private playParticle(id: string) {
  645. resources.load(`vfx/${id}`, JsonAsset, (err, asset) => {
  646. if (err) return;
  647. const n = new Node('p'); n.parent = this.node; n.setPosition(0, this.frameCY + this.frameH / 2, 0); n.setSiblingIndex(26);
  648. const ps = n.addComponent(ParticleSystem2D);
  649. applyParticleConfig(ps, asset.json, this.particleTex as SpriteFrame);
  650. this.scheduleOnce(() => ps.stopSystem(), 1.2);
  651. this.scheduleOnce(() => n.destroy(), 4);
  652. });
  653. }
  654. update(dt: number) {
  655. this.t += dt;
  656. if (Math.abs(this.displayBalance - this.balance) > 0.5) {
  657. this.displayBalance += (this.balance - this.displayBalance) * Math.min(1, dt * 6);
  658. this.balanceLabel.string = `${Math.floor(this.displayBalance)}`;
  659. }
  660. // 待机时每个果冻轻微上下呼吸,画面更"活"
  661. if (!this.spinning && this.cells.length) {
  662. for (let c = 0; c < COLS; c++) {
  663. for (let r = 0; r < ROWS; r++) {
  664. const sk = this.cells[c] && this.cells[c][r]; if (!sk) continue;
  665. const by = this.symBaseY[c] && this.symBaseY[c][r]; if (by === undefined) continue;
  666. const px = sk.node.position.x;
  667. sk.node.setPosition(px, by + Math.sin(this.t * 2.2 + c * 0.8 + r * 0.6) * this.cell * FEEDBACK.idleBob, 0);
  668. }
  669. }
  670. }
  671. }
  672. private rand() { return SYMBOLS[Math.floor(Math.random() * SYMBOLS.length)]; }
  673. private reel(c: number) {
  674. const strip = REEL_STRIPS[c % Math.max(1, REEL_STRIPS.length)];
  675. return strip && strip.length ? strip : SYMBOLS;
  676. }
  677. private reelRand(c: number) {
  678. const strip = this.reel(c);
  679. return strip[Math.floor(Math.random() * strip.length)];
  680. }
  681. private setSpinEnabled(on: boolean) {
  682. const btn = this.spinBtn.getComponent(Button); if (btn) btn.interactable = on;
  683. }
  684. private roundRect(g: Graphics, x: number, y: number, w: number, h: number, r: number) {
  685. g.moveTo(x + r, y); g.lineTo(x + w - r, y);
  686. g.arc(x + w - r, y + r, r, -Math.PI / 2, 0, false);
  687. g.lineTo(x + w, y + h - r); g.arc(x + w - r, y + h - r, r, 0, Math.PI / 2, false);
  688. g.lineTo(x + r, y + h); g.arc(x + r, y + h - r, r, Math.PI / 2, Math.PI, false);
  689. g.lineTo(x, y + r); g.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5, false); g.close();
  690. }
  691. }