YZ_Turntable.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import { utils } from "./Utils";
  2. import { YZ_Reward, BannerLocation, ViewLocation } from "./YZ_Constant";
  3. import CompatibleTool from "./CompatibleTool";
  4. import YZ_LocalStorage from "./YZ_LocalStorage";
  5. const { ccclass, property } = cc._decorator;
  6. // const gearInfo = [200, 100, 300, 400, 800, 500, 1000, 4000];
  7. const ST_TurntableCounts = "turntable_counts";
  8. const ST_TurntableTimes = "turntable_times";
  9. /**
  10. * 转盘
  11. */
  12. @ccclass
  13. export default class YZ_Turntable extends cc.Component {
  14. @property(cc.Node)
  15. spinBtn: cc.Node = null;
  16. @property(cc.Sprite)
  17. wheelSp: cc.Sprite = null;
  18. @property(cc.Sprite)
  19. greaSprite: cc.Sprite = null;
  20. @property(cc.SpriteFrame)
  21. firstSpriteFrame: cc.SpriteFrame = null;
  22. @property(cc.SpriteFrame)
  23. secondSpriteFrame: cc.SpriteFrame = null;
  24. @property(cc.SpriteFrame)
  25. lastSpriteFrame: cc.SpriteFrame = null;
  26. @property({ type: cc.AudioClip })
  27. effectAudio: cc.AudioClip = null;
  28. @property(cc.Node)
  29. panel: cc.Node = null;
  30. @property(cc.Node)
  31. videoIcon: cc.Node = null
  32. @property(cc.Node)
  33. spriteDouble: cc.Node = null
  34. @property(cc.Node)
  35. spritePass: cc.Node = null
  36. @property(cc.Node)
  37. tirpNode: cc.Node = null
  38. @property(cc.Node)
  39. checked: cc.Node = null
  40. @property(cc.Node)
  41. closeBtn: cc.Node = null
  42. @property(cc.Node)
  43. videoBtn: cc.Node = null
  44. @property(cc.Node)
  45. resultMsg: cc.Node = null
  46. @property(cc.Label)
  47. turnCountLbl: cc.Label = null
  48. @property(sp.Skeleton)
  49. luckBox: sp.Skeleton = null;
  50. @property(cc.ProgressBar)
  51. countBar: cc.ProgressBar = null;
  52. @property(cc.Node)
  53. normalBtn: cc.Node = null
  54. @property(cc.Node)
  55. normalCloseBtn: cc.Node = null
  56. @property(cc.Node)
  57. topCloseBtn: cc.Node = null
  58. /**
  59. * max: 15,
  60. min: 2,
  61. */
  62. maxSpeed: number = 12;
  63. /**
  64. * 减速前旋转时间
  65. * max: 5,
  66. min: 1,
  67. */
  68. duration: number = 3;
  69. /**
  70. * 加速度
  71. * max: 0.2,
  72. min: 0.01,
  73. */
  74. acc: number = 0.1;
  75. /**
  76. * 指定结束时的齿轮
  77. * max: 17,
  78. min: 0,
  79. */
  80. targetID: number = 0;
  81. /**
  82. * 旋转结束是否回弹
  83. *
  84. */
  85. springback: boolean = false;
  86. wheelState = 0;
  87. curSpeed = 0;
  88. spinTime = 0; //减速前旋转时间
  89. gearNum = 8;
  90. defaultAngle = 360 / 8 / 2 - 22; //修正默认角度
  91. gearAngle = 360 / this.gearNum; //每个齿轮的角度
  92. finalAngle = 0; //最终结果指定的角度
  93. effectFlag = 0; //用于音效播放
  94. audioID = 0;
  95. decAngle = 0;
  96. /**
  97. * 抽奖次数
  98. */
  99. _turntableCount: number = 0;
  100. /**
  101. * 抽奖次数
  102. */
  103. _turntablePlayCount: number = 0;
  104. get turntableCount() {
  105. return this._turntableCount;
  106. }
  107. set turntableCount(count: number) {
  108. this._turntableCount = count;
  109. YZ_LocalStorage.setItem(ST_TurntableTimes, new Date().toDateString());
  110. YZ_LocalStorage.setItem(ST_TurntableCounts, this._turntableCount);
  111. }
  112. get turntablePlayCount() {
  113. return this._turntablePlayCount;
  114. }
  115. luckBoxCount = 4;
  116. _updateProgress: boolean = false;
  117. _progress: number = 0;
  118. defaultProgress: number = 0.00000001;
  119. set turntablePlayCount(count: number) {
  120. if (this.luckBoxCount - count == 0) {
  121. this.luckBox.node.active = true;
  122. count = 0;
  123. this.countBar.progress = this.defaultProgress;
  124. this.luckBox.setAnimation(1, "dakai", false);
  125. this.scheduleOnce(() => {
  126. utils.SendEvent("转盘抽奖获取累计4次大奖!");
  127. this.showMsg(this.gearInfo[7]);
  128. if (this.rewardCallFunc) {
  129. let result: YZ_Reward = new YZ_Reward();
  130. result.rewardValue = this.gearInfo[7]
  131. this.rewardCallFunc(result);
  132. }
  133. }, 0.6);
  134. this.scheduleOnce(() => {
  135. this.luckBox.setAnimation(1, "kd", false);
  136. }, 2);
  137. this.scheduleOnce(() => {
  138. this.luckBox.node.runAction(cc.sequence(cc.scaleTo(0.2, 0.8), cc.callFunc(() => {
  139. this.luckBox.node.active = false;
  140. this.luckBox.node.scale = 2;
  141. })));
  142. }, 4);
  143. }
  144. this._turntablePlayCount = count;
  145. YZ_LocalStorage.setItem("turntablePlayCount", this._turntablePlayCount);
  146. this._progress = count / this.luckBoxCount;
  147. this.turnCountLbl.string = (this.luckBoxCount - count) + "";
  148. this.turnCountLbl.node.runAction(cc.sequence(cc.scaleTo(0.3, 1.8), cc.scaleTo(0.3, 1)));
  149. }
  150. /**
  151. * 奖励回调
  152. */
  153. rewardCallFunc: Function = null;
  154. gearInfo = [200, 100, 300, 400, 800, 500, 1000, 4000];
  155. /**
  156. * 初始化事件回调
  157. * @param closeCallFunc
  158. * @param rewardCallFunc
  159. */
  160. init(closeCallFunc: Function, rewardCallFunc: Function, gearInfo: [], gearImagePath?: string) {
  161. // this.closeCallFunc = closeCallFunc;
  162. this.rewardCallFunc = rewardCallFunc;
  163. if (gearInfo.length > 0) {
  164. this.gearInfo = gearInfo;
  165. }
  166. if (gearImagePath) {
  167. utils.showLog("配置为服务器的奖励图片:", gearImagePath);
  168. CompatibleTool.LoadRes(gearImagePath, (err, res) => {
  169. if (!err && cc.isValid(this) && this.greaSprite) {
  170. this.greaSprite.spriteFrame = new cc.SpriteFrame(res);
  171. }
  172. });
  173. }
  174. }
  175. onLoad() {
  176. if (utils.otherConfig && utils.otherConfig.group) {
  177. this.node.group = utils.otherConfig.group;
  178. }
  179. this.wheelSp.node.rotation = this.defaultAngle;
  180. // this.spinBtn.on(cc.Node.EventType.TOUCH_END, (event) => {
  181. // this.startTurnTable();
  182. // });
  183. utils.SendEvent("转盘抽奖-显示成功!");
  184. this.initCount();
  185. if (this.turntableCount <= 0) {
  186. this.normalBtn.active = false;
  187. this.videoBtn.active = true;
  188. this.normalCloseBtn.active = true;
  189. utils.showScaleAction(this.videoBtn, null, false, false);
  190. } else {
  191. this.normalCloseBtn.active = false;
  192. utils.showScaleAction(this.normalBtn, null, false, false);
  193. }
  194. utils.showSkipBtn(this.closeBtn, true);
  195. // if (this.turntableCount <= 0 && !CC_DEBUG) {
  196. // let result = utils.controView(ViewLocation.turntable);
  197. // if (result.is_open === true) {
  198. // utils.delayShowNode(this.tirpNode);
  199. // this.checked.active = result.isSelect;
  200. // this.spritePass.active = !this.checked.active;
  201. // this.spriteDouble.active = this.checked.active;
  202. // this.videoIcon.active = this.checked.active;
  203. // this.closeBtn.active = false;
  204. // this.videoBtn.active = true;
  205. // this.spinBtn.active = false;
  206. // } else {
  207. // this.checked.active = false;
  208. // this.videoBtn.active = false;
  209. // this.spinBtn.active = true;
  210. // this.spinBtn.children[0].getComponent(cc.Sprite).spriteFrame = this.secondSpriteFrame;
  211. // }
  212. // }
  213. this.rewardCallFunc = utils.rewardCallFunc;
  214. if (cc.winSize.height < cc.winSize.width) {
  215. utils.adManager.HideBanner(BannerLocation.Game);
  216. } else {
  217. utils.adManager.ShowBanner(BannerLocation.Game);
  218. }
  219. }
  220. /**
  221. * 开始抽奖
  222. */
  223. startTurnTable() {
  224. if (this.wheelState !== 0) {
  225. return;
  226. }
  227. if (this.turntableCount <= 0) {
  228. if (this.checked.active || this.spinBtn.active) {
  229. utils.adManager.ShowVideo((ret, msg) => {
  230. if (ret) {
  231. this.turntableCount = 1;
  232. this.startTurnTable();
  233. utils.SendEvent("转盘抽奖获取奖励-成功!");
  234. } else {
  235. utils.showMsg("观看完整视频可以获取一次抽奖机会!");
  236. utils.SendEvent("转盘抽奖视频播放-失败!");
  237. this.onClose();
  238. }
  239. })
  240. return;
  241. } else {
  242. // AldUtils.SendEvent("转盘抽奖-未勾选!");
  243. this.onClose();
  244. return;
  245. }
  246. }
  247. this.turntableCount = 0;
  248. this.targetID = this.getTargetId();
  249. this.decAngle = 2 * 360; // 减速旋转两圈
  250. this.wheelState = 1;
  251. this.curSpeed = 0;
  252. this.spinTime = 0;
  253. }
  254. onCheck() {
  255. this.checked.active = !this.checked.active;
  256. this.spritePass.active = !this.checked.active;
  257. this.spriteDouble.active = this.checked.active;
  258. this.videoIcon.active = this.checked.active;
  259. // AldUtils.SendEvent(`转盘抽奖-用户${this.checked.active ? "点击勾选" : "取消勾选"}`);
  260. }
  261. onClose() {
  262. this.panel.runAction(cc.sequence(cc.scaleTo(0.3, 0).easing(cc.easeBackIn()), cc.callFunc(() => {
  263. this.node.destroy();
  264. })));
  265. }
  266. onEnable() {
  267. this.panel.scale = 0;
  268. let ratio: number = 1;
  269. if (cc.winSize.height < cc.winSize.width) {
  270. // 横屏游戏
  271. ratio = 0.6;
  272. } else {
  273. ratio = cc.winSize.width / 1080;
  274. }
  275. this.panel.runAction(cc.scaleTo(0.3, ratio).easing(cc.easeBackOut()));
  276. }
  277. onDestroy() {
  278. if (utils.turnTablePanelCloseFunc) {
  279. utils.turnTablePanelCloseFunc();
  280. utils.turnTablePanelCloseFunc = null;
  281. } else {
  282. utils.rewardCloseFunc && utils.rewardCloseFunc();
  283. utils.rewardCloseFunc = null;
  284. }
  285. utils.adManager.HideBanner(BannerLocation.Game)
  286. }
  287. checkBtnSpriteFrame() {
  288. if (this.turntableCount <= 0) {
  289. this.normalBtn.active = false;
  290. this.videoBtn.active = true;
  291. this.normalCloseBtn.active = true;
  292. utils.showScaleAction(this.videoBtn, null, false, false);
  293. } else {
  294. this.normalCloseBtn.active = false;
  295. }
  296. // if (this.turntableCount <= 0) {
  297. // let result = utils.controView(ViewLocation.turntable);
  298. // if (result.is_open === true) {
  299. // utils.delayShowNode(this.tirpNode);
  300. // this.checked.active = result.isSelect;
  301. // this.spritePass.active = !this.checked.active;
  302. // this.spriteDouble.active = this.checked.active;
  303. // this.videoIcon.active = this.checked.active;
  304. // this.closeBtn.active = false;
  305. // this.videoBtn.active = true;
  306. // this.spinBtn.active = false;
  307. // } else {
  308. // this.checked.active = false;
  309. // this.videoBtn.active = false;
  310. // this.spinBtn.children[0].getComponent(cc.Sprite).spriteFrame = this.lastSpriteFrame;
  311. // }
  312. // }
  313. }
  314. initCount() {
  315. let turntableCount = YZ_LocalStorage.getItem(ST_TurntableCounts);
  316. let day: string = YZ_LocalStorage.getItem(ST_TurntableTimes);
  317. let curDate = new Date();
  318. if (day != curDate.toDateString()) {
  319. this.turntableCount = 1;
  320. } else {
  321. this._turntableCount = turntableCount;
  322. }
  323. this.countBar.progress = this.defaultProgress;
  324. this.turntablePlayCount = YZ_LocalStorage.getItem("turntablePlayCount") ? YZ_LocalStorage.getItem("turntablePlayCount") : 0;
  325. this.countBar.progress = this.turntablePlayCount / this.luckBoxCount;
  326. utils.showLog("当前剩余抽奖次数:", this.turntableCount);
  327. }
  328. test() {
  329. if (this.wheelState !== 0) {
  330. return;
  331. }
  332. this.targetID = this.getTargetId();
  333. cc.log("结果:", this.targetID);
  334. this.decAngle = 2 * 360; // 减速旋转两圈
  335. this.wheelState = 1;
  336. this.curSpeed = 0;
  337. this.spinTime = 0;
  338. }
  339. getTargetId() {
  340. var ranNum = Math.random();//生成一个0-1的随机数
  341. var n = Math.random() / 4;//0-0.25随机数
  342. let result = 0;
  343. if (ranNum < 0.01) {
  344. result = 1; // 8
  345. }
  346. else if (ranNum < 0.04) {
  347. result = 0.9; // 7
  348. } else if (ranNum < 0.1) {
  349. result = 0.5; // 4
  350. } else if (ranNum < 0.2) {
  351. result = 0.7; // 5
  352. } else if (ranNum < 1) {
  353. result = Math.floor(Math.random() * 4) * 0.1; //0-4
  354. }
  355. return Math.floor(8 * result);
  356. }
  357. showMsg(gold) {
  358. let msg = cc.instantiate(this.resultMsg);
  359. cc.find("kuang/goldLbl", msg).getComponent(cc.Label).string = `${gold}`
  360. msg.active = true;
  361. msg.scale = 0.3;
  362. this.node.addChild(msg);
  363. msg.runAction(cc.scaleTo(0.3, this.panel.scale).easing(cc.easeBackOut()));
  364. this.scheduleOnce(() => {
  365. msg.destroy();
  366. msg.removeFromParent();
  367. }, 1.2);
  368. }
  369. showRes() {
  370. this.showMsg(this.gearInfo[this.targetID]);
  371. this.scheduleOnce(() => {
  372. this.turntablePlayCount++;
  373. }, 1);
  374. if (this.rewardCallFunc) {
  375. let result: YZ_Reward = new YZ_Reward();
  376. result.rewardValue = this.gearInfo[this.targetID]
  377. this.rewardCallFunc(result);
  378. }
  379. this.checkBtnSpriteFrame();
  380. }
  381. update(dt) {
  382. if (this.countBar.progress < this._progress) {
  383. this.countBar.progress += dt;
  384. }
  385. if (this.wheelState === 0) {
  386. return;
  387. }
  388. // 播放音效有可能卡
  389. this.effectFlag += this.curSpeed;
  390. if (this.effectFlag >= this.gearAngle) {
  391. this.audioID = cc.audioEngine.playEffect(this.effectAudio, false);
  392. this.effectFlag = 0;
  393. }
  394. if (this.wheelState == 1) {
  395. // cc.log('....加速,speed:' + this.curSpeed);
  396. this.spinTime += dt;
  397. this.wheelSp.node.rotation = this.wheelSp.node.rotation + this.curSpeed;
  398. if (this.curSpeed <= this.maxSpeed) {
  399. this.curSpeed += this.acc;
  400. }
  401. else {
  402. if (this.spinTime < this.duration) {
  403. return;
  404. }
  405. // cc.log('....开始减速');
  406. //设置目标角度
  407. this.finalAngle = 360 - this.targetID * this.gearAngle + this.defaultAngle;
  408. this.maxSpeed = this.curSpeed;
  409. if (this.springback) {
  410. this.finalAngle += this.gearAngle;
  411. }
  412. this.wheelSp.node.rotation = this.finalAngle;
  413. this.wheelState = 2;
  414. }
  415. }
  416. else if (this.wheelState == 2) {
  417. // cc.log('......减速');
  418. var curRo = this.wheelSp.node.rotation; //应该等于finalAngle
  419. var hadRo = curRo - this.finalAngle;
  420. this.curSpeed = this.maxSpeed * ((this.decAngle - hadRo) / this.decAngle) + 0.2;
  421. this.wheelSp.node.rotation = curRo + this.curSpeed;
  422. if ((this.decAngle - hadRo) <= 0) {
  423. // cc.log('....停止');
  424. this.wheelState = 0;
  425. this.wheelSp.node.rotation = this.finalAngle;
  426. if (this.springback) {
  427. //倒转一个齿轮
  428. // var act = new cc.rotateBy(0.6, -this.gearAngle);
  429. var act = cc.rotateBy(0.6, -this.gearAngle);
  430. var seq = cc.sequence(cc.delayTime(0.2), act, cc.callFunc(this.showRes, this));
  431. this.wheelSp.node.runAction(seq);
  432. }
  433. else {
  434. this.showRes();
  435. }
  436. }
  437. }
  438. }
  439. }