score.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * @author uu
  3. * @file UI 分数控制器
  4. */
  5. var AC = require('action')
  6. const ADSdk = require("ADSdk");
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. scorePrefab: cc.Prefab,
  11. scoreParticlePrefab: cc.Prefab,
  12. mainScoreLabel: cc.Label,
  13. successDialog: require('successDialog'),
  14. characterMgr: require('character'),
  15. failDialog: cc.Node,
  16. multPropPrefab: cc.Prefab,
  17. // progressBar: require('progress'),
  18. // leftStepLabel: cc.Label,
  19. chainSpriteFrameArr: [cc.SpriteFrame],
  20. stepAniLabel: cc.Label,
  21. //提示小框
  22. tipBox: require('tipBox')
  23. },
  24. init(g) {
  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 = "农民"
  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.showCharacter(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. let num = Utils.randomNum(1, 100);
  177. let probabilit = AppConst.probabilityConfig.redBag.probability;
  178. // cc.log(num)
  179. if (num < probabilit) {
  180. Utils.setRedPacket();
  181. this.scheduleOnce(() => {
  182. this._controller.showRedBag();
  183. }, 0.5);
  184. }
  185. },
  186. hideChainSprite() {
  187. this.chainSprite.node.active = false
  188. },
  189. checkLevelUp() {
  190. if (this.level < this.levelData.length && this.score >= this.levelData[this.level - 1].score) {
  191. this.level++
  192. this.level > (this.levelData.length + 1) ? this.levelLimit() : this.onLevelUp()
  193. }
  194. this.progressBar.init(this.score, this.levelData[this.level - 1], this.level)
  195. },
  196. // 增加倍数
  197. addMult(color, pos) {
  198. //TODO: 动态生成一个图片 移动到multLabel上 有bug
  199. // if (this.multPropPool.size() > 0) {
  200. // let multProp = this.multPropPool.get()
  201. // multProp.parent = this.mainScoreLabel.node
  202. // multProp.x = pos.x
  203. // multProp.y = pos.y
  204. // multProp.getComponent(cc.Sprite).spriteFrame = this._game.propSpriteFrame[color - 1]
  205. // multProp.runAction(cc.sequence(cc.moveTo(0.2, 187, 0), cc.callFunc(() => {
  206. // this.multPropPool.put(multProp)
  207. // })))
  208. // }
  209. if (this.multiple < this._controller.config.json.maxMultiple) {
  210. this.multiple *= 2
  211. this.showMultLabel()
  212. }
  213. },
  214. // 关闭倍数的数字显示
  215. closeMultLabel() {
  216. this.multiple = 1
  217. this.multLabel.node.active = false
  218. },
  219. showMultLabel() {
  220. this.multLabel.node.scale = 0.5
  221. this.multLabel.string = this.multiple
  222. this.multLabel.node.active = true
  223. this.multLabel.node.runAction(AC.popOut(0.3))
  224. },
  225. // 增加分数倍数
  226. initCurrentScoreLabel() {
  227. this.mainScoreLabel.node.active = true
  228. this.mainScoreLabel.node.x = 0
  229. this.mainScoreLabel.node.y = 0
  230. this.mainScoreLabel.node.scale = 1
  231. },
  232. // 生成小的分数节点
  233. onCurrentScoreLabel(num, pos, callback) {
  234. // TODO: 增加一个撒花特效
  235. this.mainScoreLabel.string = num
  236. let action = cc.spawn(cc.moveTo(0.2, pos.x, pos.y), cc.scaleTo(0.2, 0.4)).easing(cc.easeBackOut())
  237. let seq = cc.sequence(action, callback)
  238. this.mainScoreLabel.node.runAction(seq)
  239. },
  240. // 升级
  241. onLevelUp() {
  242. this._controller.pageMgr.addPage(2)
  243. this._controller.pageMgr.addPage(3)
  244. this._controller.musicMgr.onWin()
  245. this.successDialog.init(this, this.level, this.levelData, this.score) //升级之后的等级
  246. this.characterMgr.onLevelUp()
  247. this.characterMgr.onSuccessDialog(this.level)
  248. this._game._status = 2
  249. // banner
  250. // if (this._controller.social.node.active) {
  251. // this._controller.social.openBannerAdv()
  252. // }
  253. },
  254. // 等级限制
  255. levelLimit() {
  256. //console.log('等级达到上限')
  257. this.hideNextLevelData()
  258. },
  259. // 点击升级按钮
  260. onLevelUpButton(double) {
  261. console.log(double)
  262. if (this.isLevelUp) {
  263. return
  264. } else {
  265. this.isLevelUp = true
  266. }
  267. setTimeout(() => {
  268. this.isLevelUp = false
  269. }, 500)
  270. if (double && double.currentTarget) {
  271. double = 1
  272. } else {
  273. double = double || 1
  274. }
  275. this._controller.pageMgr.onOpenPage(1)
  276. this.initCurrentScoreLabel()
  277. this.mainScoreLabel.string = this.levelData[this.level - 2].step * double
  278. this.characterMgr.onLevelUpBtn(this.level)
  279. this.nameLabel.string = this.levelData[this.level - 1].name
  280. setTimeout(() => {
  281. this.onCurrentScoreLabel(this.levelData[this.level - 2].step * double, {
  282. x: -248,
  283. y: 350
  284. }, cc.callFunc(() => {
  285. // this.tipBox.init(this) 每次升级就咏诗
  286. this.onStep(this.levelData[this.level - 2].step * double).then()
  287. this._game._status = 1
  288. this.mainScoreLabel.node.active = false
  289. }))
  290. }, 300);
  291. this.showNextLevelData()
  292. this.checkLevelUp()
  293. },
  294. // todo: 新增一个 动画 数字上浮和缩放
  295. showStepAni(num) {
  296. this.stepAniLabel.string = '+' + (num + '')
  297. this.stepAniLabel.node.x = -248
  298. this.stepAniLabel.node.y = 400
  299. this.stepAniLabel.node.runAction(cc.sequence(cc.toggleVisibility(), cc.moveBy(0.6, 0, 60), cc.toggleVisibility()))
  300. let action = cc.sequence(cc.scaleTo(0.2, 0.8), AC.popOut(0.8))
  301. this.leftStepLabel.node.parent.runAction(action)
  302. },
  303. // 游戏结束
  304. // todo 复活
  305. onGameOver(isTrue) {
  306. isTrue = isTrue || 0
  307. if (this._game._status != 3 && (isTrue || this.reviveTime >= 3)) {
  308. this._game.gameOver()
  309. this.updateFailPage()
  310. // if (this._controller.social.node.active) {
  311. // // 仅上传分数
  312. // this._controller.social.onGameOver(this.level, this.score)
  313. // }
  314. } else if (!isTrue) {
  315. this._game.askRevive()
  316. }
  317. },
  318. onDoubleStepBtn() {
  319. // TODO 广告 视频步数加倍
  320. if (AppConst.SHOWADUI) {
  321. if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
  322. ADSdk.pay(3, this);
  323. } else {
  324. ADSdk.showVideo(null, (result) => {
  325. if (result == 1) {
  326. this.onLevelUpButton(2)
  327. }
  328. });
  329. }
  330. } else {
  331. this.onLevelUpButton(2)
  332. }
  333. // if (this._controller.social.node.active) {
  334. // this._controller.social.onReviveButton(0)
  335. // } else {
  336. // this.onLevelUpButton(2)
  337. // }
  338. // this._controller.game.fakeShareSuccess()
  339. },
  340. onDoubleStep() {
  341. this.onLevelUpButton(2)
  342. },
  343. onRevive() {
  344. this.reviveTime += 1
  345. this.onStep(5).then()
  346. },
  347. // 展示下一级的信息
  348. showNextLevelData() {
  349. let nextLevelData = this.levelData[this.level]
  350. },
  351. // 达到最高级之后 隐藏
  352. hideNextLevelData() {
  353. },
  354. updateFailPage() {
  355. this.failScore.string = " " + (this.score + '')
  356. this.characterMgr.onFail(this.level)
  357. this.failName.string = this.levelData[this.level - 1].name
  358. //this.failHighScore.string = "正在获取您的最高分..."
  359. },
  360. });