Daily.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import GameM, { AUDIO_TYPE, VIDEO_TYPE } from "../manager/GameM";
  8. import DailyUpItem from "../prefabs/DailyUpItem";
  9. import DailyDownItem from "../prefabs/DailyDownItem";
  10. import UiM, { PANEL_NAME } from "../manager/UiM";
  11. import Main from "../Main";
  12. import EffectNode from "./EffectNode";
  13. import AdM from "../manager/AdM";
  14. import { thirdVideoTS } from "../manager/topon/AnyThinkAdsMgr/thirdVideoTS";
  15. import DailyData from "../datas/DailyData";
  16. import { PlayerPrefs } from "../tools/MyExtends";
  17. import GuideMng from "../manager/GuideMng";
  18. import { Utils } from "../utils/Utils";
  19. const { ccclass, property } = cc._decorator;
  20. @ccclass
  21. export default class Daily extends cc.Component {
  22. @property(cc.Node)
  23. content: cc.Node = null;
  24. @property(cc.Node)
  25. sview: cc.Node = null;
  26. @property(cc.Node)
  27. dragArea: cc.Node = null
  28. @property(cc.Node)
  29. item1: cc.Node = null
  30. @property(cc.Node)
  31. item2: cc.Node = null
  32. @property(cc.Node)
  33. btnGet: cc.Node = null;
  34. @property(cc.Node)
  35. btnNo: cc.Node = null;
  36. @property(cc.Label)
  37. labDays: cc.Label = null;
  38. @property(cc.Node)
  39. iconGot: cc.Node = null;
  40. @property(cc.RichText)
  41. labCondition: cc.RichText = null;
  42. @property(cc.Node)
  43. btnLeft: cc.Node = null;
  44. @property(cc.Node)
  45. btnRight: cc.Node = null;
  46. @property(cc.Node)
  47. arrLeft: cc.Node = null;
  48. @property(cc.Node)
  49. arrRight: cc.Node = null;
  50. @property(cc.Animation)
  51. getBtnAni: cc.Animation = null
  52. @property(cc.Node)
  53. par: cc.Node = null
  54. @property(cc.Node)
  55. guideRect1: cc.Node = null;
  56. curSelDownItem = null
  57. preDownItem = null
  58. curSelItem = null
  59. curData = null
  60. itemP = null
  61. //分帧加载
  62. addNum = 0
  63. addTotal = 0
  64. _n = 0
  65. _dis = 5
  66. private disX = 0
  67. private lastX = 0
  68. private startX = 0
  69. private coolTime = false
  70. private hasChoose = false
  71. async onLoad() {
  72. this.addNum = 0
  73. this.addTotal = 0
  74. this.itemP = await Utils.loadResPromise('prefabs/DailyUpItem')
  75. // this.content.width = this.itemP.width * DailyData.Instance.dailyCfg.length + 10 * (DailyData.Instance.dailyCfg.length - 1)
  76. this.addTotal = DailyData.Instance.dailyCfg.length
  77. this.dragArea.on(cc.Node.EventType.TOUCH_START, this.onDragStart, this)
  78. this.dragArea.on(cc.Node.EventType.TOUCH_END, this.onDragEnd, this)
  79. }
  80. onEnable() {
  81. this.item1.active = false
  82. this.hasChoose = false
  83. let len = this.content.childrenCount
  84. for (var i = 0; i < len; i++) {
  85. let item: cc.Node = this.content.getChildByName(i.toString())
  86. let s = item.getComponent(DailyUpItem)
  87. if (DailyData.Instance.dailyGetArr[i] == '1') {
  88. s.init(DailyData.Instance.dailyCfg[i], true)
  89. }
  90. else {
  91. s.init(DailyData.Instance.dailyCfg[i], DailyData.Instance.dailyGetArr[this.addNum] == '1')
  92. if (!this.hasChoose) {
  93. let day = DailyData.Instance.dailyCfg[i].getDay
  94. if (day >= GameM.commonData.loginDays) {
  95. if (DailyData.Instance.dailyGetArr[i] == '0') {
  96. this.initSel(i, item, s)
  97. this.hasChoose = true
  98. }
  99. }
  100. }
  101. }
  102. }
  103. AdM.showBanner()
  104. AdM.destroyNative()
  105. this.guideRect1.active = GuideMng.Ins.CurGuideId == 16;
  106. //this.guideRect1.active = GameM.commonData.maxCarLevel < GameM.commonData.loginRewardOpenLevel;
  107. //this.guideRect1.active = PlayerPrefs.GetInt("GGuide9", 0) != 1;
  108. }
  109. onDisable() {
  110. this.curSelDownItem = null
  111. this.preDownItem = null
  112. this.curSelItem = null
  113. this.curData = null
  114. AdM.destroyBanner()
  115. }
  116. onScroll() {
  117. if (Math.abs(this.content.x - this.lastX) >= this.disX) {
  118. this.lastX = this.content.x
  119. this.content.children.forEach(element => {
  120. this.checkVisible(element)
  121. });
  122. }
  123. }
  124. onClickClose() {
  125. this.coolTime = false
  126. GameM.audioM.playEffect(AUDIO_TYPE.button)
  127. // this.node.active = false
  128. this.hideEff()
  129. }
  130. update(dt) {
  131. if (this.addNum < this.addTotal) {
  132. if (this._n <= this._dis) {
  133. this._n++
  134. }
  135. else {
  136. this._n = 0
  137. let item: cc.Node = cc.instantiate(this.itemP)
  138. item.name = this.addNum.toString()
  139. let s = item.getComponent(DailyUpItem)
  140. s.init(DailyData.Instance.dailyCfg[this.addNum], DailyData.Instance.dailyGetArr[this.addNum] == '1')
  141. this.disX = item.width + 10
  142. item.x = item.width * 0.5 + (item.width + 10) * this.addNum
  143. this.content.addChild(item)
  144. this.checkVisible(item)
  145. if (!this.hasChoose) {
  146. let day = DailyData.Instance.dailyCfg[this.addNum].getDay
  147. if (day >= GameM.commonData.loginDays) {
  148. if (DailyData.Instance.dailyGetArr[this.addNum] == '0') {
  149. this.hasChoose = true
  150. this.initSel(this.addNum, item, s)
  151. }
  152. }
  153. }
  154. this.content.width = this.addTotal * (item.width + 10)
  155. this.addNum++
  156. if (this.addNum == this.addTotal && !this.hasChoose) {
  157. this.initSel(this.addNum - 1, item, s)
  158. }
  159. item.on(cc.Node.EventType.TOUCH_END, this.onSel, this)
  160. }
  161. }
  162. this.onScroll()
  163. }
  164. initSel(index, item, s) {
  165. this.coolTime = true
  166. this.curData = DailyData.Instance.dailyCfg[index]
  167. this.curSelItem = item
  168. s.setSel(true)
  169. this.curSelDownItem = this.item1
  170. this.curSelDownItem.getComponent(DailyDownItem).init(this.curData)
  171. this.curSelDownItem.x = 1500
  172. this.curSelDownItem.active = true
  173. this.item2.x = 1500
  174. cc.tween(this.curSelDownItem).to(0.6, { x: 0 }, cc.easeInOut(1)).call(() => {
  175. this.coolTime = false
  176. }).start()
  177. this.freshBtns(DailyData.Instance.dailyGetArr[this.curData.index] == '1')
  178. this.autoScroll()
  179. }
  180. autoScroll() {
  181. let endX = 0
  182. let len = DailyData.Instance.dailyCfg.length
  183. if (this.curData.index == 0 || this.curData.index == 1 || this.curData.index == 2) {
  184. endX = 0
  185. if (this.curData.index == 0) {
  186. this.btnLeft.active = false
  187. this.arrLeft.active = false
  188. }
  189. else {
  190. this.btnLeft.active = true
  191. this.arrLeft.active = true
  192. this.btnRight.active = true
  193. this.arrRight.active = true
  194. }
  195. }
  196. else if (this.curData.index == len - 1 || this.curData.index == len - 2 || this.curData.index == len - 3) {
  197. endX = this.content.width
  198. if (this.curData.index == len - 1) {
  199. this.btnRight.active = false
  200. this.arrRight.active = false
  201. }
  202. else {
  203. this.btnLeft.active = true
  204. this.arrLeft.active = true
  205. this.btnRight.active = true
  206. this.arrRight.active = true
  207. }
  208. }
  209. else {
  210. endX = this.content.width * (this.curData.index + 0.5) / len
  211. this.btnLeft.active = true
  212. this.arrLeft.active = true
  213. this.btnRight.active = true
  214. this.arrRight.active = true
  215. }
  216. this.sview.getComponent(cc.ScrollView).scrollTo(cc.v2(endX / this.content.width, 0), 0.6)
  217. }
  218. checkVisible(item: cc.Node) {
  219. let disX = item.y + this.content.y
  220. if (disX >= 90 + this.disX || disX <= -this.sview.width - 90 - this.disX) {
  221. item.active = false
  222. }
  223. else {
  224. item.active = true
  225. }
  226. }
  227. onSel(evt: cc.Event.EventTouch) {
  228. if (this.coolTime) {
  229. return
  230. }
  231. GameM.audioM.playEffect(AUDIO_TYPE.button)
  232. let index = Number(evt.currentTarget.name)
  233. let data = DailyData.Instance.dailyCfg[index]
  234. this.onSelByData(data)
  235. }
  236. onSelByData(data) {
  237. if (this.curData) {
  238. let preTo = -1500
  239. if (data.index == this.curData.index) {
  240. return
  241. }
  242. else if (data.index > this.curData.index) {
  243. if (this.curSelDownItem == this.item1) {
  244. this.curSelDownItem = this.item2
  245. this.preDownItem = this.item1
  246. }
  247. else {
  248. this.curSelDownItem = this.item1
  249. this.preDownItem = this.item2
  250. }
  251. this.curSelDownItem.getComponent(DailyDownItem).init(data)
  252. this.curSelDownItem.x = 1500
  253. preTo = -1500
  254. }
  255. else {
  256. if (this.curSelDownItem == this.item1) {
  257. this.curSelDownItem = this.item2
  258. this.preDownItem = this.item1
  259. }
  260. else {
  261. this.curSelDownItem = this.item1
  262. this.preDownItem = this.item2
  263. }
  264. this.curSelDownItem.getComponent(DailyDownItem).init(data)
  265. this.curSelDownItem.x = -1500
  266. preTo = 1500
  267. }
  268. this.coolTime = true
  269. cc.tween(this.preDownItem).to(0.6, { x: preTo }, cc.easeInOut(1)).start()
  270. cc.tween(this.curSelDownItem).to(0.6, { x: 0 }, cc.easeInOut(1)).call(() => {
  271. this.coolTime = false
  272. }).start()
  273. }
  274. else {
  275. return
  276. }
  277. if (this.curSelItem) {
  278. this.curSelItem.getComponent(DailyUpItem).setSel(false)
  279. }
  280. this.curData = data
  281. this.curSelItem = this.content.getChildByName(this.curData.index.toString())
  282. let s = this.curSelItem.getComponent(DailyUpItem)
  283. s.setSel(true)
  284. this.freshBtns(DailyData.Instance.dailyGetArr[this.curData.index] == '1')
  285. this.autoScroll()
  286. }
  287. onLeft() {
  288. GameM.audioM.playEffect(AUDIO_TYPE.button)
  289. this.onSelDown(2)
  290. }
  291. onRight() {
  292. GameM.audioM.playEffect(AUDIO_TYPE.button)
  293. this.onSelDown(1)
  294. }
  295. /**
  296. * 滑动选择
  297. * @param state 1 向左 2 向右
  298. */
  299. onSelDown(state, slide = false) {
  300. if (this.coolTime) {
  301. return
  302. }
  303. if (!this.curData) {
  304. return
  305. }
  306. let preTo = -1500
  307. let data
  308. if (state == 1) {
  309. if (this.curData.index >= DailyData.Instance.dailyCfg.length - 1) {
  310. if (slide) {
  311. EffectNode.instance.PlayTip('已到最后一项')
  312. }
  313. return
  314. }
  315. data = DailyData.Instance.dailyCfg[this.curData.index + 1]
  316. if (this.curSelDownItem == this.item1) {
  317. this.curSelDownItem = this.item2
  318. this.preDownItem = this.item1
  319. }
  320. else {
  321. this.curSelDownItem = this.item1
  322. this.preDownItem = this.item2
  323. }
  324. this.curSelDownItem.getComponent(DailyDownItem).init(data)
  325. this.curSelDownItem.x = 1500
  326. preTo = -1500
  327. }
  328. else if (state == 2) {
  329. if (this.curData.index <= 0) {
  330. if (slide) {
  331. EffectNode.instance.PlayTip('已到第一项')
  332. }
  333. return
  334. }
  335. data = DailyData.Instance.dailyCfg[this.curData.index - 1]
  336. if (this.curSelDownItem == this.item1) {
  337. this.curSelDownItem = this.item2
  338. this.preDownItem = this.item1
  339. }
  340. else {
  341. this.curSelDownItem = this.item1
  342. this.preDownItem = this.item2
  343. }
  344. this.curSelDownItem.getComponent(DailyDownItem).init(data)
  345. this.curSelDownItem.x = -1500
  346. preTo = 1500
  347. }
  348. this.coolTime = true
  349. cc.tween(this.preDownItem).to(0.6, { x: preTo }, cc.easeInOut(1)).start()
  350. cc.tween(this.curSelDownItem).to(0.6, { x: 0 }, cc.easeInOut(1)).call(() => {
  351. this.coolTime = false
  352. }).start()
  353. if (this.curSelItem) {
  354. this.curSelItem.getComponent(DailyUpItem).setSel(false)
  355. }
  356. this.curData = data
  357. this.curSelItem = this.content.getChildByName(this.curData.index.toString())
  358. let s = this.curSelItem.getComponent(DailyUpItem)
  359. s.setSel(true)
  360. this.freshBtns(DailyData.Instance.dailyGetArr[this.curData.index] == '1')
  361. this.autoScroll()
  362. }
  363. onDragStart(evt) {
  364. this.startX = evt.touch._point.x
  365. }
  366. onDragEnd(evt) {
  367. let end = evt.touch._point.x - this.startX
  368. if (Math.abs(end) > 20) {
  369. if (end > 0) {
  370. this.onSelDown(2, true)
  371. }
  372. else {
  373. this.onSelDown(1, true)
  374. }
  375. }
  376. }
  377. freshBtns(hasGet) {
  378. if (hasGet) {
  379. this.iconGot.active = true
  380. this.btnGet.active = false
  381. this.btnNo.active = false
  382. this.labCondition.node.active = false
  383. }
  384. else {
  385. this.iconGot.active = false
  386. let canGet = false
  387. let conditionShow = true
  388. switch (this.curData.conditionType) {
  389. case 1:
  390. this.labCondition.string = `领取条件:累计登陆${this.curData.conditionLv}天<color=#FCFF04>(${GameM.commonData.loginDays}/${this.curData.conditionLv})</c>`
  391. canGet = GameM.commonData.loginDays >= this.curData.getDay
  392. conditionShow = !canGet
  393. break;
  394. case 2:
  395. this.labCondition.string = `领取条件:武将等级达到${this.curData.conditionLv}级<color=#FCFF04>(${GameM.commonData.maxCarLevel}/${this.curData.conditionLv})</c>`
  396. canGet = GameM.commonData.loginDays >= this.curData.getDay
  397. conditionShow = GameM.commonData.maxCarLevel < this.curData.conditionLv
  398. break;
  399. case 3:
  400. this.labCondition.string = `领取条件:玩家等级达到${this.curData.conditionLv}级<color=#FCFF04>(${GameM.commonData.roleData.lv}/${this.curData.conditionLv})</c>`
  401. canGet = GameM.commonData.loginDays >= this.curData.getDay
  402. conditionShow = GameM.commonData.roleData.lv < this.curData.conditionLv
  403. break;
  404. }
  405. this.btnGet.active = canGet
  406. this.btnNo.active = !canGet
  407. if (this.curData.getDay - GameM.commonData.loginDays == 1) {
  408. this.labDays.string = `明天领取`
  409. }
  410. else {
  411. this.labDays.string = `累计登陆${this.curData.getDay}天`
  412. }
  413. this.labCondition.node.active = true
  414. if (canGet) {
  415. if (!conditionShow) {
  416. this.labCondition.node.active = false
  417. this.getBtnAni.play()
  418. }
  419. else {
  420. this.getBtnAni.stop()
  421. this.getBtnAni.setCurrentTime(0)
  422. }
  423. }
  424. else {
  425. // this.labCondition.string = `领取条件:累计登陆${this.curData.getDay}天<color=#FCFF04>(${GameM.commonData.loginDays}/${this.curData.getDay})</c>`
  426. this.labCondition.string = `当前累计登陆:<color=#FCFF04>${GameM.commonData.loginDays}天</c>`
  427. this.getBtnAni.stop()
  428. this.getBtnAni.setCurrentTime(0)
  429. }
  430. }
  431. }
  432. clickGet() {
  433. GameM.audioM.playEffect(AUDIO_TYPE.button)
  434. if (DailyData.Instance.dailyGetArr[this.curData.index] == '1') {
  435. return
  436. }
  437. if (this.coolTime) {
  438. return
  439. }
  440. this.coolTime = true
  441. if (this.curData.conditionType == 1) {
  442. if (GameM.commonData.loginDays < this.curData.conditionLv) {
  443. cc.tween(this.labCondition.node).to(0.15, { scale: 1.3 }).to(0.15, { scale: 1 }).start()
  444. this.coolTime = false
  445. return
  446. }
  447. }
  448. else if (this.curData.conditionType == 2) {
  449. if (GameM.commonData.maxCarLevel < this.curData.conditionLv) {
  450. cc.tween(this.labCondition.node).to(0.15, { scale: 1.3 }).to(0.15, { scale: 1 }).start()
  451. this.coolTime = false
  452. return
  453. }
  454. }
  455. else if (this.curData.conditionType == 3) {
  456. if (GameM.commonData.roleData.lv < this.curData.conditionLv) {
  457. cc.tween(this.labCondition.node).to(0.15, { scale: 1.3 }).to(0.15, { scale: 1 }).start()
  458. this.coolTime = false
  459. return
  460. }
  461. }
  462. if (this.curData.rewardType == 2 && UiM.Instance.hallNode.getComponent(Main).getEmptySpace() == '') {
  463. EffectNode.instance.PlayTip("位置已满,请空出位置再领");
  464. this.coolTime = false
  465. return
  466. }
  467. if (this.curData.rewardType == 1) {
  468. UiM.Instance.redPackageNode.init(this.curData.rewardNum, 0, 1, null)
  469. UiM.Instance.redPackageNode.node.active = true
  470. UiM.Instance.redPackageNode.node.x = 1000
  471. }
  472. // GameM.adM.watchVideo(VIDEO_TYPE.dailyGet)
  473. this.onVideoEnd()
  474. }
  475. clickShowTip() {
  476. GameM.audioM.playEffect(AUDIO_TYPE.button)
  477. cc.tween(this.labCondition.node).to(0.15, { scale: 1.3 }).to(0.15, { scale: 1 }).start()
  478. }
  479. onVideoEnd() {
  480. switch (this.curData.rewardType) {
  481. case 1:
  482. UiM.Instance.redPackageNode.init(this.curData.rewardNum, 0, 1, null)
  483. UiM.Instance.openPanel(UiM.Instance.redPackageNode.node, false)
  484. break
  485. case 2:
  486. UiM.Instance.hallNode.getComponent(Main).getDailyCar(this.curData.rewardNum)
  487. break
  488. }
  489. }
  490. onVideoFail() {
  491. this.coolTime = false
  492. if (this.curData.rewardType == 1) {
  493. UiM.Instance.redPackageNode.node.active = false
  494. UiM.Instance.redPackageNode.node.x = 0
  495. }
  496. }
  497. hasOpen() {
  498. DailyData.Instance.updateDailyGet(this.curData.index)
  499. this.curSelItem.getComponent(DailyUpItem).init(DailyData.Instance.dailyCfg[this.curData.index], true)
  500. EffectNode.instance.PlayTip("领取成功");
  501. this.iconGot.active = false
  502. this.btnGet.active = false
  503. this.btnNo.active = false
  504. this.labCondition.node.active = false
  505. cc.tween(this.iconGot)
  506. .delay(0.5)
  507. .call(() => {
  508. this.iconGot.active = true
  509. this.iconGot.scale = 5
  510. })
  511. .to(0.15, { scale: 1 })
  512. .call(() => {
  513. GameM.audioM.playEffect(AUDIO_TYPE.gaizhang)
  514. this.node.y -= 10
  515. })
  516. .delay(0.1)
  517. .call(() => {
  518. this.node.y += 10
  519. })
  520. .delay(0.7)
  521. .call(() => {
  522. if (this.curData.index != DailyData.Instance.dailyCfg.length - 1) {
  523. for (var i = this.curData.index + 1; i < DailyData.Instance.dailyCfg.length; i++) {
  524. if (DailyData.Instance.dailyGetArr[i] != '1') {
  525. this.onSelByData(DailyData.Instance.dailyCfg[i])
  526. break
  527. }
  528. }
  529. }
  530. }).start()
  531. UiM.Instance.hallNode.getComponent(Main).freshDailyRed()
  532. }
  533. hideEff() {
  534. let main = UiM.Instance.hallNode.getComponent(Main)
  535. let pos1 = main.dailyBtn.node.position
  536. let pos2 = main.topPart.position
  537. let pos = cc.v2(pos1.x, pos1.y + pos2.y)
  538. cc.tween(this.par).to(0.2, { scale: 0, position: pos }).call(() => {
  539. UiM.Instance.offPanel(PANEL_NAME.DailyNode, false, () => {
  540. AdM.createInter(13);
  541. })
  542. // UiM.Instance.hallNode.getComponent(Main).checkOfflineAward()
  543. // this.par.scale = 1
  544. // this.par.setPosition(cc.v2(0, 0))
  545. }).start();
  546. }
  547. //西游
  548. Click_GuideRectBtn() {
  549. this.guideRect1.active = false;
  550. }
  551. }