element.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @author heyuchang
  3. * @file 单个方块控制
  4. */
  5. //电子邮件puhalskijsemen@gmail.com
  6. //源码网站 开vpn全局模式打开 http://web3incubators.com/
  7. //电报https://t.me/gamecode999
  8. //网页客服 http://web3incubators.com/kefu.html
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. _status: 0, //1为可触发点击 2为已经消失
  13. _itemType: 0, //新增道具功能 1为双倍倍数 2为炸弹
  14. warningSprite: cc.Sprite,
  15. lightSprite: cc.Sprite,
  16. },
  17. init(g, data, width, itemType, pos) {
  18. this._game = g
  19. this._status = 1
  20. if (pos) {
  21. //cc.log('生成的方块', pos)
  22. }
  23. pos = pos || {
  24. x: data.x,
  25. y: data.y
  26. }
  27. this._itemType = itemType || 0
  28. this.warningType = 0
  29. this.isPush = false
  30. this.bindEvent()
  31. this.color = data.color || Math.ceil(Math.random() * 4)
  32. this.colorSprite = this.node.getChildByName('color').getComponent(cc.Sprite)
  33. this.colorSprite.spriteFrame = itemType ? g.propSpriteFrame[(itemType - 1) * 4 + this.color - 1] : this._game.blockSprite[this.color - 1]
  34. this.warningSprite.spriteFrame = ''
  35. this._width = width
  36. this._controller = g._controller
  37. // 计算宽
  38. this.lightSprite.node.active = false
  39. // this.lightSprite.spriteFrame = this._game.blockSprite[this.color - 1]
  40. this.node.width = this.node.height = width
  41. this.startTime = data.startTime
  42. this.iid = data.y
  43. this.jid = data.x
  44. // console.log('生成方块位置', data.y, data.x)
  45. this.node.x = -(730 / 2 - g.gap - width / 2) + pos.x * (width + g.gap)
  46. this.node.y = (730 / 2 - g.gap - width / 2) - pos.y * (width + g.gap)
  47. this.node.angle = 0
  48. this.playStartAction()
  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. let self = this
  113. // 爆炸触发
  114. if (this._status == 1 && isBomb == true) {
  115. this._status = 2
  116. this.playDieAction().then(() => {
  117. this.onBlockPop(color, isChain, isBomb)
  118. })
  119. return
  120. }
  121. if (color.type) {
  122. // 一定是用户主动触发 保存这个坐标给game
  123. if (this.isSingle && this._itemType <= 1) {
  124. this.node.scale = 1
  125. this._game._score.tipBox.init(this._game._score, 3)
  126. let action1 = cc.scaleTo(0.1, 1.1, 0.9)
  127. let action2 = cc.scaleTo(0.3, 1).easing(cc.easeBackOut(2.0))
  128. let action = cc.sequence(action1, action2)
  129. this.node.runAction(action)
  130. return
  131. }
  132. // console.log('方块位置', this.iid, this.jid, this._itemType)
  133. color = this.color
  134. if (this._status == 1 && this._game._status == 1 && this.color == color) {
  135. this._game.onUserTouched(this.iid, this.jid, this._itemType, this.color, this.warningType, {
  136. x: this.node.x,
  137. y: this.node.y
  138. })
  139. this._game._score.onStep(-1).then((res) => {
  140. if (res) {
  141. this.playDieAction().then(() => {
  142. this.onBlockPop(color, null, null)
  143. })
  144. }
  145. })
  146. }
  147. } else {
  148. // 由其他方块触发
  149. if (this._status == 1 && this._game._status == 5 && this.color == color) {
  150. this.playDieAction().then(() => {
  151. this.onBlockPop(color, null, null)
  152. })
  153. }
  154. }
  155. },
  156. onBlockPop(color, isChain, isBomb) {
  157. let self = this
  158. isChain = JSON.stringify(isChain) == 'null' ? true : isChain
  159. isBomb = isBomb ? isBomb : false
  160. self._game.checkNeedFall()
  161. self._game._status = 5
  162. self._controller.musicManager.onPlayAudio(0
  163. //self._game._score.chain - 1
  164. )
  165. if (this._itemType != 0) {
  166. // console.log("触发了道具", this._itemType)
  167. self._game.onItem(this._itemType, color, {
  168. x: this.node.x,
  169. y: this.node.y
  170. })
  171. }
  172. 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)
  173. // 连锁状态
  174. if (isChain) {
  175. if ((self.iid - 1) >= 0) {
  176. self._game.map[self.iid - 1][self.jid].getComponent('element').onTouched(color)
  177. }
  178. if ((self.iid + 1) < this._game.rowNum) {
  179. self._game.map[self.iid + 1][self.jid].getComponent('element').onTouched(color)
  180. }
  181. if ((self.jid - 1) >= 0) {
  182. self._game.map[self.iid][self.jid - 1].getComponent('element').onTouched(color)
  183. }
  184. if ((self.jid + 1) < this._game.rowNum) {
  185. self._game.map[self.iid][self.jid + 1].getComponent('element').onTouched(color)
  186. }
  187. }
  188. },
  189. playFallAction(y, data) { //下降了几个格子
  190. this._status = 0
  191. if (data) {
  192. this.iid = data.y
  193. this.jid = data.x
  194. }
  195. let action = cc.moveBy(0.25, 0, -y * (this._game.gap + this._game.blockWidth)).easing(cc.easeBounceOut(5 / y)) //1 * y / this._game.animationSpeed
  196. let seq = cc.sequence(action, cc.callFunc(() => {
  197. this._status = 1
  198. // this._game.checkNeedGenerator()
  199. }, this))
  200. this.node.runAction(seq)
  201. },
  202. playStartAction() {
  203. this.node.scaleX = 0
  204. this.node.scaleY = 0
  205. let action = cc.scaleTo(0.8 / this._game.animationSpeed, 1, 1).easing(cc.easeBackOut())
  206. let seq = cc.sequence(action, cc.callFunc(() => {
  207. this._status = 1
  208. }, this))
  209. // 如果有延迟时间就用延迟时间
  210. if (this.startTime) {
  211. setTimeout(() => {
  212. this.node.runAction(seq)
  213. }, this.startTime / 1
  214. // (cc.game.getFrameRate() / 60)
  215. )
  216. } else {
  217. this.node.runAction(seq)
  218. }
  219. },
  220. playDieAction() {
  221. let self = this
  222. clearTimeout(this.surfaceTimer)
  223. this.node.stopAllActions()
  224. this._status = 2
  225. this.node.scaleX = 1
  226. this.node.scaleY = 1
  227. return new Promise((resolve, reject) => {
  228. let action
  229. if (this.warningSprite.spriteFrame) { //有道具预警
  230. let action1 = cc.scaleTo(0.2 / self._game.animationSpeed, 1.1)
  231. let action2 = cc.moveTo(0.2 / self._game.animationSpeed, this._game.target.x, this._game.target.y)
  232. let action3 = cc.scaleTo(0.2, 0)
  233. var seq = cc.sequence(action1, cc.callFunc(() => {
  234. resolve('')
  235. }, this), cc.spawn(action2, action3))
  236. } else {
  237. action = cc.scaleTo(0.2 / self._game.animationSpeed, 0, 0)
  238. var seq = cc.sequence(action, cc.callFunc(() => {
  239. resolve('')
  240. }, this))
  241. }
  242. self.node.runAction(seq)
  243. });
  244. },
  245. surfaceAction(dela) {
  246. this.surfaceTimer = setTimeout(() => {
  247. let action = cc.scaleTo(0.4 / this._game.animationSpeed, 0.8, 0.8)
  248. let action1 = cc.scaleTo(0.4 / this._game.animationSpeed, 1, 1)
  249. this.node.runAction(cc.sequence(action, action1))
  250. }, dela)
  251. },
  252. generatePropAction() {
  253. },
  254. generateItem(type) {
  255. this._itemType = type
  256. },
  257. });