cell.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @author uu
  3. * @file 单个方块控制
  4. */
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. _status: 0, //1为可触发点击 2为已经消失
  9. _itemType: 0, //新增道具功能 1为双倍倍数 2为炸弹
  10. warningSprite: cc.Sprite,
  11. lightSprite: cc.Sprite,
  12. },
  13. init(g, data, width, itemType, pos) {
  14. this._game = g
  15. this._status = 1
  16. if (pos) {
  17. //cc.log('生成的方块', pos)
  18. }
  19. pos = pos || {
  20. x: data.x,
  21. y: data.y
  22. }
  23. this._itemType = itemType || 0
  24. this.warningType = 0
  25. this.isPush = false
  26. this.bindEvent()
  27. this.color = data.color || Math.ceil(Math.random() * 4)
  28. this.colorSprite = this.node.getChildByName('color').getComponent(cc.Sprite)
  29. this.colorSprite.spriteFrame = itemType ? g.propSpriteFrame[(itemType - 1) * 4 + this.color - 1] : this._game.blockSprite[this.color - 1]
  30. this.warningSprite.spriteFrame = ''
  31. this._width = width
  32. this._controller = g._controller
  33. // 计算宽
  34. this.lightSprite.node.active = false
  35. // this.lightSprite.spriteFrame = this._game.blockSprite[this.color - 1]
  36. this.node.width = this.node.height = width
  37. this.startTime = data.startTime
  38. this.iid = data.y
  39. this.jid = data.x
  40. // console.log('生成方块位置', data.y, data.x)
  41. this.node.x = -(730 / 2 - g.gap - width / 2) + pos.x * (width + g.gap)
  42. this.node.y = (730 / 2 - g.gap - width / 2) - pos.y * (width + g.gap)
  43. this.node.rotation = 0
  44. this.playStartAction()
  45. },
  46. changeItemType(type){
  47. this._itemType=type
  48. this.colorSprite.spriteFrame = type ? this._game.propSpriteFrame[(type - 1) * 4 + this.color - 1] : this._game.blockSprite[this.color - 1]
  49. },
  50. onWarning(type) {
  51. this.warningSprite.spriteFrame = this._game.warningSpriteFrame[type - 1] || ''
  52. this.warningType = type
  53. // this.lightSprite.node.active = true
  54. let action1 = cc.blink(1, 10)
  55. // this.lightSprite.node.runAction(action1)
  56. },
  57. warningInit() {
  58. this.warningSprite.spriteFrame = ''
  59. // this.lightSprite.node.active = false
  60. this.isPush = false
  61. },
  62. growInit() {
  63. this.growType = 0
  64. this.colorSprite.node.height = this.colorSprite.node.width = this._width
  65. this.colorSprite.node.y = this.colorSprite.node.x = 0
  66. },
  67. grow(type) { //1234 上下左右
  68. switch (type) {
  69. case 1:
  70. if (this.growType != 2) {
  71. this.colorSprite.node.height += this._game.gap * 2
  72. this.colorSprite.node.y += this._game.gap
  73. this.growType = 1
  74. }
  75. break
  76. case 2:
  77. if (this.growType != 2) {
  78. this.colorSprite.node.height += this._game.gap * 2
  79. this.colorSprite.node.y -= this._game.gap
  80. this.growType = 1
  81. }
  82. break
  83. case 3:
  84. if (this.growType != 1) {
  85. this.colorSprite.node.width += this._game.gap * 2
  86. this.colorSprite.node.x -= this._game.gap
  87. this.growType = 2
  88. }
  89. break
  90. case 4:
  91. if (this.growType != 1) {
  92. this.colorSprite.node.width += this._game.gap * 2
  93. this.colorSprite.node.x += this._game.gap
  94. this.growType = 2
  95. }
  96. break
  97. }
  98. },
  99. bindEvent() {
  100. this.node.on(cc.Node.EventType.TOUCH_START, this.onTouched, this)
  101. },
  102. // 用户点击 或者被其他方块触发
  103. onTouched(color, isChain, isBomb, time) { //道具新增参数 isChain是否连锁 isBomb是否强制消除
  104. if (time) {
  105. setTimeout(() => {
  106. this.onTouched(color, false, isBomb)
  107. }, time)
  108. return
  109. }
  110. isChain = JSON.stringify(isChain) == 'null' ? true : isChain
  111. isBomb = isBomb ? isBomb : false
  112. color=color||this.color
  113. let self = this
  114. // 爆炸触发
  115. if (this._status == 1 && isBomb == true) {
  116. this._status = 2
  117. this.playDieAction().then(() => {
  118. this.onBlockPop(color, isChain, isBomb)
  119. })
  120. return
  121. }
  122. if (color.type) {
  123. // 一定是用户主动触发 保存这个坐标给game
  124. if (this.isSingle && this._itemType <= 1) {
  125. this.node.scale = 1
  126. this._game._score.tipBox.init(this._game._score, 3)
  127. let action1 = cc.scaleTo(0.1, 1.1, 0.9)
  128. let action2 = cc.scaleTo(0.3, 1).easing(cc.easeBackOut(2.0))
  129. let action = cc.sequence(action1, action2)
  130. this.node.runAction(action)
  131. return
  132. }
  133. // console.log('方块位置', this.iid, this.jid, this._itemType)
  134. color = this.color
  135. if (this._status == 1 && this._game._status == 1 && this.color == color) {
  136. this._game.onUserTouched(this.iid, this.jid, this._itemType, this.color, this.warningType, {
  137. x: this.node.x,
  138. y: this.node.y
  139. })
  140. this._game._score.onStep(-1).then((res) => {
  141. if (res) {
  142. this.playDieAction().then(() => {
  143. this.onBlockPop(color, null, null)
  144. })
  145. }
  146. })
  147. }
  148. } else {
  149. // 由其他方块触发
  150. if (this._status == 1 && this._game._status == 5 && this.color == color) {
  151. this.playDieAction().then(() => {
  152. this.onBlockPop(color, null, null)
  153. })
  154. }
  155. }
  156. },
  157. onBlockPop(color, isChain, isBomb) {
  158. let self = this
  159. isChain = JSON.stringify(isChain) == 'null' ? true : isChain
  160. isBomb = isBomb ? isBomb : false
  161. self._game.checkNeedFall()
  162. self._game._status = 5
  163. self._controller.musicMgr.onPlayAudio(0
  164. //self._game._score.chain - 1
  165. )
  166. if (this._itemType != 0) {
  167. // console.log("触发了道具", this._itemType)
  168. self._game.onItem(this._itemType, this.color, {
  169. x: this.node.x,
  170. y: this.node.y
  171. })
  172. }
  173. self._game._score.addScore(cc.v2(this.node.x, this.node.y - this.node.width + this._game.gap), this._itemType == 3 ? this._game._controller.config.json.propConfig[2].score : null)
  174. // 连锁状态
  175. if (isChain) {
  176. if ((self.iid - 1) >= 0) {
  177. self._game.map[self.iid - 1][self.jid].getComponent('cell').onTouched(color)
  178. }
  179. if ((self.iid + 1) < this._game.rowNum) {
  180. self._game.map[self.iid + 1][self.jid].getComponent('cell').onTouched(color)
  181. }
  182. if ((self.jid - 1) >= 0) {
  183. self._game.map[self.iid][self.jid - 1].getComponent('cell').onTouched(color)
  184. }
  185. if ((self.jid + 1) < this._game.rowNum) {
  186. self._game.map[self.iid][self.jid + 1].getComponent('cell').onTouched(color)
  187. }
  188. }
  189. },
  190. playFallAction(y, data) { //下降了几个格子
  191. this._status = 0
  192. if (data) {
  193. this.iid = data.y
  194. this.jid = data.x
  195. }
  196. let action = cc.moveBy(0.25, 0, -y * (this._game.gap + this._game.blockWidth)).easing(cc.easeBounceOut(5 / y)) //1 * y / this._game.animationSpeed
  197. let seq = cc.sequence(action, cc.callFunc(() => {
  198. this._status = 1
  199. // this._game.checkNeedGenerator()
  200. }, this))
  201. this.node.runAction(seq)
  202. },
  203. playStartAction() {
  204. this.node.scaleX = 0
  205. this.node.scaleY = 0
  206. let action = cc.scaleTo(0.8 / this._game.animationSpeed, 1, 1).easing(cc.easeBackOut())
  207. let seq = cc.sequence(action, cc.callFunc(() => {
  208. this._status = 1
  209. }, this))
  210. // 如果有延迟时间就用延迟时间
  211. if (this.startTime) {
  212. setTimeout(() => {
  213. this.node.runAction(seq)
  214. }, this.startTime / 1
  215. // (cc.game.getFrameRate() / 60)
  216. )
  217. } else {
  218. this.node.runAction(seq)
  219. }
  220. },
  221. playDieAction() {
  222. let self = this
  223. clearTimeout(this.surfaceTimer)
  224. this.node.stopAllActions()
  225. this._status = 2
  226. this.node.scaleX = 1
  227. this.node.scaleY = 1
  228. return new Promise((resolve, reject) => {
  229. let action
  230. if (this.warningSprite.spriteFrame) { //有道具预警
  231. let action1 = cc.scaleTo(0.2 / self._game.animationSpeed, 1.1)
  232. let action2 = cc.moveTo(0.2 / self._game.animationSpeed, this._game.target.x, this._game.target.y)
  233. let action3 = cc.scaleTo(0.2, 0)
  234. var seq = cc.sequence(action1, cc.callFunc(() => {
  235. resolve('')
  236. }, this), cc.spawn(action2, action3))
  237. } else {
  238. action = cc.scaleTo(0.2 / self._game.animationSpeed, 0, 0)
  239. var seq = cc.sequence(action, cc.callFunc(() => {
  240. resolve('')
  241. }, this))
  242. }
  243. self.node.runAction(seq)
  244. });
  245. },
  246. surfaceAction(dela) {
  247. this.surfaceTimer = setTimeout(() => {
  248. let action = cc.scaleTo(0.4 / this._game.animationSpeed, 0.8, 0.8)
  249. let action1 = cc.scaleTo(0.4 / this._game.animationSpeed, 1, 1)
  250. this.node.runAction(cc.sequence(action, action1))
  251. }, dela)
  252. },
  253. generatePropAction() {
  254. },
  255. generateItem(type) {
  256. this._itemType = type
  257. },
  258. });