MouseAutoView.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import { MouseConst } from "./MouseConst";
  2. namespace DFortuneAutoNSpace {
  3. export const Rounds = [10, 30, 50, 80, 100];
  4. export const LostArea = [0.5, 1];
  5. export const WinMuls = 5000;
  6. export const WinMaxMuls = 500;
  7. export const Colors = {
  8. MainNormal: '#FFC824', //主色调 亮色 hand、okbtn、数字选择中等
  9. MainDisable: '#937835',
  10. SliderLineBg: '#30303B', //滑动条线的底色
  11. SliderAreaNormal: '#878787',
  12. SliderAreaDisable: '#58585D',
  13. SliderHanderNormal: '#EAB82A',
  14. SliderHanderDisable: '#947935',
  15. RoundNormalLab: '#6F6F78',
  16. RoundHoverLab: '#66583D',
  17. OkBtnDisable: '#66573D',
  18. OkBtnLabelDisable: '#837762',
  19. OkBtnLabelNormal: '#FFF4D8',
  20. };
  21. }
  22. /**@description 自动界面 */
  23. const { ccclass, property } = cc._decorator;
  24. @ccclass
  25. export default class DFortuneAutoView extends cc.Component {
  26. @property(cc.Node)
  27. maskBg: cc.Node = null;
  28. @property(cc.Node)
  29. root: cc.Node = null;
  30. @property(cc.Node)
  31. content: cc.Node = null;
  32. @property(cc.Node)
  33. showCover: cc.Node = null; //弹出时的遮盖
  34. @property(cc.Button)
  35. btnClose: cc.Button = null;
  36. @property(cc.Button)
  37. btnMore: cc.Button = null;
  38. @property(cc.Button)
  39. btnOK: cc.Button = null;
  40. @property(cc.Node)
  41. userNode: cc.Node = null;
  42. @property(cc.Node)
  43. betNode: cc.Node = null;
  44. @property(cc.Node)
  45. winNode: cc.Node = null;
  46. @property(cc.Node)
  47. roundNode: cc.Node = null;
  48. @property(cc.Node)
  49. loseLimitNode: cc.Node = null;
  50. @property(cc.Node)
  51. winLimitNode: cc.Node = null;
  52. @property(cc.Node)
  53. maxWinLimitNode: cc.Node = null;
  54. roundBtns: cc.Button[] = [];
  55. loseSlider: cc.Slider = null;
  56. winSlider: cc.Slider = null;
  57. maxSlider: cc.Slider = null;
  58. autoData: NSlots.IAutoData = {
  59. round: 0,
  60. loseLimit: 0,
  61. bet: 0
  62. };
  63. private _isDisplay: boolean = false;
  64. private _isCanSlider: boolean = false; //选了局数才行
  65. private _isCanOk: boolean = false; //选了loselimit才行
  66. private callback: Function = null;
  67. /**@description 读取十六进制颜色值 */
  68. private getColor(hex: string) {
  69. return (new cc.Color()).fromHEX(hex);
  70. }
  71. loseArea: number[] = [0, 0];
  72. winArea: number[] = [0, 0];
  73. maxArea: number[] = [0, 0];
  74. protected onLoad() {
  75. this.root.opacity = 0;
  76. this.init();
  77. this.initColor();
  78. this.updateMoreUI();
  79. this.updateOkUI();
  80. this.updateSlidersNode();
  81. this.root.height = this.content.height;
  82. this.root.getComponent(cc.Widget).updateAlignment();
  83. let dstY = this.root.y;
  84. this.root.getComponent(cc.Widget).enabled = false;
  85. this.root.y = this.root.y - this.root.height;
  86. this.scheduleOnce(() => {
  87. cc.tween(this.root).set({ opacity: 255 })
  88. .to(0.25, { y: dstY }, { easing: 'quintOut' })
  89. .call(() => this.show()).start();
  90. this.showCover.height = this.content.height;
  91. this.showCover.active = true;
  92. this.showCover.opacity = 255;
  93. }, 0.05);
  94. this.maskBg.getComponent(cc.Button).interactable = false;
  95. }
  96. show() {
  97. this.maskBg.getComponent(cc.Button).interactable = true;
  98. cc.tween(this.showCover).to(0.15, { opacity: 0 }).call(() => {
  99. this.showCover.active = false;
  100. this.showCover.opacity = 0;
  101. }).start();
  102. }
  103. protected start(): void {
  104. this.btnClose.node.on('click', this.onBtnClose, this);
  105. this.maskBg.on('click', this.onBtnClose, this);
  106. this.btnMore.node.on('click', this.onBtnMore, this);
  107. this.btnOK.node.on('click', this.onBtnOk, this);
  108. this.loseSlider.node.on('slide', this.onLoseSlider, this);
  109. this.winSlider.node.on('slide', this.onWinSlider, this);
  110. this.maxSlider.node.on('slide', this.onMaxSlider, this);
  111. }
  112. protected init() {
  113. //round
  114. for (let index = 0; index < 5; index++) {
  115. let btn = this.roundNode.getChildByName('btnsNode').getChildByName('btn_' + index).getComponent(cc.Button);
  116. btn.node.getComponentInChildren(cc.Label).string = DFortuneAutoNSpace.Rounds[index] + '';
  117. this.roundBtns[index] = btn;
  118. btn.node.on('click', () => this.onBtnRound(btn, index), this);
  119. btn.node.on(cc.Node.EventType.MOUSE_ENTER, () => this.onMouseRoundBtn(btn, true), this);
  120. btn.node.on(cc.Node.EventType.MOUSE_LEAVE, () => this.onMouseRoundBtn(btn, false), this);
  121. }
  122. this.loseSlider = this.loseLimitNode.getComponentInChildren(cc.Slider);
  123. this.winSlider = this.winLimitNode.getComponentInChildren(cc.Slider);
  124. this.maxSlider = this.maxWinLimitNode.getComponentInChildren(cc.Slider);
  125. }
  126. //点击局数
  127. onBtnRound(btn: cc.Button, index: number) {
  128. this._isCanSlider = true;
  129. btn.interactable = false;
  130. this.autoData.round = DFortuneAutoNSpace.Rounds[index];
  131. this.loseArea[0] = this.autoData.round * DFortuneAutoNSpace.LostArea[0] * this.autoData.bet;
  132. this.loseArea[1] = this.autoData.round * DFortuneAutoNSpace.LostArea[1] * this.autoData.bet;
  133. this.roundBtns.forEach((b, idx) => {
  134. b.interactable = idx != index;
  135. let cstr = idx == index ? DFortuneAutoNSpace.Colors.MainNormal : DFortuneAutoNSpace.Colors.RoundNormalLab;
  136. b.node.getChildByName('desc').color = this.getColor(cstr);
  137. });
  138. this.updateSlidersNode();
  139. this._isCanOk && this.onLoseSlider(this.loseSlider);
  140. }
  141. //鼠标触摸局数按钮
  142. onMouseRoundBtn(btn: cc.Button, enter: boolean) {
  143. if (!btn.interactable) return;
  144. let cstr = enter ? DFortuneAutoNSpace.Colors.RoundHoverLab : DFortuneAutoNSpace.Colors.RoundNormalLab;
  145. btn.node.getChildByName('desc').color = this.getColor(cstr);
  146. }
  147. protected initColor() {
  148. this.userNode.getComponentInChildren(cc.Sprite).node.color = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  149. this.betNode.getComponentInChildren(cc.Sprite).node.color = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  150. this.winNode.getComponentInChildren(cc.Sprite).node.color = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  151. this.loseSlider.node.getComponent(cc.ProgressBar).barSprite.node.color = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  152. this.winSlider.node.getComponent(cc.ProgressBar).barSprite.node.color = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  153. this.maxSlider.node.getComponent(cc.ProgressBar).barSprite.node.color = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  154. this.loseSlider.node.getChildByName('Background').color = this.getColor(DFortuneAutoNSpace.Colors.SliderLineBg);
  155. this.winSlider.node.getChildByName('Background').color = this.getColor(DFortuneAutoNSpace.Colors.SliderLineBg);
  156. this.maxSlider.node.getChildByName('Background').color = this.getColor(DFortuneAutoNSpace.Colors.SliderLineBg);
  157. this.loseSlider.node.getComponent(cc.ProgressBar).progress = 0;
  158. this.winSlider.node.getComponent(cc.ProgressBar).progress = 0;
  159. this.maxSlider.node.getComponent(cc.ProgressBar).progress = 0;
  160. this.loseSlider.progress = 0;
  161. this.winSlider.progress = 0;
  162. this.maxSlider.progress = 0;
  163. let btns: cc.Button[] = [];
  164. btns.push(this.loseSlider.handle);
  165. btns.push(this.winSlider.handle);
  166. btns.push(this.maxSlider.handle);
  167. btns.forEach(b => {
  168. b.interactable = false;
  169. b.transition = cc.Button.Transition.COLOR;
  170. b.normalColor = b.hoverColor = b.pressedColor = this.getColor(DFortuneAutoNSpace.Colors.SliderHanderNormal);
  171. b.disabledColor = this.getColor(DFortuneAutoNSpace.Colors.SliderHanderDisable);
  172. });
  173. this.btnOK.interactable = false;
  174. this.btnOK.transition = cc.Button.Transition.COLOR;
  175. this.btnOK.normalColor = this.btnOK.hoverColor = this.btnOK.pressedColor = this.getColor(DFortuneAutoNSpace.Colors.MainNormal);
  176. this.btnOK.disabledColor = this.getColor(DFortuneAutoNSpace.Colors.OkBtnDisable);
  177. this.roundBtns.forEach((b, idx) => {
  178. b.node.getChildByName('desc').color = this.getColor(DFortuneAutoNSpace.Colors.RoundNormalLab);
  179. });
  180. }
  181. /**
  182. * @description
  183. * 初始化数据
  184. */
  185. public initData(uCoin: number, bet: number, win: number, cb?: Function) {
  186. this.autoData.bet = bet;
  187. this.callback = cb;
  188. this.userNode.getComponentInChildren(cc.Label).string = MouseConst.formatAmount(uCoin);
  189. this.betNode.getComponentInChildren(cc.Label).string = MouseConst.formatAmount(bet);
  190. this.winNode.getComponentInChildren(cc.Label).string = MouseConst.formatAmount(win);
  191. this.winArea[0] = 0;
  192. this.winArea[1] = DFortuneAutoNSpace.WinMuls * bet;
  193. this.maxArea[0] = 0;
  194. this.maxArea[1] = DFortuneAutoNSpace.WinMaxMuls * bet;
  195. }
  196. //滑动区域UI
  197. updateSlidersNode() {
  198. this.loseSlider.handle.interactable = this._isCanSlider;
  199. this.winSlider.handle.interactable = this._isCanSlider;
  200. this.maxSlider.handle.interactable = this._isCanSlider;
  201. this.loseSlider.enabled = this._isCanSlider;
  202. this.winSlider.enabled = this._isCanSlider;
  203. this.maxSlider.enabled = this._isCanSlider;
  204. let labs: cc.Label[] = [];
  205. labs.push(...this.loseLimitNode.getComponentsInChildren(cc.Label));
  206. labs.push(...this.winLimitNode.getComponentsInChildren(cc.Label));
  207. labs.push(...this.maxWinLimitNode.getComponentsInChildren(cc.Label));
  208. labs.forEach(l => {
  209. let cstr = this._isCanSlider ? DFortuneAutoNSpace.Colors.SliderAreaNormal : DFortuneAutoNSpace.Colors.SliderAreaDisable;
  210. l.node.color = this.getColor(cstr);
  211. });
  212. //一个特殊颜色
  213. this.loseLimitNode.getChildByName('desc_1').color = this.getColor(this._isCanSlider ? DFortuneAutoNSpace.Colors.MainNormal : DFortuneAutoNSpace.Colors.MainDisable);
  214. }
  215. //点击更多按钮的UI
  216. updateMoreUI() {
  217. this.btnMore.node.getComponentInChildren(cc.Label).string = this._isDisplay ? 'Oculto' : 'Mais';
  218. this.winLimitNode.opacity = 0;
  219. this.maxWinLimitNode.opacity = 0;
  220. this.winLimitNode.active = this._isDisplay;
  221. this.maxWinLimitNode.active = this._isDisplay;
  222. this.scheduleOnce(() => {
  223. this.winLimitNode.opacity = 255;
  224. this.maxWinLimitNode.opacity = 255;
  225. }, 0.01);
  226. }
  227. //ok按钮
  228. updateOkUI() {
  229. if (this.btnOK.interactable) return;
  230. this.btnOK.interactable = this._isCanOk;
  231. let cstr = this._isCanOk ? DFortuneAutoNSpace.Colors.OkBtnLabelNormal : DFortuneAutoNSpace.Colors.OkBtnLabelDisable;
  232. this.btnOK.node.getChildByName('desc').color = this.getColor(cstr);
  233. }
  234. onBtnMore() {
  235. this._isDisplay = !this._isDisplay;
  236. this.updateMoreUI();
  237. }
  238. onBtnOk() {
  239. this.close();
  240. }
  241. onBtnClose() {
  242. this.autoData = null;
  243. this.close();
  244. }
  245. onLoseSlider(slider: cc.Slider) {
  246. this._isCanOk = true;
  247. this.updateOkUI();
  248. let porocess = Math.round(slider.progress * 100);
  249. porocess = getNearestEven(porocess) / 100;
  250. slider.node.getComponent(cc.ProgressBar).progress = slider.progress = porocess;
  251. let coin = this.loseArea[0] + (this.loseArea[1] - this.loseArea[0]) * porocess;
  252. let desc = this.loseLimitNode.getChildByName('limit_coin').getComponent(cc.Label);
  253. desc.string = coin == 0 ? 'Nenhum' : MouseConst.formatAmount(coin);
  254. this.autoData.loseLimit = coin;
  255. }
  256. onWinSlider(slider: cc.Slider) {
  257. let porocess = Math.round(slider.progress * 100);
  258. porocess = getNearestEven(porocess) / 100;
  259. slider.node.getComponent(cc.ProgressBar).progress = slider.progress = porocess;
  260. let coin = (this.winArea[1] - this.winArea[0]) * porocess;
  261. let desc = this.winLimitNode.getChildByName('limit_coin').getComponent(cc.Label);
  262. desc.string = porocess == 0 ? 'Nenhum' : MouseConst.formatAmount(coin);
  263. this.autoData.winLimit = coin;
  264. }
  265. onMaxSlider(slider: cc.Slider) {
  266. let porocess = Math.round(slider.progress * 100);
  267. porocess = getNearestEven(porocess) / 100;
  268. slider.node.getComponent(cc.ProgressBar).progress = slider.progress = porocess;
  269. let coin = (this.maxArea[1] - this.maxArea[0]) * porocess;
  270. let desc = this.maxWinLimitNode.getChildByName('limit_coin').getComponent(cc.Label);
  271. desc.string = porocess == 0 ? 'Nenhum' : MouseConst.formatAmount(coin);
  272. this.autoData.maxLimit = coin;
  273. }
  274. close() {
  275. cc.tween(this.root)
  276. .by(0.25, { y: -cc.winSize.height }, { easing: 'quintIn' })
  277. .call(() => {
  278. this.node.destroy();
  279. this?.callback(this.autoData);
  280. this.callback = null;
  281. }).start();
  282. }
  283. }
  284. function getNearestEven(num: number) {
  285. if (num % 2 === 0) { // 如果num已经是偶数
  286. return num;
  287. } else { // 否则,找到更接近的偶数
  288. if ((num + 1) % 2 === 0) {
  289. return num + 1;
  290. } else {
  291. return num - 1;
  292. }
  293. }
  294. }