Turntable.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import GameM, { VIDEO_TYPE, AUDIO_TYPE } from "../manager/GameM";
  2. import TurntableItem from "../prefabs/TurntableItem";
  3. import UiM, { PANEL_NAME } from "../manager/UiM";
  4. import Gift from "./Gift";
  5. import CommonData, { HTTP_TYPE, MONEY_TYPE } from "../datas/CommonData";
  6. import EffectNode from "./EffectNode";
  7. import AdM from "../manager/AdM";
  8. import Sciencen_M from "../utils/Sciencen_M";
  9. import RoleManageData from "../datas/RoleManageData";
  10. import TurntableData from "../datas/TurntableData";
  11. import GuideMng from "../manager/GuideMng";
  12. import { PlayerPrefs } from "../tools/MyExtends";
  13. import { MateData } from "../datas/MateData";
  14. import { Utils } from "../utils/Utils";
  15. const { ccclass, property } = cc._decorator;
  16. @ccclass
  17. export default class Turntable extends cc.Component {
  18. @property(cc.Label)
  19. labDes: cc.Label = null;
  20. @property(cc.Label)
  21. labLeftTimes: cc.Label = null;
  22. @property(cc.Label)
  23. labLeftCardCount: cc.Label = null;
  24. @property(cc.Node)
  25. adIcon: cc.Node = null;
  26. @property(cc.Node)
  27. labCardTip: cc.Node = null;
  28. @property(cc.Node)
  29. turnIn: cc.Node = null;
  30. @property(cc.Node)
  31. ruleNode: cc.Node = null;
  32. @property(cc.Label)
  33. labRule: cc.Label = null;
  34. @property(cc.ParticleSystem)
  35. starAni: cc.ParticleSystem = null;
  36. @property(sp.Skeleton)
  37. runAni: sp.Skeleton = null;
  38. @property([cc.Node])
  39. extraNode: cc.Node[] = []
  40. @property(cc.Sprite)
  41. extraProgress: cc.Sprite = null
  42. private lock = false
  43. // LIFE-CYCLE CALLBACKS:
  44. // onLoad () {}
  45. private data = null
  46. private endAngle = 0
  47. private startDraw = false
  48. private slowDis = 720
  49. private slowAngle = 0
  50. private perAdd = 10
  51. private perMinus = 0
  52. private itemList: Array<cc.Node> = null
  53. //需要视频次数
  54. private needVideoTimes = 8
  55. onEnable() {
  56. this.labLeftTimes.string = TurntableData.Instance.turntableTimes.toString()
  57. this.labLeftCardCount.string = GameM.commonData.roleData.turntableCard.toString()
  58. this.adIcon.active = (TurntableData.Instance.turntableTimes <= this.needVideoTimes && GameM.commonData.roleData.turntableCard <= 0);
  59. this.labCardTip.active = GameM.commonData.roleData.turntableCard > 0
  60. if (GuideMng.Ins.CurGuideId == 15) {
  61. if (PlayerPrefs.GetInt("GGuide15", 0) != 1) {
  62. //转盘引导
  63. GuideMng.Ins.ResumeGuide();
  64. }
  65. }
  66. AdM.showBanner()
  67. this.updateExtraInfo()
  68. }
  69. onDisable() {
  70. AdM.destroyBanner()
  71. }
  72. start() {
  73. if (!TurntableData.Instance.turntableCft) {
  74. this.lock = true
  75. TurntableData.Instance.getTurntableCfg()
  76. }
  77. else {
  78. this.init()
  79. }
  80. }
  81. // upRewardInfo:1,100;2,200
  82. async init() {
  83. this.runAni.node.active = false
  84. let turntableCft = TurntableData.Instance.turntableCft
  85. let datas = new Array<any>()
  86. // let turntableUseCard = GameM.commonData.roleData.turntableUseCard
  87. for (let entry of turntableCft) {
  88. if (entry.reward == 2) {
  89. let upRewardInfo: string = entry.upRewardInfo
  90. let a = upRewardInfo.split(";")
  91. let currentArr = this.checkCurrentNum(a)
  92. if (currentArr.length > 0) {
  93. entry.num = new Number(currentArr[1]).valueOf()
  94. }
  95. }
  96. datas.push(entry)
  97. }
  98. let typeTemp = new Array<any>()
  99. for (let entry of datas) {
  100. if (entry.reward == 2) {
  101. typeTemp.push(entry)
  102. }
  103. }
  104. typeTemp = typeTemp.sort((a, b) => {
  105. return a.num > b.num ? 1 : -1
  106. })
  107. for (let i = 0; i < typeTemp.length; i++) {
  108. if (i < 4) {
  109. typeTemp[i].moneyLevel = i + 1
  110. } else {
  111. typeTemp[i].moneyLevel = 4
  112. }
  113. }
  114. this.itemList = new Array<cc.Node>()
  115. let pre = await Utils.loadResPromise('prefabs/TurnTableItem')
  116. let item;
  117. let s
  118. for (var i = 0; i < datas.length; i++) {
  119. item = cc.instantiate(pre)
  120. s = item.getComponent(TurntableItem)
  121. for (let entry of typeTemp) {
  122. if (entry.index == datas[i].index) {
  123. datas[i] = entry
  124. break
  125. }
  126. }
  127. s.init(datas[i])
  128. this.itemList.push(item)
  129. this.turnIn.addChild(item)
  130. }
  131. this.labRule.string = GameM.commonData.TxtCft[0].turn_table;
  132. this.lock = false
  133. // this.dengNode.runAction(cc.repeatForever(cc.sequence(cc.fadeIn(0.3), cc.fadeOut(0.3))))
  134. }
  135. checkCurrentNum(arr: Array<string>): Array<string> {
  136. let turntableUseCard = GameM.commonData.roleData.turntableUseCard
  137. let turntableTimes = TurntableData.Instance.turntableTimes
  138. let nowCount = turntableUseCard + (10 - turntableTimes)
  139. let resultArray = new Array<string>()
  140. for (let entry of arr) {
  141. let a = entry.split(",")
  142. if (new Number(a[0]).valueOf() <= nowCount) {
  143. resultArray = a
  144. }
  145. }
  146. return resultArray
  147. }
  148. updateCurrentNum() {
  149. let turntableCft = TurntableData.Instance.turntableCft
  150. for (let entry of turntableCft) {
  151. if (entry.reward != 2) {
  152. continue
  153. } else {
  154. let upRewardInfo: string = entry.upRewardInfo
  155. let a = upRewardInfo.split(";")
  156. let currentArr = this.checkCurrentNum(a)
  157. if (currentArr.length > 0) {
  158. entry.num = new Number(currentArr[1]).valueOf()
  159. let childList = this.turnIn.children
  160. for (let child of childList) {
  161. let turntableItem = child.getComponent(TurntableItem)
  162. if (turntableItem) {
  163. if (turntableItem.data.index == entry.index) {
  164. turntableItem.updateNum(entry.num)
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. clickRule() {
  173. GameM.audioM.playEffect(AUDIO_TYPE.button)
  174. if (this.lock) {
  175. return
  176. }
  177. this.ruleNode.active = true
  178. }
  179. clickCloseRule() {
  180. this.ruleNode.active = false
  181. }
  182. /** 开始抽奖 */
  183. clickDraw() {
  184. if (this.lock) {
  185. return
  186. }
  187. if (GameM.commonData.roleData.turntableCard > 0) {
  188. this.lock = true
  189. GameM.audioM.playEffect(AUDIO_TYPE.turntableDraw)
  190. this.data = TurntableData.Instance.getTurnTable(0)
  191. let index = this.data.index - 1
  192. this.perAdd = 10
  193. this.turnIn.angle += 360 * 5
  194. this.endAngle = index * -60
  195. this.slowAngle = this.endAngle + this.slowDis
  196. this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5))
  197. this.startDraw = true
  198. this.starAni.resetSystem()
  199. this.starAni.node.active = true
  200. GameM.commonData.roleData.turntableTotal++
  201. GameM.commonData.updateRoleData()
  202. this.updateCurrentNum()
  203. }
  204. else if (TurntableData.Instance.turntableTimes > 0) {
  205. this.lock = true
  206. if (TurntableData.Instance.turntableTimes > this.needVideoTimes) {
  207. GameM.audioM.playEffect(AUDIO_TYPE.turntableDraw)
  208. this.data = TurntableData.Instance.getTurnTable(0)
  209. let index = this.data.index - 1
  210. this.perAdd = 10
  211. this.turnIn.angle += 360 * 5
  212. this.endAngle = index * -60
  213. this.slowAngle = this.endAngle + this.slowDis
  214. this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5))
  215. this.startDraw = true
  216. this.starAni.resetSystem()
  217. this.starAni.node.active = true
  218. GameM.commonData.roleData.turntableTotal++
  219. GameM.commonData.updateRoleData()
  220. this.updateCurrentNum()
  221. }
  222. else {
  223. if (GameM.commonData.isVideoTest) {
  224. this.onTurnTableVideoEnd()
  225. } else {
  226. let state = GameM.adM.watchVideo(VIDEO_TYPE.turnTable)
  227. if (!state) {
  228. this.lock = false
  229. }
  230. }
  231. }
  232. }
  233. else {
  234. EffectNode.instance.PlayTip('次数不足,请等待重置')
  235. }
  236. }
  237. onTurnTableVideoEnd() {
  238. GameM.audioM.playEffect(AUDIO_TYPE.turntableDraw)
  239. this.data = TurntableData.Instance.getTurnTable(1)
  240. let index = this.data.index - 1
  241. this.perAdd = 10
  242. this.turnIn.angle += 360 * 5
  243. this.endAngle = index * -60
  244. this.slowAngle = this.endAngle + this.slowDis
  245. this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5))
  246. this.startDraw = true
  247. this.starAni.resetSystem()
  248. this.starAni.node.active = true
  249. EffectNode.instance.PlayTip("感谢您的观看,额外奖励已发放");
  250. GameM.commonData.roleData.turntableTotal++
  251. GameM.commonData.updateRoleData()
  252. }
  253. onTurnTableVideoFail() {
  254. this.startDraw = false
  255. this.lock = false
  256. }
  257. update(dt) {
  258. if (this.startDraw) {
  259. this.turnIn.angle -= this.perAdd
  260. //开始减速
  261. if (this.turnIn.angle <= this.slowAngle) {
  262. if (this.perAdd <= this.perMinus * 4) {
  263. this.perAdd -= this.perMinus * 0.01
  264. }
  265. else {
  266. this.perAdd -= this.perMinus
  267. }
  268. }
  269. if (this.turnIn.angle <= this.endAngle) {
  270. this.startDraw = false
  271. this.starAni.node.active = false
  272. this.runAni.node.active = true
  273. this.runAni.setAnimation(0, 'animation', false)
  274. GameM.audioM.playEffect(AUDIO_TYPE.turntableDrawEnd)
  275. if (GameM.commonData.roleData.turntableCard > 0) {
  276. RoleManageData.Instance.updateTurntableCardCount()
  277. this.labLeftCardCount.string = GameM.commonData.roleData.turntableCard.toString()
  278. this.adIcon.active = (TurntableData.Instance.turntableTimes <= this.needVideoTimes && GameM.commonData.roleData.turntableCard <= 0);
  279. this.labCardTip.active = GameM.commonData.roleData.turntableCard > 0
  280. AdM.onSendEvent(`item_${MONEY_TYPE.turnTableCard}`, `使用道具${MONEY_TYPE.turnTableCard}`, 'item')
  281. } else {
  282. TurntableData.Instance.updateTurntableTimes()
  283. this.labLeftTimes.string = TurntableData.Instance.turntableTimes.toString()
  284. this.adIcon.active = TurntableData.Instance.turntableTimes <= this.needVideoTimes;
  285. this.labCardTip.active = GameM.commonData.roleData.turntableCard > 0
  286. }
  287. this.updateExtraInfo()
  288. this.scheduleOnce(() => {
  289. this.lock = false
  290. UiM.Instance.checkDelayShow(() => {
  291. if (this.data.reward == 1) {
  292. //GameM.commonData.updateGiftCacheNum(this.data.num)
  293. UiM.Instance.giftNode.active = true
  294. let gift = UiM.Instance.giftNode.getComponent(Gift)
  295. gift.init(MONEY_TYPE.box, String(this.data.num), null, false, 0, null, -1, true);
  296. }
  297. else if (this.data.reward == 2) {
  298. UiM.Instance.giftNode.active = true
  299. let gift = UiM.Instance.giftNode.getComponent(Gift)
  300. let num = Sciencen_M.instance.accMul(GameM.commonData.goldSecond.toString(), (this.data.num * 60).toString())
  301. gift.init(MONEY_TYPE.gold, num, null, false, 0, null, -1, true)
  302. }
  303. else if (this.data.reward == 3) {
  304. UiM.Instance.giftNode.active = true
  305. let gift = UiM.Instance.giftNode.getComponent(Gift)
  306. gift.init(MONEY_TYPE.redMoney, String(this.data.num), null, false, 0, null, -1, true)
  307. }
  308. else if (this.data.reward == 4) {
  309. UiM.Instance.giftNode.active = true
  310. let gift = UiM.Instance.giftNode.getComponent(Gift)
  311. gift.init(MONEY_TYPE.talentCoin, String(this.data.num), null, false, 0, null, -1, true)
  312. }
  313. })
  314. }, 0.7)
  315. }
  316. }
  317. }
  318. clickClose() {
  319. GameM.audioM.playEffect(AUDIO_TYPE.button)
  320. if (this.lock) {
  321. return
  322. }
  323. // this.node.active = false
  324. UiM.Instance.offPanel(PANEL_NAME.TurnTableNode, false, () => {
  325. AdM.createInter(6);
  326. })
  327. }
  328. clickIconShowTip() {
  329. // let qipao = this.node.getChildByName('qipao')
  330. // qipao.active = true
  331. // this.scheduleOnce(() => {
  332. // if (cc.isValid(qipao)) {
  333. // qipao.active = false
  334. // }
  335. // }, 2)
  336. }
  337. updateExtraInfo() {
  338. let getTimes = GameM.commonData.roleData.turntableExtrasReward
  339. let totalTimes = GameM.commonData.getDailyFinishTimesDataByType(2)
  340. // this.extraProgress.fillRange = 0
  341. let range = 0
  342. for (let i = 0; i < this.extraNode.length; i++) {
  343. let cfgInfo = TurntableData.Instance.turntableExtrasCfg[i]
  344. let getType = 0
  345. let propType = cfgInfo[1]
  346. let num = cfgInfo[2]
  347. if (cfgInfo[0] <= totalTimes) {
  348. range = i / (this.extraNode.length - 1)
  349. if (getTimes.indexOf(i) >= 0) {
  350. getType = 2
  351. } else {
  352. getType = 1
  353. }
  354. } else {
  355. getType = 0
  356. }
  357. this.showReward(getType, propType, num, this.extraNode[i], totalTimes, cfgInfo[0])
  358. }
  359. this.extraProgress.fillRange = range
  360. }
  361. /**
  362. *
  363. * @param getType 领取状态 0 未完成,1,未领取,2.已领取
  364. * @param propType 物品id
  365. * @param num 物品数量
  366. * @param node 物品节点
  367. */
  368. showReward(getType: number, propType: number, num: number, node: cc.Node, curTime: number, needTimes: number) {
  369. // if(propType == MONEY_TYPE.carLvUp)
  370. node.stopAllActions()
  371. node.scale = 1
  372. if (propType == MONEY_TYPE.talentCoin) {
  373. cc.loader.loadRes('xiyou/icon/1003', cc.SpriteFrame, (err, assets) => {
  374. if (err) {
  375. cc.error(err);
  376. return;
  377. }
  378. if (cc.isValid(node)) {
  379. node.getChildByName("prop").getComponent(cc.Sprite).spriteFrame = assets;
  380. node.getChildByName("prop").scale = 0.6
  381. }
  382. })
  383. }
  384. node.getChildByName("num").getComponent(cc.Label).string = num + ""
  385. if (getType == 0) {
  386. node.getChildByName("getBg").active = false
  387. node.getChildByName("getIcon").active = false
  388. node.getChildByName("progress").getComponent(cc.Label).string = curTime + "/" + needTimes + "次"
  389. } else if (getType == 1) {
  390. node.getChildByName("getBg").active = true
  391. node.getChildByName("getIcon").active = false
  392. node.getChildByName("progress").getComponent(cc.Label).string = needTimes + "/" + needTimes + "次"
  393. node.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(1, 1.3), cc.scaleTo(1, 1))))
  394. } else if (getType == 2) {
  395. node.getChildByName("getBg").active = true
  396. node.getChildByName("getIcon").active = true
  397. node.getChildByName("progress").getComponent(cc.Label).string = needTimes + "/" + needTimes + "次"
  398. }
  399. }
  400. clickGetReward(event, type) {
  401. let index = Number(type)
  402. let getTimes = GameM.commonData.roleData.turntableExtrasReward
  403. let totalTimes = GameM.commonData.getDailyFinishTimesDataByType(2)
  404. let cfgInfo = TurntableData.Instance.turntableExtrasCfg[index]
  405. if (cfgInfo[0] <= totalTimes) {
  406. if (getTimes.indexOf(index) >= 0) {
  407. EffectNode.instance.PlayTip("已领取");
  408. } else {
  409. let name = ""
  410. if (MONEY_TYPE.talentCoin == cfgInfo[1]) {
  411. MateData.Ins.talentCoin += cfgInfo[2]
  412. GameM.httpM.SendData(HTTP_TYPE.updateBattleStoneCount, { "count": MateData.Ins.talentCoin })
  413. GameM.commonData.roleData.turntableExtrasReward.push(index)
  414. GameM.commonData.updateRoleData()
  415. name = "天赋石 *" + cfgInfo[2] + " "
  416. }
  417. EffectNode.instance.PlayTip(name + "领取成功");
  418. this.updateExtraInfo()
  419. }
  420. } else {
  421. let desc = ""
  422. if (MONEY_TYPE.talentCoin == cfgInfo[1]) {
  423. // MateData.Ins.talentCoin += cfgInfo[2]
  424. // GameM.httpM.SendData(HTTP_TYPE.updateBattleStoneCount, { "count": MateData.Ins.talentCoin })
  425. // GameM.commonData.roleData.turntableExtrasReward.push(index)
  426. // GameM.commonData.updateRoleData()
  427. desc = "即可获得 天赋石 *" + cfgInfo[2]
  428. }
  429. EffectNode.instance.PlayTip("完成" + (cfgInfo[0]) + "次转盘" + desc);
  430. }
  431. }
  432. }