Stage.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { _decorator, Component, Input, input, instantiate, KeyCode, macro, math, Node, Prefab, Sprite, UITransform, v3, Vec3 } from 'cc';
  2. import { GameMgr } from '../manager/GameMgr';
  3. import { ArrayUtil } from '../util/ArrayUtil';
  4. import { ShelveType } from '../enum/ShelveType';
  5. import { ShelveLocker } from './ShelveLocker';
  6. import { ShelveBase } from '../scriptBase/ShelveBase';
  7. import { CommonShelve } from './CommonShelve';
  8. import { SmallShelve } from './SmallShelve';
  9. import { UIMgr } from '../manager/UIMgr';
  10. import { UI } from '../enum/UI';
  11. import { PREVIEW } from 'cc/env';
  12. import { Mode, ModeName } from '../enum/Mode';
  13. import { cfg_level_gs, cfg_level_ls, cfg_level_tp } from '../config/cfg_level';
  14. import { DtoLevelCfg } from '../dto/DtoLevelCfg';
  15. import { DtoLevelData } from '../dto/DtoLevelData';
  16. import { cfg_icon_gs, cfg_icon_ls, cfg_icon_tp } from '../config/cfg_icon';
  17. import { DtoIconCfg } from '../dto/DtoIconCfg';
  18. import { ResMgr } from '../manager/ResMgr';
  19. import { Bundle } from '../enum/Bundle';
  20. import { Global } from '../Global';
  21. import { EventMgr } from '../manager/EventMgr';
  22. import { EventType } from '../enum/EventType';
  23. import { CoinMgr } from '../manager/CoinMgr';
  24. import { ComboBox } from './ComboBox';
  25. import { Goods } from './Goods';
  26. import { AudioMgr } from '../manager/AudioMgr';
  27. const { ccclass, property } = _decorator;
  28. @ccclass('Game/Stage')
  29. export class Stage extends Component {
  30. @property(Sprite)
  31. private bg: Sprite = null
  32. @property(Node)
  33. private shelveBox: Node = null
  34. @property(ComboBox)
  35. private comboBox: ComboBox = null
  36. @property(Node)
  37. private tip: Node = null
  38. @property(Prefab)
  39. private shelveLockerPre: Prefab = null
  40. @property(Prefab)
  41. private shelvePreArr: Prefab[] = []
  42. private levelCfg: DtoLevelCfg = null
  43. private levelData: DtoLevelData = null
  44. private iconCfg: DtoIconCfg = null
  45. private shelveArr: ShelveBase[] = []
  46. private doubleCoin: boolean = false
  47. private isBusy: boolean = false
  48. protected onLoad(): void {
  49. const bgName: string = 'bg_' + ModeName[GameMgr.mode]
  50. this.bg.spriteFrame = ResMgr.getSpriteFrame(Bundle.Game, bgName, 'image/')
  51. EventMgr.on(EventType.MergeGoods, this.onMergeGoods, this)
  52. EventMgr.on(EventType.TimeOut, this.onTimeOut, this)
  53. EventMgr.on(EventType.TimeOutAlert, this.onTimeOutAlert, this)
  54. EventMgr.on(EventType.UseSkillFreezeTime, this.onUseSkillFreezeTime, this)
  55. EventMgr.on(EventType.UseSkillDoubleCoin, this.onUseDoubleCoin, this)
  56. EventMgr.on(EventType.UseSkillRefreshPosition, this.onUseRefreshPos, this)
  57. EventMgr.on(EventType.UseSkillEraseGroup, this.onUseEraseGroup, this)
  58. if (PREVIEW) {
  59. input.on(Input.EventType.KEY_DOWN, (e) => {
  60. if (e.keyCode !== KeyCode.KEY_Q) return
  61. if (GameMgr.Pause) return
  62. UIMgr.open(UI.Win)
  63. }, this)
  64. }
  65. }
  66. protected onDestroy(): void {
  67. UIMgr.close(UI.Game)
  68. this.unscheduleAllCallbacks()
  69. EventMgr.off(EventType.MergeGoods, this.onMergeGoods, this)
  70. EventMgr.off(EventType.TimeOut, this.onTimeOut, this)
  71. EventMgr.off(EventType.TimeOutAlert, this.onTimeOutAlert, this)
  72. EventMgr.off(EventType.UseSkillFreezeTime, this.onUseSkillFreezeTime, this)
  73. EventMgr.off(EventType.UseSkillDoubleCoin, this.onUseDoubleCoin, this)
  74. EventMgr.off(EventType.UseSkillRefreshPosition, this.onUseRefreshPos, this)
  75. EventMgr.off(EventType.UseSkillEraseGroup, this.onUseEraseGroup, this)
  76. }
  77. protected start() {
  78. UIMgr.open(UI.Game)
  79. this.tip.active = GameMgr.mode === Mode.tp && GameMgr.CurLevel === 0
  80. this.scheduleOnce(() => {
  81. if (GameMgr.isHardLevel) UIMgr.open(UI.StageTip, '难度飙升')
  82. }, 1)
  83. switch (GameMgr.mode) {
  84. case Mode.tp:
  85. this.levelCfg = cfg_level_tp
  86. this.iconCfg = cfg_icon_tp
  87. break;
  88. case Mode.gs:
  89. this.levelCfg = cfg_level_gs
  90. this.iconCfg = cfg_icon_gs
  91. break
  92. case Mode.ls:
  93. this.levelCfg = cfg_level_ls
  94. this.iconCfg = cfg_icon_ls
  95. break
  96. }
  97. if (GameMgr.CurLevel >= this.levelCfg.length) {
  98. this.levelData = ArrayUtil.pickItem(this.levelCfg)
  99. } else {
  100. this.levelData = this.levelCfg[GameMgr.CurLevel]
  101. }
  102. const shelveMap: number[][] = this.levelData.shelveMap
  103. const group: number = this.levelData.group
  104. const lockCnt: number = this.levelData.lockCnt
  105. const goodsTypeCnt: number = this.levelData.GoodsTypeCnt
  106. Global.Level_Time = this.levelData.time
  107. // Global.Level_Time = 10
  108. EventMgr.emit(EventType.TimeReset)
  109. this.createShelves(shelveMap)
  110. this.createShelveLockers(lockCnt)
  111. this.createGoodsArr(group, goodsTypeCnt)
  112. this.createGoodsData()
  113. this.schedule(this.onCheckWin, 0.5, macro.REPEAT_FOREVER)
  114. }
  115. private createShelves(shelveMap: number[][]): void {
  116. const row: number = shelveMap.length
  117. const col: number = shelveMap[0].length
  118. const uiTrans: UITransform = this.shelveBox.getComponent(UITransform)
  119. const spaceX: number = 0
  120. const spaceY: number = 0
  121. uiTrans.width = col * 230 + spaceX * (col - 1)
  122. uiTrans.height = row * 145 + spaceY * (row - 1)
  123. for (let i = 0; i < shelveMap.length; i++) {
  124. for (let j = 0; j < shelveMap[i].length; j++) {
  125. const type: ShelveType = shelveMap[i][j]
  126. const shelvePre: Prefab = this.shelvePreArr[type]
  127. const shelveNode: Node = instantiate(shelvePre)
  128. this.shelveBox.addChild(shelveNode)
  129. }
  130. }
  131. this.shelveArr = this.shelveBox.getComponentsInChildren(ShelveBase)
  132. }
  133. private createShelveLockers(lockCnt: number): void {
  134. if (lockCnt <= 0) return
  135. const shelves: ShelveBase[] = ArrayUtil.pickItems(this.shelveArr, lockCnt, true)
  136. for (let i = 0; i < shelves.length; i++) {
  137. const shelve: ShelveBase = shelves[i];
  138. const shelveLockerNode: Node = instantiate(this.shelveLockerPre)
  139. shelve.node.addChild(shelveLockerNode)
  140. const shelveLocker: ShelveLocker = shelveLockerNode.getComponent(ShelveLocker)
  141. shelveLocker.UnlockCnt = math.randomRangeInt(3, 6)
  142. }
  143. }
  144. private createGoodsArr(group: number, goodsTypeCnt: number): void {
  145. const idxArr: number[] = ArrayUtil.pickIndexs(this.iconCfg, goodsTypeCnt, true)
  146. GameMgr.goodsArr = []
  147. for (let i = 0; i < group; i++) {
  148. const goodsId: number = ArrayUtil.pickItem(idxArr) + 1
  149. GameMgr.goodsArr.push(goodsId, goodsId, goodsId)
  150. }
  151. ArrayUtil.shuffle(GameMgr.goodsArr)
  152. }
  153. private createGoodsData(): void {
  154. const commonShelveArr: CommonShelve[] = []
  155. const smallShelveArr: SmallShelve[] = []
  156. for (let i = 0; i < this.shelveArr.length; i++) {
  157. const shelve: ShelveBase = this.shelveArr[i];
  158. if (shelve instanceof CommonShelve) {
  159. commonShelveArr.push(shelve)
  160. } else if (shelve instanceof SmallShelve) {
  161. smallShelveArr.push(shelve)
  162. }
  163. }
  164. //先悬浮格子
  165. for (let i = 0; i < smallShelveArr.length; i++) {
  166. const smallShelve: SmallShelve = smallShelveArr[i];
  167. const smallShelveId: number = smallShelve.Id
  168. if (!GameMgr.goodsData.hasOwnProperty(smallShelveId)) {
  169. GameMgr.goodsData[smallShelveId] = {
  170. 0: [],
  171. }
  172. }
  173. // const cnt: number = math.randomRangeInt(2, 4)
  174. for (let j = 0; j < 3; j++) {
  175. const goodsId: number = GameMgr.goodsArr.shift()
  176. GameMgr.goodsData[smallShelveId][0].push(goodsId)
  177. }
  178. }
  179. const placeCnt: number = commonShelveArr.length * 3
  180. while (GameMgr.goodsArr.length > 0) {
  181. //筛选槽位
  182. const placeArr: number[] = []
  183. for (let i = 0; i < placeCnt; i++) {
  184. placeArr.push(i)
  185. }
  186. ArrayUtil.shuffle(placeArr)
  187. if (placeCnt <= 6) {
  188. placeArr.splice(0, math.randomRangeInt(2, 3))
  189. } else {
  190. placeArr.splice(0, math.randomRangeInt(4, 6))
  191. }
  192. for (let i = 0; i < placeCnt; i++) {
  193. const placeId: number = i
  194. const commonShelveId: number = commonShelveArr[Math.floor(placeId / 3)].Id
  195. const slotId: number = placeId % 3
  196. if (!GameMgr.goodsData.hasOwnProperty(commonShelveId)) {
  197. GameMgr.goodsData[commonShelveId] = {
  198. 0: [],
  199. 1: [],
  200. 2: [],
  201. }
  202. }
  203. let goodsId: number = -1
  204. if (GameMgr.goodsArr.length <= 0 || !placeArr.includes(placeId)) goodsId = -1
  205. else goodsId = GameMgr.goodsArr.shift()
  206. GameMgr.goodsData[commonShelveId][slotId].push(goodsId)
  207. }
  208. }
  209. }
  210. private onCheckWin(): void {
  211. const isWin: boolean = this.checkWin()
  212. if (!isWin) return
  213. if (this.isBusy) return
  214. this.unscheduleAllCallbacks()
  215. UIMgr.open(UI.Win)
  216. }
  217. private checkWin(): boolean {
  218. for (let i = 0; i < this.shelveArr.length; i++) {
  219. const shelve: ShelveBase = this.shelveArr[i];
  220. if (!shelve.isEmpty) return false
  221. }
  222. return true
  223. }
  224. private onTimeOut(): void {
  225. const isWin: boolean = this.checkWin()
  226. if (isWin) return
  227. UIMgr.open(UI.TimeOut)
  228. }
  229. private onTimeOutAlert(): void {
  230. const effectRedAlertPre: Prefab = ResMgr.getRes(Bundle.Game, 'EffectRedAlert')
  231. const effectRedAlertNode: Node = instantiate(effectRedAlertPre)
  232. this.node.addChild(effectRedAlertNode)
  233. }
  234. private onUseSkillFreezeTime(): void {
  235. const effectFreezePre: Prefab = ResMgr.getRes(Bundle.Game, 'EffectFreeze')
  236. const effectFreezeNode: Node = instantiate(effectFreezePre)
  237. this.node.addChild(effectFreezeNode)
  238. }
  239. private onMergeGoods(shelveWorldPos: Vec3): void {
  240. this.comboBox.CurCombo++
  241. AudioMgr.playSfx(`bomb_${this.comboBox.CurCombo}`)
  242. CoinMgr.CurCoin += this.doubleCoin ? this.comboBox.CurCombo * 2 : this.comboBox.CurCombo
  243. this.comboBox.createFloatText(shelveWorldPos)
  244. }
  245. private onUseDoubleCoin(): void {
  246. AudioMgr.playSfx('星星道具')
  247. this.doubleCoin = true
  248. this.scheduleOnce(this.onAfterDoubleCoin, Global.Double_Coin_Duration)
  249. }
  250. private onAfterDoubleCoin(): void {
  251. this.doubleCoin = false
  252. }
  253. private onUseRefreshPos(): void {
  254. AudioMgr.playSfx('转换道具')
  255. for (let i = 0; i < this.shelveArr.length; i++) {
  256. const shelve: ShelveBase = this.shelveArr[i]
  257. const goodsIdArr: number[] = shelve.GoodsIdArr
  258. for (let j = 0; j < goodsIdArr.length; j++) {
  259. const goodsId: number = goodsIdArr[j];
  260. GameMgr.goodsArr.push(goodsId)
  261. }
  262. }
  263. ArrayUtil.shuffle(GameMgr.goodsArr)
  264. this.createGoodsData()
  265. for (let i = 0; i < this.shelveArr.length; i++) {
  266. const shelve: ShelveBase = this.shelveArr[i]
  267. shelve.clear()
  268. this.scheduleOnce(() => {
  269. shelve.init()
  270. })
  271. }
  272. this.isBusy = true
  273. this.scheduleOnce(() => {
  274. this.isBusy = false
  275. }, 0.5)
  276. }
  277. private onUseEraseGroup(): void {
  278. AudioMgr.playSfx('灯泡道具')
  279. //先统计场上的物品
  280. const goodsMap = {}
  281. for (let i = 0; i < this.shelveArr.length; i++) {
  282. const shelve: ShelveBase = this.shelveArr[i];
  283. const info: Object = shelve.GoodsInfo
  284. for (const layer in info) {
  285. if (Object.prototype.hasOwnProperty.call(info, layer)) {
  286. for (const slot in info[layer]) {
  287. if (Object.prototype.hasOwnProperty.call(info[layer], slot)) {
  288. const goods: Goods = info[layer][slot];
  289. const goodsId: number = goods.Id
  290. if (!goodsMap.hasOwnProperty(goodsId)) {
  291. goodsMap[goodsId] = []
  292. }
  293. goodsMap[goodsId].push(goods)
  294. }
  295. }
  296. }
  297. }
  298. for (const goodsId in goodsMap) {
  299. if (Object.prototype.hasOwnProperty.call(goodsMap, goodsId)) {
  300. ArrayUtil.shuffle(goodsMap[goodsId])
  301. }
  302. }
  303. }
  304. //筛选出超过三个的物品
  305. const map2 = {}
  306. for (const goodsId in goodsMap) {
  307. if (Object.prototype.hasOwnProperty.call(goodsMap, goodsId)) {
  308. if (goodsMap[goodsId].length >= 3) {
  309. map2[goodsId] = goodsMap[goodsId]
  310. }
  311. }
  312. }
  313. //从中挑选三组
  314. const map3 = {}
  315. let cnt: number = 0
  316. for (const goodsId in map2) {
  317. if (Object.prototype.hasOwnProperty.call(map2, goodsId)) {
  318. map3[goodsId] = map2[goodsId]
  319. cnt++
  320. if (cnt >= 3) break
  321. }
  322. }
  323. //将这三组中随机三个物品消除
  324. for (const goodsId in map3) {
  325. if (Object.prototype.hasOwnProperty.call(map3, goodsId)) {
  326. const goodsArr: Goods[] = map3[goodsId];
  327. const pickedGoodsArr: Goods[] = ArrayUtil.pickItems(goodsArr, 3, true)
  328. for (let i = 0; i < pickedGoodsArr.length; i++) {
  329. const goods: Goods = pickedGoodsArr[i];
  330. const shelve: ShelveBase = goods.node.parent.parent.getComponent(ShelveBase)
  331. if (!shelve) {
  332. continue
  333. }
  334. shelve.removeGoods(goods)
  335. }
  336. }
  337. }
  338. }
  339. }