GameMgr.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { _decorator, Component, EventTouch, instantiate, isValid, Node, Prefab, Sprite, UITransform, v3, Vec2, Vec3 } from 'cc'
  2. import { ResMgr } from './ResMgr'
  3. import { Bundle } from '../enum/Bundle'
  4. import { Goods } from '../game/Goods'
  5. import { EventMgr } from './EventMgr'
  6. import { EventType } from '../enum/EventType'
  7. import { ShelveLocker } from '../game/ShelveLocker'
  8. import { ShelveBase } from '../scriptBase/ShelveBase'
  9. import { Mode, ModeName } from '../enum/Mode'
  10. import { StorageUtil } from '../util/StorageUtil'
  11. import { ObjUtil } from '../util/ObjUtil'
  12. import { AudioMgr } from './AudioMgr'
  13. import { PREVIEW } from 'cc/env'
  14. import { GlobalMgr } from './GlobalMgr'
  15. import { AdvertMgr } from './AdvertMgr'
  16. const { ccclass, property } = _decorator
  17. const PickOffset: number = 80
  18. @ccclass('Manager/GameMgr')
  19. export class GameMgr extends Component {
  20. private static _inst: GameMgr = null
  21. private static _node: Node = null
  22. private static _uiTrans: UITransform = null
  23. public static get Stage(): Node {
  24. return this._node.children[0]
  25. }
  26. private static curLevel: object = {}
  27. public static get CurLevel(): number {
  28. if (!ObjUtil.has(this.curLevel, this.mode)) return 0
  29. return this.curLevel[this.mode]
  30. }
  31. public static get CurPassedLevel(): number {
  32. let passCnt: number = 0
  33. for (const mode in this.curLevel) {
  34. if (Object.prototype.hasOwnProperty.call(this.curLevel, mode)) {
  35. const level = this.curLevel[mode]
  36. passCnt += level
  37. }
  38. }
  39. return passCnt
  40. }
  41. public static get isHardLevel(): boolean {
  42. return (this.CurLevel + 1) % 5 === 0
  43. }
  44. private static unlockedMode: Mode[] = []
  45. public static mode: Mode = Mode.tp
  46. private static _pickedGoods: Goods = null
  47. private static _holdGoods: Node = null
  48. public static shelveArr: ShelveBase[] = []
  49. public static shelveLockerArr: ShelveLocker[] = []
  50. public static goodsArr: number[] = []
  51. public static goodsData = {}
  52. private static pause: boolean = false
  53. public static get Pause(): boolean {
  54. return this.pause
  55. }
  56. public static set Pause(v: boolean) {
  57. this.pause = v;
  58. EventMgr.emit(EventType.Pause, v)
  59. }
  60. protected onLoad(): void {
  61. globalThis.GameMgr = GameMgr
  62. GameMgr._inst = this
  63. GameMgr._node = this.node
  64. GameMgr._uiTrans = this.getComponent(UITransform)
  65. this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
  66. this.node.on(Node.EventType.TOUCH_END, this.onTouchFinish, this)
  67. this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchFinish, this)
  68. GameMgr.unlockedMode = StorageUtil.getObj('unlockedMode', [0])
  69. GameMgr.curLevel = StorageUtil.getObj('curLevel', { 0: 0 })
  70. GlobalMgr.instance.initilize();
  71. AdvertMgr.instance.initilize();
  72. AdvertMgr.instance.showInterstitial();
  73. }
  74. public static unlockMode(mode: Mode): void {
  75. if (this.unlockedMode.includes(mode)) return
  76. this.unlockedMode.push(mode)
  77. this.unlockedMode.sort()
  78. StorageUtil.setObj('unlockedMode', this.unlockedMode)
  79. EventMgr.emit(EventType.ModeUnlocked)
  80. }
  81. private static createStage(): void {
  82. const stagePre: Prefab = ResMgr.getRes(Bundle.Game, 'Stage')
  83. const stageNode: Node = instantiate(stagePre)
  84. this._node.addChild(stageNode)
  85. }
  86. public static startGame(): void {
  87. this.reset()
  88. this._node.destroyAllChildren()
  89. this._inst.scheduleOnce(() => {
  90. this.createStage()
  91. })
  92. }
  93. public static passGame(): void {
  94. if (!ObjUtil.has(this.curLevel, this.mode)) {
  95. this.curLevel[this.mode] = 0
  96. }
  97. this.curLevel[this.mode]++
  98. StorageUtil.setObj('curLevel', this.curLevel)
  99. if (this.mode === Mode.tp && this.curLevel[this.mode] >= 15) {
  100. this.unlockMode(Mode.gs)
  101. } else if (this.mode === Mode.gs && this.curLevel[this.mode] >= 10) {
  102. this.unlockMode(Mode.ls)
  103. }
  104. }
  105. public static quitGame(): void {
  106. this._node.destroyAllChildren()
  107. this.reset()
  108. }
  109. public static pick(goods: Goods, uiPos: Vec2): void {
  110. this._pickedGoods = goods
  111. this._holdGoods = new Node()
  112. const sp: Sprite = this._holdGoods.addComponent(Sprite)
  113. sp.spriteFrame = goods.Sp.spriteFrame
  114. this._node.addChild(this._holdGoods)
  115. uiPos.y += PickOffset
  116. const localPos: Vec3 = GameMgr._uiTrans.convertToNodeSpaceAR(v3(uiPos.x, uiPos.y, 0))
  117. this._holdGoods.setPosition(localPos)
  118. AudioMgr.playSfx('点击消除物')
  119. }
  120. public static release(): void {
  121. let isSuccess: boolean = false
  122. const holdGoodsWorldPos: Vec3 = this._holdGoods.getWorldPosition()
  123. for (let i = 0; i < this.shelveArr.length; i++) {
  124. const shelve: ShelveBase = this.shelveArr[i]
  125. if (shelve.getComponentInChildren(ShelveLocker)) continue
  126. const uiTrans: UITransform = shelve.getComponent(UITransform)
  127. const shelveWorldPos: Vec3 = shelve.node.getWorldPosition()
  128. const goodsLocalPos: Vec3 = uiTrans.convertToNodeSpaceAR(holdGoodsWorldPos)
  129. const disX: number = Math.abs(shelveWorldPos.x - holdGoodsWorldPos.x)
  130. const disY: number = Math.abs(shelveWorldPos.y - holdGoodsWorldPos.y)
  131. if (disX < uiTrans.width / 2 && disY < uiTrans.height / 2) {
  132. const slotId: number = shelve.getSlot(goodsLocalPos)
  133. if (slotId < 0 || shelve.isSlotOccupied(slotId)) break
  134. const goodsId: number = this._pickedGoods.Id
  135. shelve.placeGoods(goodsId, slotId)
  136. isSuccess = true
  137. break
  138. }
  139. }
  140. if (isSuccess) {
  141. EventMgr.emit(EventType.PlaceGoods)
  142. } else {
  143. this._pickedGoods.Visible = true
  144. }
  145. this._pickedGoods = null
  146. this._holdGoods.destroy()
  147. this._holdGoods = null
  148. AudioMgr.playSfx('放下消除物')
  149. }
  150. public static get isPicking(): boolean {
  151. return !!this._pickedGoods
  152. }
  153. private onTouchMove(e: EventTouch): void {
  154. const holdGoods: Node = GameMgr._holdGoods
  155. if (!isValid(holdGoods)) return
  156. const uiPos: Vec2 = e.getUILocation()
  157. uiPos.y += PickOffset
  158. const localPos: Vec3 = GameMgr._uiTrans.convertToNodeSpaceAR(v3(uiPos.x, uiPos.y, 0))
  159. holdGoods.setPosition(localPos)
  160. }
  161. private onTouchFinish(e: EventTouch): void {
  162. if (!isValid(GameMgr._holdGoods)) return
  163. GameMgr.release()
  164. }
  165. private static reset(): void {
  166. this._pickedGoods = null
  167. this._holdGoods = null
  168. this.shelveArr = []
  169. this.shelveLockerArr = []
  170. this.goodsArr = []
  171. this.goodsData = {}
  172. }
  173. public static isModeUnlocked(mode: Mode): boolean {
  174. return this.unlockedMode.includes(mode)
  175. }
  176. public static get isModeAllUnlocked(): boolean {
  177. return this.unlockedMode.length >= ModeName.length
  178. }
  179. public static get isStucked(): boolean {
  180. for (let i = 0; i < this.shelveArr.length; i++) {
  181. const shelve: ShelveBase = this.shelveArr[i];
  182. if (!shelve.isAllSlotOccupied()) return false
  183. }
  184. return true
  185. }
  186. }
  187. if (PREVIEW) {
  188. globalThis.GameMgr = GameMgr
  189. }