GameView.ts 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. import LayerPanel, {UrlInfo} from "../../Common/manage/Layer/LayerPanel";
  2. import Tools from "../../Common/Tools";
  3. import EndView from "./EndView";
  4. import Text from "./logic/common/text";
  5. import gameConfig from "./logic/common/config";
  6. import HomeView from "./HomeView";
  7. import CacheMgr from "../../Common/manage/CacheMgr";
  8. import AudioMgr from "../../Common/manage/AudioMgr";
  9. import LoadMgr from "../../Common/manage/LoadMgr";
  10. import Constant from "../../Common/Constant";
  11. import PanelMgr, {Layer} from "../../Common/manage/PanelMgr";
  12. import tween = cc.tween;
  13. import ShowConfig from "../../Common/ShowConfig";
  14. import Global from "../../Common/Global";
  15. import QgBanner from "../../Common/manage/Api/QgBanner";
  16. import Emit from "../../Common/manage/Emit/Emit";
  17. import EmitData from "../../Common/manage/Emit/EmitData";
  18. const {ccclass, property} = cc._decorator;
  19. @ccclass
  20. export default class GameView extends LayerPanel {
  21. public static getUrl(): UrlInfo {
  22. return {
  23. bundle: "gameView",
  24. name: "gameView"
  25. }
  26. }
  27. private _paramData: any = {};
  28. private _button: cc.Node = null;
  29. //test
  30. private testMakeBottomBlock: cc.Node = null
  31. private testReadyMakeBottomBlock: cc.Node = null
  32. //logic
  33. @property(cc.SpriteFrame)
  34. public graySpriteFrame: cc.SpriteFrame = null
  35. @property(cc.SpriteFrame)
  36. public whiteSpriteFrame: cc.SpriteFrame = null
  37. @property(cc.SpriteFrame)
  38. public hintBlockSpriteFrame: cc.SpriteFrame = null
  39. @property(cc.Prefab)
  40. public text_prefab: cc.Prefab = null
  41. @property(cc.Prefab)
  42. public hammer_prefab: cc.Prefab = null
  43. @property(cc.Prefab)
  44. public sprite_prefab: cc.Prefab = null
  45. @property([cc.SpriteFrame])
  46. public sprite_spriteFrame: cc.SpriteFrame[] = []
  47. @property(cc.SpriteFrame)
  48. public hint_1_spriteFrame: cc.SpriteFrame = null
  49. @property(cc.SpriteFrame)
  50. public hint_2_spriteFrame: cc.SpriteFrame = null
  51. @property(cc.Prefab)
  52. public start_prefab: cc.Prefab = null
  53. @property(cc.Prefab)
  54. public hardUp_prefab: cc.Prefab = null
  55. @property(cc.Prefab)
  56. public nice_prefab: cc.Prefab = null
  57. @property(cc.Prefab)
  58. public get_prefab: cc.Prefab = null
  59. private _startPoint: cc.Node = null
  60. private _content: cc.Node = null
  61. private _hintUI: cc.Node = null
  62. private _whiteHint: cc.Node = null
  63. private _textHint: cc.Node = null
  64. private _hardLevelLabel: cc.Label = null
  65. private _scoreLabel: cc.Label = null
  66. private _mouth: cc.Node = null
  67. private _mask: cc.Node = null
  68. private _hammer: cc.Node = null
  69. private _sprite: cc.Node = null
  70. private _menu: cc.Node = null
  71. private _menuPanel: cc.Node = null
  72. private _hint_hammer: cc.Node = null
  73. private _hint_sprite: cc.Node = null
  74. private _hint_mask: cc.Node = null
  75. private _hint_label: cc.Node = null
  76. private _hint_hand: cc.Node = null
  77. private _price_sprite: cc.Node = null
  78. private _sprite_icon: cc.Node = null
  79. private _hammer_sprite: cc.Node = null
  80. private _hammer_icon: cc.Node = null
  81. private _content_cover: cc.Node = null
  82. private _lineDatas: lineData[] = [null]
  83. private _hintData: lineData = null
  84. private blockPool: cc.NodePool = null
  85. private nextBlockInfo: nextBlockInfo [] = []
  86. private touchEndFlag: boolean = false
  87. private hardLevel: number = 1
  88. private score: number = 0
  89. private continueXiao: number = 0 // 当前连消
  90. private allContinueXiao: number = 0//当前难度总消除
  91. private moveX = -1
  92. private sprite_color: number = 0 //精灵颜色
  93. private hintFlag = true
  94. public initUI() {
  95. //todo 逻辑
  96. this.testMakeBottomBlock = this.getNode("testUI/makeBottomBlock")
  97. this.testReadyMakeBottomBlock = this.getNode("testUI/readymakeBottomBlock")
  98. this.onTouch(this.testMakeBottomBlock, () => {
  99. this.makeBottomBlock()
  100. })
  101. this.onTouch(this.testReadyMakeBottomBlock, () => {
  102. this.readyMakeBottomBlock()
  103. })
  104. this._startPoint = this.getNode("startPoint")
  105. this._content = this.getNode("content")
  106. this._content_cover = this.getNode("content_cover")
  107. this._hintUI = this.getNode("hintUI")
  108. this._whiteHint = this.getNode("white_hint")
  109. this._textHint = this.getNode("textHint")
  110. this._mouth = this.getNode("content_cover/top/mouth")
  111. this._mask = this.getNode("mask")
  112. this._hammer_sprite = this.getNode("bottomUI/hammer/price")
  113. this._hammer_icon = this.getNode("bottomUI/hammer/vedioIcon")
  114. this._hammer_icon.active = false
  115. this._price_sprite = this.getNode("bottomUI/sprite/price")
  116. this._sprite_icon = this.getNode("bottomUI/sprite/vedioIcon")
  117. this._sprite_icon.active = false
  118. this._hint_mask = this.getNode("hint_mask")
  119. this._hint_mask.active = false
  120. this._hint_label = this.getNode("hint_label")
  121. this._hint_label.active = false
  122. this._hint_hand = this.getNode("hint_hand")
  123. this._hint_hand.active = false
  124. this._menu = this.getNode("bottomUI/menu")
  125. this.onTouch(this._menu, this.handle_menu)
  126. this._menuPanel = this.getNode("bottomUI/menuPanel")
  127. this._menuPanel.active = false
  128. this.onTouch(this._menuPanel.children[0], this.handle_restart)
  129. this.onTouch(this._menuPanel.children[1], this.handle_return)
  130. this._hammer = this.getNode("bottomUI/hammer")
  131. // this._hammer.on(cc.Node.EventType.TOUCH_END, this.handle_hammer, this)
  132. this.onTouch(this._hammer, this.handle_hammer)
  133. this._sprite = this.getNode("bottomUI/sprite")
  134. this.onTouch(this._sprite, this.handle_sprite)
  135. this._hint_hammer = this.getNode("hint_hammer")
  136. this._hint_hammer.active = false
  137. this._hint_sprite = this.getNode("hint_sprite")
  138. this._hint_sprite.active = false
  139. this._hardLevelLabel = this.getNode("content_cover/top/hardLevel").getComponent(cc.Label)
  140. this._scoreLabel = this.getNode("content_cover/top/scoreData").getComponent(cc.Label)
  141. this.updateSprite()
  142. //创建对象池
  143. this.blockPool = new cc.NodePool()
  144. let blockExm = new cc.Node()
  145. blockExm.x = 0
  146. blockExm.y = 0
  147. blockExm.setAnchorPoint(0, 0)
  148. let sprite = blockExm.addComponent(cc.Sprite)
  149. sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM
  150. for (let i = 0; i < 80; i++) {
  151. let node = cc.instantiate(blockExm)
  152. this.blockPool.put(node)
  153. }
  154. this.scheduleOnce(() => {
  155. this._menuPanel.position = this._menu.position
  156. let gridExm = new cc.Node()
  157. gameConfig.gridSize = this._startPoint.height / 10
  158. this._content.width = gameConfig.gridSize * 8
  159. this._content.height = gameConfig.gridSize * 10
  160. this._whiteHint.width = this._content.width
  161. this._whiteHint.height = this._content.height
  162. this._whiteHint.parent = this._content
  163. gridExm.setAnchorPoint(0, 0)
  164. gridExm.width = gameConfig.gridSize
  165. gridExm.height = gameConfig.gridSize
  166. gridExm.opacity = 200
  167. this._hint_hand.width = gameConfig.gridSize
  168. this._hint_hand.height = gameConfig.gridSize
  169. this._hint_hand.setAnchorPoint(0, 0)
  170. let lineContentExm = new cc.Node()
  171. lineContentExm.setAnchorPoint(0.5, 0)
  172. lineContentExm.width = gridExm.width * 8
  173. lineContentExm.height = gridExm.height
  174. this._hintUI.width = lineContentExm.width
  175. let startPosition = this._startPoint.getPosition()
  176. let gridColorTemp = 0 // 0 浅色 1 深色
  177. for (let i = 10; i >= 1; i--) {
  178. let lineContent = cc.instantiate(lineContentExm)
  179. lineContent.name = i.toString()
  180. lineContent.x = 0
  181. lineContent.y = startPosition.y
  182. startPosition.y += lineContent.height
  183. this._content.addChild(lineContent)
  184. let posData = Tools.getNodeFourPoint(lineContent)
  185. lineContent.setAnchorPoint(0, 0)
  186. lineContent.position = cc.v3(posData.left_down)
  187. if (this._hintUI.x != lineContent.x) {
  188. this._hintUI.x = lineContent.x
  189. this._hintData = {
  190. blockNodes: [],
  191. line: this._hintUI,
  192. linePos: [-1]
  193. }
  194. let flagX = 0
  195. for (let i = 1; i <= 8; i++) {
  196. this._hintData.linePos[i] = flagX
  197. flagX += gameConfig.gridSize
  198. }
  199. }
  200. let flagX = 0
  201. let linePosArr: number[] = [-1] //记录 x 轴
  202. for (let j = 1; j <= 8; j++) {
  203. let grid = cc.instantiate(gridExm)
  204. grid.name = j.toString()
  205. let sprite = grid.addComponent(cc.Sprite)
  206. sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM
  207. if (gridColorTemp == 0) {
  208. if (j == 1 && i != 10) {
  209. sprite.spriteFrame = this.graySpriteFrame
  210. } else {
  211. gridColorTemp = 1
  212. sprite.spriteFrame = this.whiteSpriteFrame
  213. }
  214. } else {
  215. if (j == 1 && i != 1) {
  216. sprite.spriteFrame = this.whiteSpriteFrame
  217. } else {
  218. gridColorTemp = 0
  219. sprite.spriteFrame = this.graySpriteFrame
  220. }
  221. }
  222. grid.y = 0
  223. grid.x = flagX
  224. linePosArr.push(grid.x)
  225. flagX += grid.width
  226. lineContent.addChild(grid)
  227. }
  228. this._lineDatas[i] = {
  229. blockNodes: [],
  230. line: lineContent,
  231. linePos: linePosArr
  232. }
  233. if (i == 10) {
  234. this._whiteHint.x = lineContent.x
  235. this._whiteHint.y = lineContent.y
  236. this._whiteHint.active = false
  237. }
  238. }
  239. this.adaptive()
  240. lineContentExm.destroy()
  241. gridExm.destroy()
  242. this._startPoint.destroy()
  243. this.text_start()
  244. this.makeBottomBlock(true)
  245. }, 0)
  246. }
  247. public show(param: any) {
  248. //todo 逻辑
  249. ShowConfig.show('gameConfig').then((res) => {
  250. if (Global.config.gameConfig.bannerShow == 1) {
  251. QgBanner.showBanner();
  252. } else {
  253. QgBanner.hideBanner();
  254. }
  255. });
  256. }
  257. public hide() {
  258. if (Global.config.gameConfig.nativeConfig.type == 2) {
  259. Emit.instance().emit(EmitData.CLOSE_NATIVE) ;
  260. }
  261. }
  262. //todo logic 方法
  263. private getBlock(size: number, color: number = -1): cc.Node {
  264. let block = this.blockPool.get()
  265. block.width = size * gameConfig.gridSize
  266. block.height = gameConfig.gridSize
  267. block.on(cc.Node.EventType.TOUCH_START, this.handle_block_start, this)
  268. block.on(cc.Node.EventType.TOUCH_MOVE, this.handle_block_move, this)
  269. block.on(cc.Node.EventType.TOUCH_END, this.handle_block_end, this)
  270. block.on(cc.Node.EventType.TOUCH_CANCEL, this.handle_block_end, this)
  271. LoadMgr.loadAtlas("view/gameView/block/p").then((p: cc.SpriteAtlas) => {
  272. let id = ((color * 10) + size)
  273. let spriteFrame = p.getSpriteFrame(id.toString())
  274. block.getComponent(cc.Sprite).spriteFrame = spriteFrame
  275. })
  276. return block
  277. }
  278. //归还方块
  279. private returnBlock(node: cc.Node) {
  280. node.off(cc.Node.EventType.TOUCH_START, this.handle_block_start, this)
  281. node.off(cc.Node.EventType.TOUCH_MOVE, this.handle_block_move, this)
  282. node.off(cc.Node.EventType.TOUCH_END, this.handle_block_end, this)
  283. node.off(cc.Node.EventType.TOUCH_CANCEL, this.handle_block_end, this)
  284. node.parent = null
  285. node.x = 0
  286. node.y = 0
  287. node.width = gameConfig.gridSize
  288. node.height = gameConfig.gridSize
  289. node.getComponent(cc.Sprite).spriteFrame = null
  290. this.blockPool.put(node)
  291. return
  292. }
  293. //预创建最低层的方块
  294. private readyMakeBottomBlock() {
  295. //先随机需要空出来几个
  296. //判断是否需要新手提示
  297. if (!CacheMgr.isNeedHint || gameConfig.hint_data.length == 0) {
  298. let blankNum = Tools.getRandom(gameConfig.bottomBlankMin, gameConfig.bottomBlankMax + 1)
  299. let blankColumns: number [] = []
  300. while (true) {
  301. let column = Tools.getRandom(1, 9)
  302. let flag = Tools.JudgeValueInArr(column, blankColumns)
  303. for (let i = 0; i < blankColumns.length; i++) {
  304. if (column == blankColumns[i]) {
  305. flag = true
  306. }
  307. }
  308. if (flag) {
  309. continue
  310. }
  311. blankColumns.push(column)
  312. if (blankColumns.length >= blankNum) {
  313. break
  314. }
  315. }
  316. //获取数组中连续的一段
  317. let allContinueArr: number [] [] = []
  318. let continueArr: number [] = []
  319. for (let i = 1; i < 9; i++) {
  320. if (Tools.JudgeValueInArr(i, blankColumns)) {
  321. if (continueArr.length > 0) {
  322. allContinueArr.push(Tools.deepClone(continueArr))
  323. }
  324. continueArr = []
  325. } else {
  326. continueArr.push(i)
  327. if (i == 8) {
  328. allContinueArr.push(Tools.deepClone(continueArr))
  329. }
  330. }
  331. }
  332. let allBlockInfo: nextBlockInfo[] = []
  333. for (let i = 0; i < allContinueArr.length; i++) {
  334. let blocInfos = this.definitionBlockType(Tools.deepClone(allContinueArr[i]))
  335. blocInfos.forEach((value) => {
  336. allBlockInfo.push(value)
  337. })
  338. }
  339. this.nextBlockInfo = allBlockInfo
  340. } else {
  341. this.nextBlockInfo = gameConfig.hint_data[0]
  342. gameConfig.hint_data.shift()
  343. }
  344. this.updateHint()
  345. }
  346. //根据一个位置数组定义这一组方块类型
  347. private definitionBlockType(arr: number []): nextBlockInfo[] {
  348. let blockInfos: nextBlockInfo[] = []
  349. while (true) {
  350. let length = arr.length
  351. if (arr.length == 0) {
  352. break
  353. }
  354. if (length >= 4) {
  355. if (Tools.checkPer(gameConfig.grade_of_difficulty_config[this.hardLevel].probability_4)) {
  356. blockInfos.push({
  357. column: arr [0],
  358. num: 4,
  359. })
  360. arr.splice(0, 4)
  361. continue
  362. }
  363. }
  364. if (length >= 3) {
  365. if (Tools.checkPer(gameConfig.grade_of_difficulty_config[this.hardLevel].probability_3)) {
  366. blockInfos.push({
  367. column: arr[0],
  368. num: 3
  369. })
  370. arr.splice(0, 3)
  371. continue
  372. }
  373. }
  374. if (length >= 2) {
  375. if (Tools.checkPer(gameConfig.grade_of_difficulty_config[this.hardLevel].probability_2)) {
  376. blockInfos.push({
  377. column: arr[0],
  378. num: 2,
  379. })
  380. arr.splice(0, 3)
  381. continue
  382. }
  383. }
  384. if (length >= 1) {
  385. if (Tools.checkPer(gameConfig.grade_of_difficulty_config[this.hardLevel].probability_1)) {
  386. blockInfos.push({
  387. column: arr[0],
  388. num: 1,
  389. })
  390. arr.splice(0, 1)
  391. }
  392. }
  393. }
  394. return blockInfos
  395. }
  396. //刷新提示
  397. private updateHint() {
  398. this._hintUI.removeAllChildren()
  399. for (let i = 0; i < this.nextBlockInfo.length; i++) {
  400. let info = this.nextBlockInfo[i]
  401. let hintBlock = new cc.Node("hintBlock")
  402. hintBlock.setAnchorPoint(0, 0)
  403. let sprite = hintBlock.addComponent(cc.Sprite)
  404. sprite.type = cc.Sprite.Type.SLICED
  405. sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM
  406. sprite.spriteFrame = this.hintBlockSpriteFrame
  407. hintBlock.width = info.num * gameConfig.gridSize
  408. hintBlock.height = this._hintUI.height
  409. this._hintUI.addChild(hintBlock)
  410. hintBlock.y = 0
  411. hintBlock.x = this._hintData.linePos[info.column]
  412. }
  413. }
  414. //创建最底层的一套方块
  415. private makeBottomBlock(isStart = false) {
  416. if (this.nextBlockInfo.length == 0) {
  417. this.readyMakeBottomBlock()
  418. }
  419. this._mask.active = true
  420. let result = this.upAllLine()
  421. Promise.all(result).then(() => {
  422. //创建方块在下一层
  423. for (let i = 0; i < this.nextBlockInfo.length; i++) {
  424. let lineData = this._lineDatas[10]
  425. let nextBlockInfo = this.nextBlockInfo[i]
  426. let color = Tools.getRandom(1, 6)
  427. let block = this.getBlock(nextBlockInfo.num, color)
  428. block.parent = lineData.line
  429. block.name = "c_" + nextBlockInfo.column
  430. block.x = lineData.linePos[nextBlockInfo.column]
  431. let blockInfo: blockInfo = {
  432. node: block,
  433. column: nextBlockInfo.column,
  434. num: nextBlockInfo.num,
  435. cover: this.getCoverColumn(nextBlockInfo.column, nextBlockInfo.num),
  436. color: color
  437. }
  438. lineData.blockNodes.push(blockInfo)
  439. }
  440. this._mask.active = false
  441. this.readyMakeBottomBlock()
  442. if (isStart) {
  443. this.makeBottomBlock()
  444. } else {
  445. if (CacheMgr.isNeedHint && this.hintFlag) {
  446. this.hintFlag = false
  447. this.hint_play()
  448. }
  449. this.downAllLine(10)
  450. }
  451. })
  452. }
  453. //将一行方块向上移动
  454. private upLine(line: number): any[] {
  455. let result = []
  456. let data = this._lineDatas[line]
  457. let nextData = this._lineDatas[line - 1]
  458. if (data.blockNodes && data.blockNodes.length > 0) {
  459. let nodesData = data.blockNodes
  460. let nextNodesData = nextData.blockNodes
  461. for (let i = 0; i < nodesData.length; i++) {
  462. let n = nodesData[i]
  463. nextNodesData.push(n)
  464. n.node.parent = nextData.line
  465. n.node.y = -gameConfig.gridSize //如果y = 0 的话,就没有动画做了
  466. let p = new Promise((resolve, reject) => {
  467. cc.tween(n.node)
  468. .to(gameConfig.upTime, {y: 0}, {easing: 'cubicInOut'})
  469. .call(() => {
  470. resolve(true)
  471. })
  472. .start()
  473. })
  474. result.push(p)
  475. }
  476. data.blockNodes = []
  477. }
  478. return result
  479. }
  480. //将所有方块向上移动
  481. private upAllLine(): any[] {
  482. //从倒数第二行开始 依次往上移动
  483. let result = []
  484. for (let i = 2; i <= 10; i++) {
  485. let r = this.upLine(i)
  486. for (let i = 0; i < r.length; i++) {
  487. result.push(r[i])
  488. }
  489. }
  490. return result
  491. }
  492. private getCoverColumn(first: number, num: number): number[] {
  493. let arr: number[] = []
  494. for (let i = first; i < first + num; i++) {
  495. arr.push(i)
  496. }
  497. return arr
  498. }
  499. private handle_block_start(e: cc.Event.EventTouch) {
  500. let node: cc.Node = e.target
  501. this._whiteHint.width = node.width
  502. let world = node.parent.convertToWorldSpaceAR(node.position)
  503. let position = this._whiteHint.parent.convertToNodeSpaceAR(world)
  504. this._whiteHint.x = position.x
  505. this._whiteHint.active = true
  506. this.moveX = node.x
  507. }
  508. private handle_block_move(e) {
  509. let node: cc.Node = e.target
  510. let a = e.getDelta()
  511. let world = node.parent.convertToWorldSpaceAR(node.position)
  512. let position = this._whiteHint.parent.convertToNodeSpaceAR(world)
  513. this._whiteHint.x = position.x
  514. let line = Number(node.parent.name)
  515. let column = Number(node.name.split("_")[1])
  516. let data = this.getCanMoveMax(line, column)
  517. // let position2 = node.parent.convertToNodeSpaceAR(e.getLocation())
  518. let x = node.x += a.x;
  519. if (data.min_x > x) {
  520. x = data.min_x
  521. } else if (data.max_x < x) {
  522. x = data.max_x
  523. }
  524. node.x = x
  525. }
  526. private getCanMoveMax(line: number, column: number) {
  527. let lineData = this._lineDatas[line]
  528. let right_column: number = -1
  529. let num: number = 0
  530. let left_column: number = column
  531. for (let i = 0; i < lineData.blockNodes.length; i++) {
  532. let data = lineData.blockNodes[i]
  533. if (data.column == column) {
  534. num = data.num
  535. right_column = data.cover[data.cover.length - 1]
  536. }
  537. }
  538. let max = 0
  539. let min = 0
  540. //寻找左右两边最大能够移动的距离
  541. while (true) {
  542. right_column++
  543. if (right_column > 8) {
  544. max = right_column - 1
  545. break
  546. }
  547. let flag = true
  548. for (let i = 0; i < lineData.blockNodes.length; i++) {
  549. let data = lineData.blockNodes[i]
  550. for (let j = 0; j < data.cover.length; j++) {
  551. if (data.cover[j] == right_column) {
  552. flag = false
  553. break
  554. }
  555. }
  556. }
  557. if (!flag) {
  558. max = right_column - 1
  559. break
  560. } else {
  561. max = right_column
  562. }
  563. }
  564. while (true) {
  565. left_column--
  566. if (left_column < 1) {
  567. min = left_column + 1
  568. break
  569. }
  570. let flag = true
  571. for (let i = 0; i < lineData.blockNodes.length; i++) {
  572. let data = lineData.blockNodes[i]
  573. for (let j = 0; j < data.cover.length; j++) {
  574. if (data.cover[j] == left_column) {
  575. flag = false
  576. break
  577. }
  578. }
  579. }
  580. if (!flag) {
  581. min = left_column + 1
  582. break
  583. } else {
  584. min = left_column
  585. }
  586. }
  587. let min_x = lineData.linePos[min]
  588. let max_x = lineData.linePos[max - num + 1]
  589. return {
  590. min_x: min_x,
  591. max_x: max_x
  592. }
  593. }
  594. private handle_block_end(e) {
  595. let node: cc.Node = e.target
  596. let line = Number(node.parent.name)
  597. let column = Number(node.name.split("_")[1])
  598. this._whiteHint.active = false
  599. let lineData = this._lineDatas[line]
  600. this.continueXiao = 0
  601. for (let i = 1; i <= 8; i++) {
  602. let grid = lineData.line.getChildByName(i.toString())
  603. let position = cc.v2(grid.x + grid.width / 2, grid.y + grid.height / 2)
  604. if (node.getBoundingBox().contains(position)) {
  605. node.x = lineData.linePos[i]
  606. node.name = "c_" + i
  607. for (let j = 0; j < lineData.blockNodes.length; j++) {
  608. let bInfo = lineData.blockNodes[j]
  609. if (bInfo.column == column) {
  610. bInfo.column = i
  611. bInfo.cover = this.getCoverColumn(i, bInfo.num)
  612. break
  613. }
  614. }
  615. break
  616. }
  617. }
  618. if (node.x == this.moveX) {
  619. this.moveX = -1
  620. return
  621. }
  622. AudioMgr.play("move_end").then()
  623. Tools.vibrateShort()
  624. this.touchEndFlag = true
  625. this.downAllLine(line)
  626. }
  627. //将一行方块向下移动
  628. private downLine(line: number): any[] {
  629. let result = []
  630. let data = this._lineDatas[line]
  631. let needChange: any [] = []
  632. for (let i = 0; i < data.blockNodes.length; i++) {
  633. //循环需要下拉行的 所有方块
  634. let blockInfo = data.blockNodes[i]
  635. //判断每一个方块最多可以下降到哪一行
  636. let toLine = -1
  637. for (let j = line + 1; j <= 10; j++) {
  638. let flag = true
  639. let nextData = this._lineDatas[j]
  640. for (let k = 0; k < nextData.blockNodes.length; k++) {
  641. let cover = nextData.blockNodes[k].cover
  642. if (Tools.judgeArraySame(blockInfo.cover, cover)) {
  643. flag = false
  644. }
  645. }
  646. if (flag) {
  647. toLine = j
  648. } else {
  649. break
  650. }
  651. }
  652. if (toLine != -1) {
  653. needChange.push({
  654. index: i,
  655. to: toLine,
  656. })
  657. let p = new Promise((resolve, reject) => {
  658. let n = blockInfo.node
  659. cc.tween(n)
  660. .to(gameConfig.downTime * 0.6, {y: -gameConfig.gridSize * (toLine - line)}, {easing: 'cubicInOut'})
  661. .by(gameConfig.downTime * 0.2, {y: 10},)
  662. .by(gameConfig.downTime * 0.2, {y: -10},)
  663. .union()
  664. .call(() => {
  665. n.parent = this._lineDatas[toLine].line
  666. n.y = 0
  667. resolve(true)
  668. })
  669. .start()
  670. })
  671. result.push(p)
  672. }
  673. }
  674. for (let i = needChange.length - 1; i >= 0; i--) {
  675. let cdata = needChange[i]
  676. this._lineDatas[cdata.to].blockNodes.push(data.blockNodes[cdata.index])
  677. data.blockNodes.splice(cdata.index, 1)
  678. }
  679. return result
  680. }
  681. //将所有方块向下移动
  682. private downAllLine(line: number) {
  683. //从倒数第二行开始 依次往上移动
  684. this._mask.active = true
  685. let result = []
  686. for (let i = line; i >= 1; i--) {
  687. if (i == 10) {
  688. continue
  689. }
  690. let r = this.downLine(i)
  691. for (let i = 0; i < r.length; i++) {
  692. result.push(r[i])
  693. }
  694. }
  695. if (result.length > 0) {
  696. Promise.all(result).then(() => {
  697. AudioMgr.play("down").then()
  698. this.scheduleOnce(() => {
  699. this.judgeAllCanClear()
  700. })
  701. })
  702. } else {
  703. this.judgeAllCanClear()
  704. }
  705. // return result
  706. }
  707. private judgeLineCanClear(line): any {
  708. let result = null
  709. let blockData = this._lineDatas[line].blockNodes
  710. //获取这个一行所有覆盖
  711. let allCover = []
  712. for (let i = 0; i < blockData.length; i++) {
  713. blockData[i].cover.forEach((value) => {
  714. allCover.push(value)
  715. })
  716. }
  717. if (allCover.length >= 8) {
  718. result = new Promise((resolve, reject) => {
  719. cc.tween(this._lineDatas[line].line)
  720. .by(gameConfig.lineShake / 2, {x: -15},)
  721. .by(gameConfig.lineShake / 2, {x: 15},)
  722. // .by(gameConfig.lineShake / 30, {y: 2.5}, {easing: 'cubicInOut'})
  723. // .by(gameConfig.lineShake / 30, {x: 5}, {easing: 'cubicInOut'})
  724. // .by(gameConfig.lineShake / 30, {y: -5}, {easing: 'cubicInOut'})
  725. // .by(gameConfig.lineShake / 30, {y: 2.5, x: -2.5}, {easing: 'cubicInOut'})
  726. .union()
  727. // .repeat(6)
  728. .call(() => {
  729. let line_data = this._lineDatas[line]
  730. for (let i = 0; i < blockData.length; i++) {
  731. this.returnBlock(blockData[i].node)
  732. }
  733. resolve(true)
  734. line_data.blockNodes = []
  735. })
  736. .start()
  737. })
  738. }
  739. return result
  740. }
  741. //判断所有行是否存在可以消除的行
  742. private judgeAllCanClear(): any[] {
  743. let result: any[] = []
  744. for (let i = 1; i <= 10; i++) {
  745. let r = this.judgeLineCanClear(i)
  746. if (r) {
  747. result.push(r)
  748. }
  749. }
  750. if (result.length > 0) {
  751. AudioMgr.play("xiaochu")
  752. Tools.vibrateShort("heavy")
  753. this.continueXiao += result.length
  754. this.allContinueXiao += result.length
  755. this.text_defen(result.length)
  756. this.text_addHard()
  757. Promise.all(result).then(() => {
  758. this.scheduleOnce(() => {
  759. this.downAllLine(10)
  760. }, 0)
  761. if (CacheMgr.isNeedHint) {
  762. this.hint_hint()
  763. }
  764. })
  765. } else {
  766. //没有需要消除的,需要判断一下是不是输了 , 即第1层是不是有东西
  767. if (this._lineDatas[1].blockNodes.length > 0) {
  768. //todo 输了
  769. this.fail_win()
  770. return
  771. } else if (this._lineDatas[9].blockNodes.length == 0) {
  772. this.touchEndFlag = false
  773. this.makeBottomBlock()
  774. } else if (this.touchEndFlag) {
  775. this.touchEndFlag = false
  776. this.makeBottomBlock()
  777. } else {
  778. this._mask.active = false
  779. }
  780. }
  781. return result
  782. }
  783. //适配边框
  784. private adaptive() {
  785. let top = this._content_cover.getChildByName("top")
  786. let left = this._content_cover.getChildByName("left_wall")
  787. let right = this._content_cover.getChildByName("right_wall")
  788. let bottom = this._content_cover.getChildByName("bottom")
  789. let temp = this._lineDatas[1].line.getPosition()
  790. temp.y += gameConfig.gridSize
  791. let left_top = this._content_cover.convertToNodeSpaceAR(this._content.convertToWorldSpaceAR(temp))
  792. temp = Tools.getNodeFourPoint(this._lineDatas[10].line).right_down
  793. let right_bottom = this._content_cover.convertToNodeSpaceAR(this._content.convertToWorldSpaceAR(temp))
  794. top.y = left_top.y
  795. top.width = this._content.width + 20
  796. top.getChildByName("scoreData").getComponent(cc.Widget).updateAlignment()
  797. top.getChildByName("hardLevel").getComponent(cc.Widget).updateAlignment()
  798. bottom.y = right_bottom.y
  799. bottom.width = this._content.width + 20
  800. left.height = this._content.height + 20
  801. left.x = left_top.x
  802. left.y = left_top.y - this._content.height / 2
  803. right.height = this._content.height + 20
  804. right.x = right_bottom.x
  805. right.y = left_top.y - this._content.height / 2
  806. }
  807. update() {
  808. this._scoreLabel.string = "当前得分:" + this.score
  809. this._hardLevelLabel.string = "当前难度:" + this.hardLevel.toString()
  810. if (CacheMgr.setting.hammerNum > 0) {
  811. this._hammer_icon.active = false
  812. this._hammer_sprite.active = true
  813. // console.log("够 的1 ")
  814. this._hammer_sprite.getComponent(cc.Label).string = CacheMgr.setting.hammerNum.toString()
  815. } else {
  816. // console.log("不够1 ")
  817. this._hammer_sprite.active = false
  818. this._hammer_icon.active = true
  819. }
  820. if (CacheMgr.setting.spriteNum > 0) {
  821. // console.log("狗的2 ")
  822. this._sprite_icon.active = false
  823. this._price_sprite.active = true
  824. this._price_sprite.getComponent(cc.Label).string = CacheMgr.setting.spriteNum.toString()
  825. } else {
  826. // console.log("不够2 ")
  827. this._price_sprite.active = false
  828. this._sprite_icon.active = true
  829. }
  830. }
  831. //更新精灵节点
  832. updateSprite() {
  833. this.sprite_color = Tools.getRandom(1, 6)
  834. this._sprite.getChildByName("sprite").getComponent(cc.Sprite).spriteFrame = this.sprite_spriteFrame[this.sprite_color]
  835. }
  836. private fail_win() {
  837. AudioMgr.play("fail")
  838. Tools.vibrateLong()
  839. let result: any [] = []
  840. let time: number = 0
  841. for (let i = 1; i <= 10; i++) {
  842. let lineData = this._lineDatas[i]
  843. lineData.blockNodes.forEach((value) => {
  844. let node = value.node
  845. let world = this._mouth.parent.convertToWorldSpaceAR(this._mouth.position)
  846. let position = node.parent.convertToNodeSpaceAR(world)
  847. position.x -= gameConfig.gridSize / 2
  848. position.y -= gameConfig.gridSize / 2
  849. // node.setAnchorPoint(0.5,0.5)
  850. let p = new Promise((resolve, reject) => {
  851. cc.tween(node)
  852. .delay(time)
  853. .bezierTo(gameConfig.blockFlyTime, cc.v2(Tools.getRandom(0, 500), Tools.getRandom(0, 500)), cc.v2(Tools.getRandom(0, 500), Tools.getRandom(0, 500)), cc.v2(position))
  854. .call(() => {
  855. node.active = false
  856. node.destroy()
  857. resolve(true)
  858. })
  859. .start()
  860. })
  861. result.push(p);
  862. time += gameConfig.blockFlyTime;
  863. })
  864. }
  865. let isNewMax = false;
  866. CacheMgr.gold = CacheMgr.gold + this.score;
  867. if (this.score > CacheMgr.checkpoint) {
  868. CacheMgr.checkpoint = this.score;
  869. isNewMax = true;
  870. }
  871. Promise.all(result).then(() => {
  872. PanelMgr.INS.openPanel({
  873. panel : EndView,
  874. layer : Layer.gameLayer,
  875. param : {
  876. score : this.score,
  877. isNewMax : isNewMax
  878. },
  879. call : ()=>{
  880. PanelMgr.INS.closePanel(GameView) ;
  881. }
  882. })
  883. });
  884. }
  885. private text_defen(n: number) {
  886. let num = this.hardLevel * n * this.continueXiao
  887. this.score += num
  888. let node: cc.Node = null
  889. node = cc.instantiate(this.get_prefab)
  890. node.children[0].getComponent(cc.Label).string = num.toString()
  891. this._textHint.addChild(node)
  892. if (this.continueXiao > 1) {
  893. node = cc.instantiate(this.nice_prefab)
  894. node.children[0].getComponent(cc.Label).string = this.continueXiao.toString()
  895. this._textHint.addChild(node)
  896. }
  897. }
  898. private text_start() {
  899. AudioMgr.play("start")
  900. let text_prefab = cc.instantiate(this.start_prefab)
  901. // text_prefab.getComponent(cc.Label).string = "游戏开始"
  902. text_prefab.getComponent(Text).delay = 5
  903. // this._textHint.addChild(text_prefab)
  904. // text_prefab = cc.instantiate(this.text_prefab)
  905. // text_prefab.getComponent(Text).delay = 5
  906. // text_prefab.getComponent(cc.Label).string = "拖动方块,消除整行."
  907. this._textHint.addChild(text_prefab)
  908. }
  909. private text_addHard() {
  910. if (this.hardLevel >= gameConfig.grade_of_difficulty_config.length - 1) {
  911. return
  912. }
  913. if (this.allContinueXiao >= 10) {
  914. this.hardLevel++
  915. this.allContinueXiao = 2
  916. let text_prefab = cc.instantiate(this.hardUp_prefab)
  917. // text_prefab.getComponent(cc.Label).string = "难度提升,得分X" + this.hardLevel
  918. text_prefab.children[0].getComponent(cc.Label).string = this.hardLevel.toString()
  919. text_prefab.getComponent(Text).delay = 5
  920. this._textHint.addChild(text_prefab)
  921. }
  922. }
  923. private handle_hammer() {
  924. if (this._hint_hammer.active) {
  925. return;
  926. }
  927. if (CacheMgr.setting.hammerNum <= 0) {
  928. Tools.handleVideo().then((res) => {
  929. if (!res) {
  930. return;
  931. }
  932. //判断是否存在3个的方块
  933. let dataBeChui: any [] = []
  934. for (let i = 1; i <= 10; i++) {
  935. this._lineDatas[i].blockNodes.forEach((value) => {
  936. if (value.num >= 3) {
  937. dataBeChui.push({
  938. line: i,
  939. column: value.column
  940. })
  941. }
  942. })
  943. }
  944. if (dataBeChui.length == 0) {
  945. this._hint_hammer.active = true
  946. // Tools.changeGold(gameConfig.price)
  947. this.scheduleOnce(() => {
  948. this._hint_hammer.active = false
  949. }, gameConfig.hide_hint_sprite)
  950. CacheMgr.setting.hammerNum++
  951. CacheMgr.setting = CacheMgr.setting
  952. return
  953. }
  954. this._mask.active = true
  955. let pss = []
  956. let needDelete: Map<number, number[]> = new Map<number, number[]>()
  957. dataBeChui.forEach((value) => {
  958. let pp = new Promise((resolve, reject) => {
  959. let lineData = this._lineDatas[value.line]
  960. let idx = 0
  961. lineData.blockNodes.forEach((value2, index) => {
  962. if (value2.column == value.column) {
  963. idx = index
  964. }
  965. })
  966. let oldBlock = lineData.blockNodes[idx]
  967. let ps = []
  968. for (let i = 0; i < oldBlock.cover.length; i++) {
  969. let h = cc.instantiate(this.hammer_prefab)
  970. h.width = gameConfig.gridSize
  971. h.height = gameConfig.gridSize
  972. let world = oldBlock.node.parent.convertToWorldSpaceAR(oldBlock.node.position)
  973. this._content.addChild(h)
  974. h.position = h.parent.convertToNodeSpaceAR(world)
  975. h.x += oldBlock.node.width / 2
  976. h.y += oldBlock.node.height / 2
  977. let p = new Promise((resolve, reject) => {
  978. cc.tween(h)
  979. .delay(0.5)
  980. .to(gameConfig.hammerRotation, {angle: 30}, {easing: 'cubicInOut'})
  981. .call(() => {
  982. cc.tween(oldBlock.node)
  983. .by(gameConfig.lineShake / 2, {x: -15},)
  984. .by(gameConfig.lineShake / 2, {x: 15},)
  985. .union()
  986. .call(() => {
  987. h.active = false
  988. h.destroy()
  989. let node = this.getBlock(1, oldBlock.color)
  990. node.y = 0
  991. node.x = lineData.linePos[oldBlock.cover[i]]
  992. node.name = "c_" + oldBlock.cover[i]
  993. lineData.line.addChild(node)
  994. lineData.blockNodes.push({
  995. node: node,
  996. column: oldBlock.cover[i],
  997. num: 1,
  998. cover: [oldBlock.cover[i]],
  999. color: oldBlock.color,
  1000. })
  1001. resolve(true)
  1002. })
  1003. .start()
  1004. })
  1005. .start()
  1006. })
  1007. ps.push(p)
  1008. }
  1009. Promise.all(ps).then(() => {
  1010. this.returnBlock(oldBlock.node)
  1011. // needDelete.push({
  1012. // line: value.line,
  1013. // idx: idx
  1014. // })
  1015. if (!needDelete.has(value.line)) {
  1016. needDelete.set(value.line, [])
  1017. }
  1018. needDelete.get(value.line).push(idx)
  1019. // lineData.blockNodes.splice(idx, 1)
  1020. resolve(true)
  1021. })
  1022. })
  1023. pss.push(pp)
  1024. })
  1025. this.scheduleOnce(() => {
  1026. AudioMgr.play("knock")
  1027. }, 0.5)
  1028. Promise.all(pss).then(() => {
  1029. this._mask.active = false
  1030. needDelete.forEach((value, key) => {
  1031. for (let i = this._lineDatas[key].blockNodes.length - 1; i >= 0; i--) {
  1032. if (Tools.JudgeValueInArr(i, value)) {
  1033. this._lineDatas[key].blockNodes.splice(i, 1)
  1034. }
  1035. }
  1036. })
  1037. this.scheduleOnce(() => {
  1038. this.downAllLine(10)
  1039. }, 0)
  1040. })
  1041. })
  1042. } else {
  1043. //判断是否存在3个的方块
  1044. let dataBeChui: any [] = []
  1045. for (let i = 1; i <= 10; i++) {
  1046. this._lineDatas[i].blockNodes.forEach((value) => {
  1047. if (value.num >= 3) {
  1048. dataBeChui.push({
  1049. line: i,
  1050. column: value.column
  1051. })
  1052. }
  1053. })
  1054. }
  1055. if (dataBeChui.length == 0) {
  1056. this._hint_hammer.active = true
  1057. // Tools.changeGold(gameConfig.price)
  1058. this.scheduleOnce(() => {
  1059. this._hint_hammer.active = false
  1060. }, gameConfig.hide_hint_sprite)
  1061. return
  1062. }
  1063. CacheMgr.setting.hammerNum--
  1064. CacheMgr.setting = CacheMgr.setting
  1065. this._mask.active = true
  1066. let pss = []
  1067. let needDelete: Map<number, number[]> = new Map<number, number[]>()
  1068. dataBeChui.forEach((value) => {
  1069. let pp = new Promise((resolve, reject) => {
  1070. let lineData = this._lineDatas[value.line]
  1071. let idx = 0
  1072. lineData.blockNodes.forEach((value2, index) => {
  1073. if (value2.column == value.column) {
  1074. idx = index
  1075. }
  1076. })
  1077. let oldBlock = lineData.blockNodes[idx]
  1078. let ps = []
  1079. for (let i = 0; i < oldBlock.cover.length; i++) {
  1080. let h = cc.instantiate(this.hammer_prefab)
  1081. h.width = gameConfig.gridSize
  1082. h.height = gameConfig.gridSize
  1083. let world = oldBlock.node.parent.convertToWorldSpaceAR(oldBlock.node.position)
  1084. this._content.addChild(h)
  1085. h.position = h.parent.convertToNodeSpaceAR(world)
  1086. h.x += oldBlock.node.width / 2
  1087. h.y += oldBlock.node.height / 2
  1088. let p = new Promise((resolve, reject) => {
  1089. cc.tween(h)
  1090. .delay(0.5)
  1091. .to(gameConfig.hammerRotation, {angle: 30}, {easing: 'cubicInOut'})
  1092. .call(() => {
  1093. cc.tween(oldBlock.node)
  1094. .by(gameConfig.lineShake / 2, {x: -15},)
  1095. .by(gameConfig.lineShake / 2, {x: 15},)
  1096. .union()
  1097. .call(() => {
  1098. h.active = false
  1099. h.destroy()
  1100. let node = this.getBlock(1, oldBlock.color)
  1101. node.y = 0
  1102. node.x = lineData.linePos[oldBlock.cover[i]]
  1103. node.name = "c_" + oldBlock.cover[i]
  1104. lineData.line.addChild(node)
  1105. lineData.blockNodes.push({
  1106. node: node,
  1107. column: oldBlock.cover[i],
  1108. num: 1,
  1109. cover: [oldBlock.cover[i]],
  1110. color: oldBlock.color,
  1111. })
  1112. resolve(true)
  1113. })
  1114. .start()
  1115. })
  1116. .start()
  1117. })
  1118. ps.push(p)
  1119. }
  1120. Promise.all(ps).then(() => {
  1121. this.returnBlock(oldBlock.node)
  1122. // needDelete.push({
  1123. // line: value.line,
  1124. // idx: idx
  1125. // })
  1126. if (!needDelete.has(value.line)) {
  1127. needDelete.set(value.line, [])
  1128. }
  1129. needDelete.get(value.line).push(idx)
  1130. // lineData.blockNodes.splice(idx, 1)
  1131. resolve(true)
  1132. })
  1133. })
  1134. pss.push(pp)
  1135. })
  1136. this.scheduleOnce(() => {
  1137. AudioMgr.play("knock")
  1138. }, 0.5)
  1139. Promise.all(pss).then(() => {
  1140. this._mask.active = false
  1141. needDelete.forEach((value, key) => {
  1142. for (let i = this._lineDatas[key].blockNodes.length - 1; i >= 0; i--) {
  1143. if (Tools.JudgeValueInArr(i, value)) {
  1144. this._lineDatas[key].blockNodes.splice(i, 1)
  1145. }
  1146. }
  1147. })
  1148. this.scheduleOnce(() => {
  1149. this.downAllLine(10)
  1150. }, 0)
  1151. })
  1152. }
  1153. }
  1154. private handle_sprite() {
  1155. if (this._hint_sprite.active) {
  1156. return;
  1157. }
  1158. if (CacheMgr.setting.spriteNum <= 0) {
  1159. Tools.handleVideo().then((res) => {
  1160. if (!res) {
  1161. return;
  1162. }
  1163. let needDelData: Map<number, number[]> = new Map<number, number[]>()
  1164. let ps = []
  1165. //遍历颜色
  1166. for (let i = 1; i <= 10; i++) {
  1167. let blockInfo = this._lineDatas[i].blockNodes
  1168. blockInfo.forEach((value, index) => {
  1169. if (value.color == this.sprite_color) {
  1170. if (!needDelData.has(i)) {
  1171. needDelData.set(i, [])
  1172. }
  1173. needDelData.get(i).push(index)
  1174. let sprite_node = cc.instantiate(this.sprite_prefab)
  1175. sprite_node.scale = gameConfig.gridSize / sprite_node.width
  1176. this.node.addChild(sprite_node)
  1177. sprite_node.position = sprite_node.parent.convertToNodeSpaceAR(this._sprite.parent.convertToWorldSpaceAR(this._sprite.position))
  1178. sprite_node.getComponent(cc.Sprite).spriteFrame = this.sprite_spriteFrame[this.sprite_color]
  1179. let p = new Promise((resolve, reject) => {
  1180. let world = value.node.parent.convertToWorldSpaceAR(value.node.position)
  1181. let position = sprite_node.parent.convertToNodeSpaceAR(world)
  1182. position.x += value.node.width / 2
  1183. position.y += value.node.height / 2
  1184. cc.tween(sprite_node)
  1185. .bezierTo(gameConfig.sprite_move, cc.v2(Tools.getRandom(-500, 500), Tools.getRandom(-500, 500)), cc.v2(Tools.getRandom(-500, 500), Tools.getRandom(-500, 500)), cc.v2(position))
  1186. .call(() => {
  1187. cc.tween(sprite_node)
  1188. .by(gameConfig.sprite_jump / 2, {y: 20}, {easing: 'cubicInOut'})
  1189. .by(gameConfig.sprite_jump / 2, {y: -20}, {easing: 'cubicInOut'})
  1190. .union()
  1191. .call(() => {
  1192. cc.tween(value.node)
  1193. .by(gameConfig.lineShake / 2, {x: -15},)
  1194. .by(gameConfig.lineShake / 2, {x: 15},)
  1195. .union()
  1196. .call(() => {
  1197. sprite_node.active = false
  1198. value.node.active = false
  1199. value.node.destroy()
  1200. resolve(true)
  1201. })
  1202. .start()
  1203. })
  1204. .start()
  1205. })
  1206. .start()
  1207. })
  1208. ps.push(p)
  1209. }
  1210. })
  1211. }
  1212. if (needDelData.size == 0) {
  1213. this._hint_sprite.active = true
  1214. // Tools.changeGold(gameConfig.price)
  1215. this.scheduleOnce(() => {
  1216. this._hint_sprite.active = false
  1217. }, gameConfig.hide_hint_sprite)
  1218. CacheMgr.setting.spriteNum++
  1219. CacheMgr.setting = CacheMgr.setting
  1220. console.log("加上一次提示机会", CacheMgr.setting)
  1221. return
  1222. }
  1223. // AudioMgr.play("sprite_move")
  1224. this._mask.active = true
  1225. Promise.all(ps).then(() => {
  1226. AudioMgr.play("sprite_xiaochu")
  1227. needDelData.forEach((value, key) => {
  1228. for (let i = this._lineDatas[key].blockNodes.length - 1; i >= 0; i--) {
  1229. if (Tools.JudgeValueInArr(i, value)) {
  1230. this._lineDatas[key].blockNodes.splice(i, 1)
  1231. }
  1232. }
  1233. })
  1234. this.updateSprite()
  1235. this.scheduleOnce(() => {
  1236. this.downAllLine(10)
  1237. }, 0)
  1238. this._mask.active = false
  1239. })
  1240. })
  1241. } else {
  1242. let needDelData: Map<number, number[]> = new Map<number, number[]>()
  1243. let ps = []
  1244. //遍历颜色
  1245. for (let i = 1; i <= 10; i++) {
  1246. let blockInfo = this._lineDatas[i].blockNodes
  1247. blockInfo.forEach((value, index) => {
  1248. if (value.color == this.sprite_color) {
  1249. if (!needDelData.has(i)) {
  1250. needDelData.set(i, [])
  1251. }
  1252. needDelData.get(i).push(index)
  1253. let sprite_node = cc.instantiate(this.sprite_prefab)
  1254. sprite_node.scale = gameConfig.gridSize / sprite_node.width
  1255. this.node.addChild(sprite_node)
  1256. sprite_node.position = sprite_node.parent.convertToNodeSpaceAR(this._sprite.parent.convertToWorldSpaceAR(this._sprite.position))
  1257. sprite_node.getComponent(cc.Sprite).spriteFrame = this.sprite_spriteFrame[this.sprite_color]
  1258. let p = new Promise((resolve, reject) => {
  1259. let world = value.node.parent.convertToWorldSpaceAR(value.node.position)
  1260. let position = sprite_node.parent.convertToNodeSpaceAR(world)
  1261. position.x += value.node.width / 2
  1262. position.y += value.node.height / 2
  1263. cc.tween(sprite_node)
  1264. .bezierTo(gameConfig.sprite_move, cc.v2(Tools.getRandom(-500, 500), Tools.getRandom(-500, 500)), cc.v2(Tools.getRandom(-500, 500), Tools.getRandom(-500, 500)), cc.v2(position))
  1265. .call(() => {
  1266. cc.tween(sprite_node)
  1267. .by(gameConfig.sprite_jump / 2, {y: 20}, {easing: 'cubicInOut'})
  1268. .by(gameConfig.sprite_jump / 2, {y: -20}, {easing: 'cubicInOut'})
  1269. .union()
  1270. .call(() => {
  1271. cc.tween(value.node)
  1272. .by(gameConfig.lineShake / 2, {x: -15},)
  1273. .by(gameConfig.lineShake / 2, {x: 15},)
  1274. .union()
  1275. .call(() => {
  1276. sprite_node.active = false
  1277. value.node.active = false
  1278. value.node.destroy()
  1279. resolve(true)
  1280. })
  1281. .start()
  1282. })
  1283. .start()
  1284. })
  1285. .start()
  1286. })
  1287. ps.push(p)
  1288. }
  1289. })
  1290. }
  1291. if (needDelData.size == 0) {
  1292. this._hint_sprite.active = true
  1293. // Tools.changeGold(gameConfig.price)
  1294. this.scheduleOnce(() => {
  1295. this._hint_sprite.active = false
  1296. }, gameConfig.hide_hint_sprite)
  1297. return
  1298. }
  1299. CacheMgr.setting.spriteNum--
  1300. CacheMgr.setting = CacheMgr.setting
  1301. // AudioMgr.play("sprite_move")
  1302. this._mask.active = true
  1303. Promise.all(ps).then(() => {
  1304. AudioMgr.play("sprite_xiaochu")
  1305. needDelData.forEach((value, key) => {
  1306. for (let i = this._lineDatas[key].blockNodes.length - 1; i >= 0; i--) {
  1307. if (Tools.JudgeValueInArr(i, value)) {
  1308. this._lineDatas[key].blockNodes.splice(i, 1)
  1309. }
  1310. }
  1311. })
  1312. this.updateSprite()
  1313. this.scheduleOnce(() => {
  1314. this.downAllLine(10)
  1315. }, 0)
  1316. this._mask.active = false
  1317. })
  1318. }
  1319. }
  1320. private handle_menu() {
  1321. // this._menuPanel.active = !this._menuPanel.active
  1322. if (this._menuPanel.y > this._menu.y) {
  1323. if (this._menuPanel.y != this._menu.y + this._menu.height) {
  1324. return
  1325. }
  1326. tween(this._menuPanel)
  1327. .to(gameConfig.menu_box_move, {y: this._menu.y}, {easing: 'cubicInOut'})
  1328. .call(() => {
  1329. this.scheduleOnce(() => {
  1330. this._menuPanel.active = false
  1331. })
  1332. })
  1333. .start()
  1334. } else {
  1335. this._menuPanel.active = true
  1336. tween(this._menuPanel)
  1337. .to(gameConfig.menu_box_move, {y: this._menu.y + this._menu.height})
  1338. .start()
  1339. }
  1340. }
  1341. //重新开始
  1342. private handle_restart() {
  1343. for (let i = 1; i <= 10; i++) {
  1344. let lineData = this._lineDatas[i]
  1345. lineData.blockNodes.forEach((value) => {
  1346. this.returnBlock(value.node)
  1347. })
  1348. lineData.blockNodes = []
  1349. }
  1350. this.continueXiao = 0
  1351. this.allContinueXiao = 0
  1352. this.hardLevel = 1
  1353. this.score = 0
  1354. this.makeBottomBlock(true)
  1355. }
  1356. //返回首页
  1357. private handle_return() {
  1358. PanelMgr.INS.openPanel({
  1359. panel : HomeView,
  1360. layer : Layer.gameLayer,
  1361. call : ()=>{
  1362. PanelMgr.INS.closePanel(GameView) ;
  1363. }
  1364. })
  1365. }
  1366. update_hintMask() {
  1367. if (this._hint_mask) {
  1368. this._hint_mask.children[0].x = -this._hint_mask.position.x
  1369. this._hint_mask.children[0].y = -this._hint_mask.position.y
  1370. }
  1371. }
  1372. hint_play() {
  1373. this._hint_mask.active = true
  1374. this._hint_mask.width = this._lineDatas[10].line.width
  1375. this._hint_mask.height = this._lineDatas[10].line.height * 2
  1376. this._hint_mask.position = this._lineDatas[10].line.position
  1377. for (let i = 9; i <= 10; i++) {
  1378. if (i == 10) {
  1379. this._lineDatas[i].blockNodes.forEach((value) => {
  1380. let node = value.node
  1381. node.off(cc.Node.EventType.TOUCH_START, this.handle_block_start, this)
  1382. node.off(cc.Node.EventType.TOUCH_MOVE, this.handle_block_move, this)
  1383. node.off(cc.Node.EventType.TOUCH_END, this.handle_block_end, this)
  1384. node.off(cc.Node.EventType.TOUCH_CANCEL, this.handle_block_end, this)
  1385. })
  1386. } else {
  1387. this._lineDatas[i].blockNodes.forEach((value) => {
  1388. if (value.column != 6) {
  1389. let node = value.node
  1390. node.off(cc.Node.EventType.TOUCH_START, this.handle_block_start, this)
  1391. node.off(cc.Node.EventType.TOUCH_MOVE, this.handle_block_move, this)
  1392. node.off(cc.Node.EventType.TOUCH_END, this.handle_block_end, this)
  1393. node.off(cc.Node.EventType.TOUCH_CANCEL, this.handle_block_end, this)
  1394. }
  1395. })
  1396. }
  1397. }
  1398. // this._hint_label.getComponent(cc.Label).string = "按住方块,向左拖动1格"
  1399. this._hint_label.getComponent(cc.Sprite).spriteFrame = this.hint_1_spriteFrame
  1400. this._hint_label.y = this._hint_mask.y + this._hint_mask.height
  1401. this._hint_label.active = true
  1402. this._hint_hand.active = true
  1403. this._hint_hand.width = gameConfig.gridSize
  1404. this._hint_hand.height = gameConfig.gridSize
  1405. let p = cc.v3(this._lineDatas[9].linePos[6])
  1406. let startWorld = this._lineDatas[9].line.convertToWorldSpaceAR(p)
  1407. let startPosition = this.node.convertToNodeSpaceAR(startWorld)
  1408. startPosition.x += gameConfig.gridSize / 2
  1409. startPosition.y -= gameConfig.gridSize / 2
  1410. let p2 = cc.v3(this._lineDatas[9].linePos[5])
  1411. let endWorld = this._lineDatas[9].line.convertToWorldSpaceAR(p2)
  1412. let endPosition = this.node.convertToNodeSpaceAR(endWorld)
  1413. endPosition.y -= gameConfig.gridSize / 2
  1414. this._hint_hand.position = startPosition
  1415. tween(this._hint_hand)
  1416. .to(gameConfig.hint_hand_move, {position: endPosition})
  1417. .to(0, {position: startPosition})
  1418. .union()
  1419. .repeatForever()
  1420. .start()
  1421. this.update_hintMask()
  1422. }
  1423. hint_hint() {
  1424. this._hint_mask.width = this._hintUI.width
  1425. this._hint_mask.height = this._hintUI.height
  1426. this._hint_mask.position = this._hintUI.position
  1427. this._hint_hand.active = false
  1428. // this._hint_label.getComponent(cc.Label).string = "这里是下一行即将出现的方块(点击空白继续)"
  1429. this._hint_label.getComponent(cc.Sprite).spriteFrame = this.hint_2_spriteFrame
  1430. this._hint_label.y = this._hint_mask.y + this._hint_mask.height
  1431. this._hint_label.active = true
  1432. this.update_hintMask()
  1433. this.hint_register()
  1434. CacheMgr.isNeedHint = false
  1435. this._hint_mask.active = false
  1436. this.scheduleOnce(() => {
  1437. tween(this._hint_label)
  1438. .to(1, {opacity: 0})
  1439. .call(() => {
  1440. this._hint_label.active = false
  1441. })
  1442. .start()
  1443. }, 4)
  1444. }
  1445. hint_register() {
  1446. this._lineDatas[9].blockNodes.forEach((value) => {
  1447. let block = value.node
  1448. block.on(cc.Node.EventType.TOUCH_START, this.handle_block_start, this)
  1449. block.on(cc.Node.EventType.TOUCH_MOVE, this.handle_block_move, this)
  1450. block.on(cc.Node.EventType.TOUCH_END, this.handle_block_end, this)
  1451. block.on(cc.Node.EventType.TOUCH_CANCEL, this.handle_block_end, this)
  1452. })
  1453. }
  1454. //重写 gameBoxScroll 滚动方向
  1455. protected gameBoxScrollViewDirection(): string {
  1456. return "v"
  1457. }
  1458. }
  1459. interface lineData {
  1460. line: cc.Node
  1461. linePos: number[]
  1462. blockNodes: blockInfo[]
  1463. }
  1464. export interface nextBlockInfo {
  1465. num: number
  1466. column: number
  1467. }
  1468. interface blockInfo {
  1469. node: cc.Node,
  1470. column: number,
  1471. num: number,
  1472. cover: number[],
  1473. color: number
  1474. }