TurntableUI.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { _decorator, Component, Node, Sprite, JsonAsset, Prefab, instantiate, Vec3, Vec4, Quat, RichText, EventHandler, Button } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { Http } from '../core/net/Http';
  4. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  5. import { ConfigData } from '../data/ConfigData';
  6. import { PrizeItem } from './PrizeItem';
  7. const { ccclass, property, requireComponent } = _decorator;
  8. @ccclass('TurntableUI')
  9. @requireComponent(Http)
  10. export class TurntableUI extends Component {
  11. @property({ tooltip: "资源加载组件", type: ResourceLoader }) public resourceLoader: ResourceLoader;
  12. @property({ tooltip: "奖品转盘", type: Node }) public prizeShowBox: Node;
  13. @property({ tooltip: "剩余次数文本", type: RichText }) public times: RichText;
  14. @property({ tooltip: "获取次数回调", type: EventHandler }) public getTimesBack: EventHandler;
  15. @property({ tooltip: "免费抽奖按钮文本", type: Node }) public freeBtnShow: Node;
  16. @property({ tooltip: "视频抽奖按钮文本", type: Node }) public vidoeBtnShow: Node;
  17. @property({ tooltip: "抽奖按钮", type: Button }) public drawBtn: Button;
  18. @property({ tooltip: "帮助按钮", type: Node }) public helpBtn: Node;
  19. @property({ tooltip: "关闭按钮", type: Node }) public closeBtn: Node;
  20. @property({ tooltip: "背景", type: Node }) public bg: Node;
  21. private turntableConfig = [];//转盘配置
  22. start() {
  23. this.getConfig();
  24. }
  25. private async getConfig() {
  26. let configData = DataSystem.getData(ConfigData);
  27. let data;
  28. if (configData.has("turntable")) {
  29. data = configData.get("turntable");
  30. } else {
  31. data = await (await this.resourceLoader.load<JsonAsset>("configs/turntable", JsonAsset)).json;
  32. }
  33. for (let i in data) {
  34. this.turntableConfig.push(data[i]);
  35. }
  36. this.initUI();
  37. }
  38. private async initUI() {
  39. for (var i = 0; i < this.turntableConfig.length; i++) {
  40. let prefab = await this.resourceLoader.load<Prefab>("prefabs/ui/turntable/turntablePrize", Prefab);
  41. let prizeItem = instantiate(prefab);
  42. prizeItem.parent = this.prizeShowBox;
  43. prizeItem.setPosition(new Vec3(0, 0));
  44. prizeItem.eulerAngles = new Vec3(0, 0, -60 * i);
  45. prizeItem.getComponent(PrizeItem).initUI(this.turntableConfig[i].icon, this.turntableConfig[i].num);
  46. }
  47. let result = await this.getComponent(Http).send("/api/draw/getDrawTimes");
  48. if (result && result.code == 0) {
  49. this.times.string = "<color=#ffffff>剩余次数:</color><color=#ffd522>" + (result.data.freeTimes + result.data.videoTimes) + "</color>";
  50. if (!result.data.freeTimes) {
  51. this.freeBtnShow.active = false;
  52. this.vidoeBtnShow.active = true;
  53. }
  54. if (!result.data.videoTimes) {
  55. this.drawBtn.interactable = false;//按键事件禁用
  56. this.drawBtn.getComponent(Sprite).grayscale = true;//按键置灰
  57. }
  58. this.getTimesBack.emit([result.data.freeTimes, result.data.videoTimes, this.turntableConfig]);
  59. }
  60. }
  61. private initBtn() {
  62. this.closeBtn.active = true;
  63. this.helpBtn.active = true;
  64. }
  65. private disableBtn() {
  66. this.closeBtn.active = false;
  67. this.helpBtn.active = false;
  68. this.drawBtn.interactable = false;//按键事件禁用
  69. }
  70. public onClose() {
  71. this.bg.active = false;
  72. }
  73. }