score.js 13 KB

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