GameLoadingUI.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /** @format */
  2. import {UI} from '../enums/UI'
  3. import {BaseUI} from './BaseUI'
  4. import {Data, Mgr} from '../GameControl'
  5. import {ccUtils} from '../utils/ccUtils'
  6. import {observer, render, node, label, editBox, list} from '../mobx/observer'
  7. import {msgCmd} from '../proto/msg_cmd'
  8. import {ConstValue} from '../data/ConstValue'
  9. const {ccclass, property} = cc._decorator
  10. @ccclass
  11. @observer
  12. export class GameLoadingUI extends BaseUI {
  13. @node('left')
  14. left: cc.Node
  15. @node('right')
  16. right: cc.Node
  17. @node('pb')
  18. pb: cc.Node
  19. pbCurNum: number = 0
  20. pbAllNum: number = 0
  21. onShow(args, fromUI: number) {
  22. this.closeDoor()
  23. this.scheduleOnce(() => {
  24. this.pb.active = true
  25. this.pbCurNum = 0
  26. this.pbAllNum = 1 + (Data.game.gameAssetMap.size > 0 ? 0 : 1)
  27. ccUtils.setProgress(0, this.pb)
  28. console.log(Date.now())
  29. if (Data.game.gameAssetMap.size > 0) {
  30. this.openDoor()
  31. } else {
  32. this.loadAllRes().then(res => {
  33. console.log(Date.now())
  34. this.openDoor()
  35. })
  36. }
  37. }, 0.6)
  38. }
  39. onHide(): any {
  40. this.pb.active = false
  41. this.unscheduleAllCallbacks()
  42. }
  43. update(dt) {
  44. if (this.pb.active && this.pbCurNum < this.pbAllNum) {
  45. this.pbCurNum += dt
  46. ccUtils.setProgress(this.pbCurNum / this.pbAllNum, this.pb)
  47. }
  48. }
  49. closeDoor() {
  50. //关门动画
  51. this.left.stopAllActions()
  52. this.right.stopAllActions()
  53. this.left.x = -ConstValue.CANVAS_WIDTH / 2
  54. this.right.x = ConstValue.CANVAS_WIDTH / 2
  55. let t = cc.tween
  56. t(this.left)
  57. .then(t().to(0.5, {x: 0}))
  58. .start()
  59. t(this.right)
  60. .then(t().to(0.5, {x: 0}))
  61. .start()
  62. }
  63. openDoor() {
  64. Mgr.ui.closeAll([UI.GameLoadingUI])
  65. Mgr.ui.show(UI.ZumaUI)
  66. //开门动画
  67. let t = cc.tween
  68. t(this.left)
  69. .then(
  70. t()
  71. .delay(1)
  72. .call(() => {
  73. this.pb.active = false
  74. })
  75. .to(0.5, {x: -ConstValue.CANVAS_WIDTH / 2})
  76. .call(() => {
  77. this.hide()
  78. })
  79. .union(),
  80. )
  81. .start()
  82. t(this.right)
  83. .then(
  84. t()
  85. .delay(1)
  86. .to(0.5, {x: ConstValue.CANVAS_WIDTH / 2})
  87. .union(),
  88. )
  89. .start()
  90. }
  91. loadAllRes() {
  92. let records = Data.game.gameBundle.getDirWithPath('')
  93. let tempMap = new Map()
  94. for (let i = 0; i < records.length; i++) {
  95. let record = records[i]
  96. let type
  97. let path = record.path
  98. if (path.includes('prefab')) {
  99. type = cc.Prefab
  100. } else if (path.includes('animation')) {
  101. type = cc.AnimationClip
  102. } else if (path.includes('atlas')) {
  103. type = cc.SpriteAtlas
  104. } else if (path.includes('texture')) {
  105. type = cc.SpriteFrame
  106. }
  107. if (type == record.ctor) tempMap.set(record.path, record)
  108. }
  109. let loadPromises = []
  110. tempMap.forEach((record, path) => {
  111. loadPromises.push(this.loadGameRes(path, record.ctor))
  112. })
  113. return Promise.all(loadPromises)
  114. }
  115. loadGameRes(url: string, type: typeof cc.Asset) {
  116. return new Promise((resolve, reject) => {
  117. Data.game.gameBundle.load(url, type, (error, assets: cc.Asset) => {
  118. if (error) {
  119. reject(error)
  120. } else {
  121. Data.game.gameAssetMap.set(url, assets)
  122. resolve(assets)
  123. }
  124. })
  125. })
  126. }
  127. //UI或者其他函数=======================================
  128. //网络事件=======================================
  129. // onXXXRsp() {}
  130. //触发事件=======================================
  131. // @render
  132. // showRender() {}
  133. // 点击事件=======================================
  134. // onClick() {}
  135. }