score.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * @author heyuchang
  3. * @file UI 分数控制器
  4. */
  5. var AC = require('GameAct')
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. scorePrefab: cc.Prefab,
  10. scoreParticlePrefab: cc.Prefab,
  11. mainScoreLabel: cc.Label,
  12. successDialog: require('successDialog'),
  13. characterMgr: require('character'),
  14. failDialog: cc.Node,
  15. multPropPrefab: cc.Prefab,
  16. // progressBar: require('progress'),
  17. // leftStepLabel: cc.Label,
  18. chainSpriteFrameArr: [cc.SpriteFrame],
  19. stepAniLabel: cc.Label,
  20. //提示小框
  21. tipBox: require('tipBox')
  22. },
  23. init(g) {
  24. console.log('zh:初始化score.js ')
  25. this._game = g
  26. this._controller = g._controller
  27. this.score = 0
  28. this.leftStep = this._controller.config.json.originStep
  29. this.chain = 1
  30. this.level = 1
  31. this.reviveTime = 0
  32. this.closeMultLabel()
  33. this.levelData = g._controller.gameData.json.levelData
  34. this.nameLabel.string = "MengXinYue"
  35. this.progressBar.init(0, this.levelData[this.level - 1], this.level)
  36. this.leftStepLabel.string = this.leftStep
  37. this.stepAniLabel.node.runAction(cc.hide())
  38. this.scoreTimer = []
  39. this.currentAddedScore = 0
  40. this.mainScoreLabel.node.active = false
  41. this.characterMgr.showHeroCharacter(this.level)
  42. this.hideChainSprite()
  43. this.tipBox.init(this, 0)
  44. if (this._controller.social.node.active) {
  45. let height = this._controller.social.getHighestLevel()
  46. if (height) {
  47. this.onStep(this.levelData[+height - 1].giftStep)
  48. }
  49. }
  50. },
  51. start() {
  52. this.generatePool()
  53. this.bindNode()
  54. },
  55. generatePool() {
  56. this.scorePool = new cc.NodePool()
  57. for (let i = 0; i < 20; i++) {
  58. let score = cc.instantiate(this.scorePrefab)
  59. this.scorePool.put(score)
  60. }
  61. this.scoreParticlePool = new cc.NodePool()
  62. for (let i = 0; i < 20; i++) {
  63. let scoreParticle = cc.instantiate(this.scoreParticlePrefab)
  64. this.scoreParticlePool.put(scoreParticle)
  65. }
  66. this.multPropPool = new cc.NodePool()
  67. for (let i = 0; i < 3; i++) {
  68. let multProp = cc.instantiate(this.multPropPrefab)
  69. this.multPropPool.put(multProp)
  70. }
  71. },
  72. // 实例化单个方块
  73. instantiateScore(self, num, pos) {
  74. let score = null
  75. if (self.scorePool && self.scorePool.size() > 0) {
  76. score = self.scorePool.get()
  77. } else {
  78. score = cc.instantiate(self.scorePrefab)
  79. }
  80. score.parent = this.scoreContainer
  81. score.getComponent('scoreCell').init(self, num, pos)
  82. let scoreParticle = null
  83. if (self.scoreParticlePool && self.scoreParticlePool.size() > 0) {
  84. scoreParticle = self.scoreParticlePool.get()
  85. } else {
  86. scoreParticle = cc.instantiate(self.scoreParticlePrefab)
  87. }
  88. scoreParticle.parent = this.scoreContainer
  89. scoreParticle.getComponent('scoreParticle').init(self, pos, this._controller.config.json.scoreParticleTime)
  90. },
  91. bindNode() {
  92. this.leftStepLabel = this.node.getChildByName('UI').getChildByName('leftStepNode').getChildByName('Label').getComponent(cc.Label)
  93. this.progressBar = this.node.getChildByName('UI').getChildByName('scoreNode').getChildByName('progressBar').getComponent('progress')
  94. this.scoreContainer = this.node.getChildByName('UI').getChildByName('scoreGroup')
  95. this.multLabel = this.mainScoreLabel.node.getChildByName('mult').getComponent(cc.Label)
  96. this.nameLabel = this.node.getChildByName('UI').getChildByName('scoreNode').getChildByName('progressBar').getChildByName('name').getComponent(cc.Label)
  97. // 失败时更新失败UI
  98. this.chainSprite = this.node.getChildByName('UI').getChildByName('chainSprite').getComponent(cc.Sprite)
  99. this.failScore = this.failDialog.getChildByName('info').getChildByName('score').getComponent(cc.Label)
  100. this.failName = this.failDialog.getChildByName('info').getChildByName('level').getComponent(cc.Label)
  101. this.failSprite = this.failDialog.getChildByName('info').getChildByName('sprite').getComponent(cc.Sprite)
  102. this.failHighScore = this.failDialog.getChildByName('info').getChildByName('highScore').getComponent(cc.Label)
  103. },
  104. //--------------------- 分数控制 ---------------------
  105. // 增加 减少步数并且刷新UI
  106. onStep(num) {
  107. this.leftStep += num
  108. return new Promise((resolve, reject) => {
  109. if (this.leftStep < 0) {
  110. this.leftStep = 0
  111. this.onGameOver()
  112. resolve(false)
  113. } else {
  114. resolve(true)
  115. }
  116. this.leftStepLabel.string = this.leftStep
  117. if (num > 0) {
  118. this.showStepAni(num)
  119. }
  120. })
  121. },
  122. //增加分数总控制 获取连击
  123. addScore(pos, score) {
  124. score = score || this._controller.config.json.scoreBase
  125. // 一次消除可以叠chain
  126. if (this.chainTimer) {
  127. clearTimeout(this.chainTimer)
  128. }
  129. this.initCurrentScoreLabel()
  130. this.chainTimer = setTimeout(() => {
  131. this.onCurrentScoreLabel(this.currentAddedScore, {
  132. x: -60,
  133. y: 355
  134. }, cc.callFunc(() => {
  135. this.score += this.currentAddedScore * this.multiple
  136. this.checkLevelUp()
  137. this.chain = 1
  138. this.closeMultLabel()
  139. this.hideChainSprite()
  140. this.currentAddedScore = 0
  141. this.mainScoreLabel.node.active = false
  142. }, this))
  143. }, 500 / 1
  144. // (cc.game.getFrameRate() / 60)
  145. )
  146. let addScore = score == this._controller.config.json.scoreBase ? (score + (this.chain > 16 ? 16 : (this.chain - 1)) * 10) : score
  147. // let addScore = score == 10 ? score * (this.chain > 10 ? 10 : this.chain) : score
  148. this.currentAddedScore += addScore
  149. this.mainScoreLabel.string = this.currentAddedScore
  150. this.instantiateScore(this, addScore, pos)
  151. this.chain++
  152. this.checkChain()
  153. },
  154. // 判断连击
  155. checkChain() {
  156. if (this.checkChainTimer) {
  157. clearTimeout(this.checkChainTimer)
  158. }
  159. this.checkChainTimer = setTimeout(() => {
  160. let config = this._controller.config.json.chainConfig
  161. for (let i = 0; i < config.length; i++) {
  162. if (this.chain <= config[i].max && this.chain >= config[i].min) {
  163. // console.log(config[i].text)
  164. this.showChainSprite(i)
  165. return
  166. }
  167. }
  168. }, 200)
  169. },
  170. showChainSprite(id) {
  171. this.chainSprite.spriteFrame = this.chainSpriteFrameArr[id]
  172. this.chainSprite.node.scale = 0.5
  173. this.chainSprite.node.active = true
  174. this.chainSprite.node.runAction(AC.popOut(0.3))
  175. },
  176. hideChainSprite() {
  177. this.chainSprite.node.active = false
  178. },
  179. checkLevelUp() {
  180. if (this.level < this.levelData.length && this.score >= this.levelData[this.level - 1].score) {
  181. this.level++
  182. this.level > (this.levelData.length + 1) ? this.levelLimit() : this.onLevelUp()
  183. }
  184. this.progressBar.init(this.score, this.levelData[this.level - 1], this.level)
  185. },
  186. // 增加倍数
  187. addMult(color, pos) {
  188. //TODO: 动态生成一个图片 移动到multLabel上 有bug
  189. // if (this.multPropPool.size() > 0) {
  190. // let multProp = this.multPropPool.get()
  191. // multProp.parent = this.mainScoreLabel.node
  192. // multProp.x = pos.x
  193. // multProp.y = pos.y
  194. // multProp.getComponent(cc.Sprite).spriteFrame = this._game.propSpriteFrame[color - 1]
  195. // multProp.runAction(cc.sequence(cc.moveTo(0.2, 187, 0), cc.callFunc(() => {
  196. // this.multPropPool.put(multProp)
  197. // })))
  198. // }
  199. if (this.multiple < this._controller.config.json.maxMultiple) {
  200. this.multiple *= 2
  201. this.showMultLabel()
  202. }
  203. },
  204. // 关闭倍数的数字显示
  205. closeMultLabel() {
  206. this.multiple = 1
  207. this.multLabel.node.active = false
  208. },
  209. showMultLabel() {
  210. this.multLabel.node.scale = 0.5
  211. this.multLabel.string = this.multiple
  212. this.multLabel.node.active = true
  213. this.multLabel.node.runAction(AC.popOut(0.3))
  214. },
  215. // 增加分数倍数
  216. initCurrentScoreLabel() {
  217. this.mainScoreLabel.node.active = true
  218. this.mainScoreLabel.node.x = 0
  219. this.mainScoreLabel.node.y = 0
  220. this.mainScoreLabel.node.scale = 1
  221. },
  222. // 生成小的分数节点
  223. onCurrentScoreLabel(num, pos, callback) {
  224. // TODO: 增加一个撒花特效
  225. this.mainScoreLabel.string = num
  226. let action = cc.spawn(cc.moveTo(0.2, pos.x, pos.y), cc.scaleTo(0.2, 0.4)).easing(cc.easeBackOut())
  227. let seq = cc.sequence(action, callback)
  228. this.mainScoreLabel.node.runAction(seq)
  229. },
  230. // 升级
  231. onLevelUp() {
  232. this._controller.pageManager.addPage(2)
  233. this._controller.pageManager.addPage(3)
  234. this._controller.musicManager.onWin()
  235. this.successDialog.init(this, this.level, this.levelData, this.score) //升级之后的等级
  236. this.characterMgr.onLevelUp()
  237. this.characterMgr.onSuccessDialog(this.level)
  238. this._game._status = 2
  239. if (this._controller.social.node.active) {
  240. this._controller.social.openBannerAdv()
  241. }
  242. },
  243. // 等级限制
  244. levelLimit() {
  245. console.log('等级达到上限')
  246. this.hideNextLevelData()
  247. },
  248. // 点击升级按钮
  249. onLevelUpButton(double) {
  250. console.log(double)
  251. if (this.isLevelUp) {
  252. return
  253. } else {
  254. this.isLevelUp = true
  255. }
  256. setTimeout(() => {
  257. this.isLevelUp = false
  258. }, 500)
  259. if (double && double.currentTarget) {
  260. double = 1
  261. } else {
  262. double = double || 1
  263. }
  264. this._controller.pageManager.onOpenPage(1)
  265. this.initCurrentScoreLabel()
  266. this.mainScoreLabel.string = this.levelData[this.level - 2].step * double
  267. this.characterMgr.onLevelUpBtn(this.level)
  268. this.nameLabel.string = this.levelData[this.level - 1].name
  269. setTimeout(() => {
  270. this.onCurrentScoreLabel(this.levelData[this.level - 2].step * double, {
  271. x: -248,
  272. y: 350
  273. }, cc.callFunc(() => {
  274. // this.tipBox.init(this) 每次升级就咏诗
  275. this.onStep(this.levelData[this.level - 2].step * double).then()
  276. this._game._status = 1
  277. this.mainScoreLabel.node.active = false
  278. }))
  279. }, 300);
  280. this.showNextLevelData()
  281. this.checkLevelUp()
  282. },
  283. // todo: 新增一个 动画 数字上浮和缩放
  284. showStepAni(num) {
  285. this.stepAniLabel.string = '+' + (num + '')
  286. this.stepAniLabel.node.x = -248
  287. this.stepAniLabel.node.y = 400
  288. this.stepAniLabel.node.runAction(cc.sequence(cc.toggleVisibility(), cc.moveBy(0.6, 0, 60), cc.toggleVisibility()))
  289. let action = cc.sequence(cc.scaleTo(0.2, 0.8), AC.popOut(0.8))
  290. this.leftStepLabel.node.parent.runAction(action)
  291. },
  292. // 游戏结束
  293. // todo 复活
  294. onGameOver(isTrue) {
  295. isTrue = isTrue || 0
  296. if (this._game._status != 3 && (isTrue || this.reviveTime >= 3)) {
  297. this._game.gameOver()
  298. this.updateFailPage()
  299. if (this._controller.social.node.active) {
  300. // 仅上传分数
  301. this._controller.social.onGameOver(this.level, this.score)
  302. }
  303. } else if (!isTrue) {
  304. this._game.askRevive()
  305. }
  306. },
  307. onDoubleStepBtn() {
  308. console.log('zh:点击双倍加步22')
  309. let testMark = true;//无限复活
  310. if(testMark){
  311. this.onLevelUpButton(2)
  312. }
  313. // if (this._controller.social.node.active) {
  314. // this._controller.social.onReviveButton(0)
  315. // } else {
  316. // this.onLevelUpButton(2)
  317. // }
  318. },
  319. onDoubleStep() {
  320. this.onLevelUpButton(2)
  321. },
  322. onRevive() {
  323. this.reviveTime += 1
  324. this.onStep(5).then()
  325. },
  326. // 展示下一级的信息
  327. showNextLevelData() {
  328. let nextLevelData = this.levelData[this.level]
  329. },
  330. // 达到最高级之后 隐藏
  331. hideNextLevelData() {
  332. },
  333. updateFailPage() {
  334. this.failScore.string = " " + (this.score + '')
  335. this.characterMgr.onFail(this.level)
  336. this.failName.string = this.levelData[this.level - 1].name
  337. //this.failHighScore.string = "正在获取您的最高分..."
  338. },
  339. });