score.js 13 KB

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